Example usage for java.util.zip Deflater Deflater

List of usage examples for java.util.zip Deflater Deflater

Introduction

In this page you can find the example usage for java.util.zip Deflater Deflater.

Prototype

public Deflater() 

Source Link

Document

Creates a new compressor with the default compression level.

Usage

From source file:com.funambol.transport.http.server.Sync4jServlet.java

/**
 * Sets the content of HTTP response./*from  ww w . j a  v  a2s .c  om*/
 *
 * Compresses the response if the Accept-Encoding is gzip or deflate.
 * Sets the Content-Encoding according to the encoding used.
 * Sets the Content-Length with the length of the compressed response.
 * Sets the Uncompressed-Content-Length with the length of the uncompressed
 * response.
 * The response will be compressed only if the length of the uncompressed
 * response is greater than the give sizeThreashold.
 *
 * @param httpResponse the HttpServletResponse
 * @param requestAcceptEncoding the <code>Accept-Encoding</code> specified
 *                              in the request
 * @param sizeThreshold if the response is smaller of this value, it
 *                       should not be compressed
 * @param resp the SyncResponse object contains the response message
 * @param requestTime the time in which the request is arrived to servlet
 * @param sessionId the session identifier
 * @throws java.io.IOException if an error occurs
 *
 */
private void setResponseContent(HttpServletResponse httpResponse, String requestAcceptEncoding,
        String sizeThreshold, SyncResponse resp, long requestTime, String sessionId) throws IOException {

    byte[] responseContent = null;

    OutputStream out = null;
    try {
        out = httpResponse.getOutputStream();

        responseContent = resp.getMessage();
        int uncompressedContentLength = responseContent.length;

        if (supportedEncoding != null && !"".equals(supportedEncoding) && enableCompression) {

            if (log.isTraceEnabled()) {
                log.trace("Setting Accept-Encoding to " + supportedEncoding);
            }

            httpResponse.setHeader(HEADER_ACCEPT_ENCODING, supportedEncoding);
        }

        String encodingToUse = null;

        if (requestAcceptEncoding != null) {

            if (requestAcceptEncoding.indexOf(COMPRESSION_TYPE_GZIP) != -1
                    && requestAcceptEncoding.indexOf(COMPRESSION_TYPE_DEFLATE) != -1) {

                encodingToUse = preferredEncoding;

            } else if (requestAcceptEncoding.indexOf(COMPRESSION_TYPE_DEFLATE) != -1) {

                encodingToUse = COMPRESSION_TYPE_DEFLATE;

            } else if (requestAcceptEncoding.indexOf(COMPRESSION_TYPE_GZIP) != -1) {

                encodingToUse = COMPRESSION_TYPE_GZIP;

            }
        }

        int threshold = 0;
        try {
            if (sizeThreshold != null && sizeThreshold.length() != 0) {
                threshold = Integer.parseInt(sizeThreshold);
            }
        } catch (NumberFormatException ex) {
            //
            // Ignoring the specified value
            //
            if (log.isTraceEnabled()) {
                log.trace("The size threshold specified by the client (" + sizeThreshold + ") is not valid.");
            }
        }

        //
        // If the encodingToUse is null or the
        // uncompressed response length is less than
        // sizeThreshold, the response will not be compressed.
        //
        if (encodingToUse == null || uncompressedContentLength < threshold) {

            if (log.isTraceEnabled()) {
                if (enableCompression) {
                    if (requestAcceptEncoding == null) {
                        log.trace(
                                "The client doesn't support any encoding. " + "The response is not compressed");
                    } else if (encodingToUse == null) {
                        log.trace("The specified Accept-Encoding (" + requestAcceptEncoding
                                + ") is not recognized. The response is not compressed");
                    } else if (uncompressedContentLength < threshold) {
                        log.trace("The response is not compressed because smaller than " + threshold);
                    }
                }
            }

            if (log.isTraceEnabled()) {
                log.trace("Setting Content-Length to: " + uncompressedContentLength);
            }
            httpResponse.setContentLength(uncompressedContentLength);
            out.write(responseContent);
            out.flush();

            return;
        }

        if (encodingToUse != null) {

            if (log.isTraceEnabled()) {
                log.trace("Compressing the response using: " + encodingToUse);
                log.trace("Setting Uncompressed-Content-Length to: " + uncompressedContentLength);
            }

            httpResponse.setHeader(HEADER_UNCOMPRESSED_CONTENT_LENGTH,
                    String.valueOf(uncompressedContentLength));

            if (encodingToUse.equals(COMPRESSION_TYPE_GZIP)) {

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                GZIPOutputStream outTmp = new GZIPOutputStream(bos);
                outTmp.write(responseContent, 0, uncompressedContentLength);
                outTmp.flush();
                outTmp.close();

                //
                // Get the compressed data
                //
                responseContent = bos.toByteArray();
                int compressedLength = responseContent.length;

                if (log.isTraceEnabled()) {
                    log.trace("Setting Content-Length to: " + compressedLength);
                    log.trace("Setting Content-Encoding to: " + COMPRESSION_TYPE_GZIP);
                }

                httpResponse.setContentLength(compressedLength);
                httpResponse.setHeader(HEADER_CONTENT_ENCODING, COMPRESSION_TYPE_GZIP);

                out.write(responseContent);
                out.flush();

            } else if (encodingToUse.equals(COMPRESSION_TYPE_DEFLATE)) {

                //
                // Create the compressor with specificated level of compression
                //
                Deflater compressor = new Deflater();
                compressor.setLevel(compressionLevel);
                compressor.setInput(responseContent);
                compressor.finish();

                //
                // Create an expandable byte array to hold the compressed data.
                // You cannot use an array that's the same size as the orginal because
                // there is no guarantee that the compressed data will be smaller than
                // the uncompressed data.
                //
                ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressedContentLength);

                //
                // Compress the response
                //
                byte[] buf = new byte[SIZE_INPUT_BUFFER];
                while (!compressor.finished()) {
                    int count = compressor.deflate(buf);
                    bos.write(buf, 0, count);
                }

                //
                // Get the compressed data
                //
                responseContent = bos.toByteArray();
                int compressedLength = responseContent.length;

                if (log.isTraceEnabled()) {
                    log.trace("Setting Content-Length to: " + compressedLength);
                    log.trace("Setting Content-Encoding to: " + COMPRESSION_TYPE_DEFLATE);
                }

                httpResponse.setContentLength(compressedLength);
                httpResponse.setHeader(HEADER_CONTENT_ENCODING, COMPRESSION_TYPE_DEFLATE);

                out.write(responseContent);
                out.flush();
            }
        }

    } finally {
        if (out != null) {
            out.close();
        }

        if (logMessages) {
            logResponse(responseContent, requestTime, sessionId);
        }
    }
}

From source file:edu.umass.cs.gigapaxos.SQLPaxosLogger.java

/**
 * @param data/*from www  . j a  va2s.com*/
 * @return Compressed form.
 * @throws IOException
 */
public static byte[] deflate(byte[] data) throws IOException {
    if (!DB_COMPRESSION)
        return data;
    byte[] compressed = null;
    double inflation = 1;
    int compressedLength = data.length;
    do {
        Deflater deflator = new Deflater();
        compressed = new byte[(int) ((inflation *= 1.1) * data.length + 16)];
        deflator.setInput(data);
        deflator.finish();
        compressedLength = deflator.deflate(compressed);
        deflator.end();
    } while (compressedLength == compressed.length);
    return Arrays.copyOf(compressed, compressedLength);
}