Example usage for java.util.zip Deflater setLevel

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

Introduction

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

Prototype

public void setLevel(int level) 

Source Link

Document

Sets the compression level to the specified value.

Usage

From source file:org.getspout.spout.packet.PacketBlockData.java

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(data);//from   w w w  . j a  va2 s  .c  o m
        deflater.setLevel(Deflater.BEST_COMPRESSION);
        deflater.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int bytesCompressed = deflater.deflate(buffer);
            bos.write(buffer, 0, bytesCompressed);
        }
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        data = bos.toByteArray();
        compressed = true;
    }
}

From source file:org.getspout.spout.packet.PacketCacheFile.java

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(fileData);/*from  w w w.j  a v  a  2s .  com*/
        deflater.setLevel(Deflater.BEST_COMPRESSION);
        deflater.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.length);
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int bytesCompressed = deflater.deflate(buffer);
            bos.write(buffer, 0, bytesCompressed);
        }
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileData = bos.toByteArray();
        compressed = true;
    }
}

From source file:org.getspout.spoutapi.packet.PacketCustomBlockChunkOverride.java

@Override
public void compress() {
    if (!compressed) {
        if (data != null && hasData) {
            Deflater deflater = new Deflater();
            deflater.setInput(data);//from   w w  w.ja  va  2s .  c  o  m
            deflater.setLevel(Deflater.BEST_COMPRESSION);
            deflater.finish();
            ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
            byte[] buffer = new byte[1024];
            while (!deflater.finished()) {
                int bytesCompressed = deflater.deflate(buffer);
                bos.write(buffer, 0, bytesCompressed);
            }
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            data = bos.toByteArray();
        }
        compressed = true;
    }
}

From source file:spartanfinal.ProcessFiles.java

public byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);
    deflater.setInput(data);/*from w  w w.j  av  a 2  s  .  c  o  m*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    byte[] output = outputStream.toByteArray();

    return output;
}

From source file:org.getspout.spoutapi.packet.PacketAddonData.java

@Override
public void compress() {
    if (!compressed) {
        if (data != null) {
            Deflater deflater = new Deflater();
            deflater.setInput(data);//from w w w. j a v a2s  .c  o  m
            deflater.setLevel(Deflater.BEST_COMPRESSION);
            deflater.finish();
            ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
            byte[] buffer = new byte[1024];
            while (!deflater.finished()) {
                int bytesCompressed = deflater.deflate(buffer);
                bos.write(buffer, 0, bytesCompressed);
            }
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            data = bos.toByteArray();
        }
        compressed = true;
    }
}

From source file:org.getspout.spout.packet.PacketAddonData.java

public void compress() {
    if (!compressed) {
        if (data != null) {
            Deflater deflater = new Deflater();
            deflater.setInput(data);/*  www.  j a v a  2  s  .  com*/
            deflater.setLevel(Deflater.BEST_COMPRESSION);
            deflater.finish();
            ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
            byte[] buffer = new byte[1024];
            while (!deflater.finished()) {
                int bytesCompressed = deflater.deflate(buffer);
                bos.write(buffer, 0, bytesCompressed);
            }
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            data = bos.toByteArray();
        }
        compressed = true;
    }
}

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

/**
 * Sets the content of HTTP response./* w ww.  j  a va  2  s  . 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);
        }
    }
}