Example usage for java.util.zip Deflater finished

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

Introduction

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

Prototype

public boolean finished() 

Source Link

Document

Returns true if the end of the compressed data output stream has been reached.

Usage

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.unsafe.DeflateJsonQueryHandler.java

@Override
public ByteBuffer encode(final WebsockQuery query) throws EncodeException {
    ByteBuffer result = null;//from  www .j  a va  2s.c o m

    try {
        final JSONObject obj = JsonConverter.toJson(query);
        byte[] data = obj.toString().getBytes();

        //compress
        final Deflater deflater = new Deflater(fCompression, true);
        deflater.setInput(data);
        deflater.finish();

        int totalSize = 0;

        int read = deflater.deflate(fBuffer, 0, BUFFER_SIZE, Deflater.SYNC_FLUSH);
        while (true) {
            totalSize += read;

            if (deflater.finished()) {
                //if finished, directly add buffer
                fBuffers.add(fBuffer);
                break;
            } else {
                //make a copy, reuse buffer
                fBuffers.add(Arrays.copyOf(fBuffer, read));
                read = deflater.deflate(fBuffer, 0, BUFFER_SIZE, Deflater.SYNC_FLUSH);
            }
        }

        result = fuse(totalSize);

        deflater.end();

        if (fDebug) {
            fTotalBytesOut += totalSize;
            fLogger.log(Level.FINEST, "encoded compressed JSON message: " + totalSize + " bytes\n"
                    + "total bytes sent: " + fTotalBytesOut);
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new EncodeException(query, "failed to encode JSON", e);
    }

    return result;
}

From source file:se.kth.infosys.lumberjack.protocol.LumberjackClient.java

public int sendCompressedFrame(List<Map<String, byte[]>> keyValuesList) throws IOException {
    output.writeByte(PROTOCOL_VERSION);// w  w  w . j  a v a 2  s  . c  o m
    output.writeByte(FRAME_COMPRESSED);

    ByteArrayOutputStream uncompressedBytes = new ByteArrayOutputStream();
    DataOutputStream uncompressedOutput = new DataOutputStream(uncompressedBytes);
    for (Map<String, byte[]> keyValues : keyValuesList) {
        logger.trace("Adding data frame");
        sendDataFrame(uncompressedOutput, keyValues);
    }
    uncompressedOutput.close();
    Deflater compressor = new Deflater();
    byte[] uncompressedData = uncompressedBytes.toByteArray();
    logger.trace("Deflating data: {} bytes", uncompressedData.length);
    if (logger.isTraceEnabled()) {
        HexDump.dump(uncompressedData, 0, System.out, 0);
    }
    compressor.setInput(uncompressedData);
    compressor.finish();

    ByteArrayOutputStream compressedBytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buffer);
        compressedBytes.write(buffer, 0, count);
    }
    compressedBytes.close();
    byte[] compressedData = compressedBytes.toByteArray();
    logger.trace("Deflated data: {} bytes", compressor.getTotalOut());
    if (logger.isTraceEnabled()) {
        HexDump.dump(compressedData, 0, System.out, 0);
    }

    output.writeInt(compressor.getTotalOut());
    output.write(compressedData);
    output.flush();

    logger.trace("Sending compressed frame: {} frames", keyValuesList.size());
    return 6 + compressor.getTotalOut();
}

From source file:com.hichinaschool.flashcards.libanki.Utils.java

/**
 * Compress data.//from ww  w . j  a  va2  s  .  c  o  m
 * @param bytesToCompress is the byte array to compress.
 * @return a compressed byte array.
 * @throws java.io.IOException
 */
public static byte[] compress(byte[] bytesToCompress, int comp) throws IOException {
    // Compressor with highest level of compression.
    Deflater compressor = new Deflater(comp, true);
    // Give the compressor the data to compress.
    compressor.setInput(bytesToCompress);
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(bytesToCompress.length);

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

    bos.close();

    // Get the compressed data
    return bos.toByteArray();
}

From source file:info.fetter.logstashforwarder.protocol.LumberjackClient.java

public int sendCompressedFrame(List<Map<String, byte[]>> keyValuesList) throws IOException {
    output.writeByte(PROTOCOL_VERSION);//from w w  w  .  j  a v a 2 s  .  c o  m
    output.writeByte(FRAME_COMPRESSED);

    ByteArrayOutputStream uncompressedBytes = new ByteArrayOutputStream();
    DataOutputStream uncompressedOutput = new DataOutputStream(uncompressedBytes);
    for (Map<String, byte[]> keyValues : keyValuesList) {
        logger.trace("Adding data frame");
        sendDataFrame(uncompressedOutput, keyValues);
    }
    uncompressedOutput.close();
    Deflater compressor = new Deflater();
    byte[] uncompressedData = uncompressedBytes.toByteArray();
    if (logger.isDebugEnabled()) {
        logger.debug("Deflating data : " + uncompressedData.length + " bytes");
    }
    if (logger.isTraceEnabled()) {
        HexDump.dump(uncompressedData, 0, System.out, 0);
    }
    compressor.setInput(uncompressedData);
    compressor.finish();

    ByteArrayOutputStream compressedBytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buffer);
        compressedBytes.write(buffer, 0, count);
    }
    compressedBytes.close();
    byte[] compressedData = compressedBytes.toByteArray();
    if (logger.isDebugEnabled()) {
        logger.debug("Deflated data : " + compressor.getTotalOut() + " bytes");
    }
    if (logger.isTraceEnabled()) {
        HexDump.dump(compressedData, 0, System.out, 0);
    }

    output.writeInt(compressor.getTotalOut());
    output.write(compressedData);
    output.flush();

    if (logger.isDebugEnabled()) {
        logger.debug("Sending compressed frame : " + keyValuesList.size() + " frames");
    }
    return 6 + compressor.getTotalOut();
}

From source file:com.itude.mobile.android.util.DataUtil.java

/**
 * Compress byte array //from  ww  w . ja  va2  s . c  om
 * 
 * @param uncompressed byte array
 * @return compressed byte array
 */
public byte[] compress(byte[] uncompressed) {
    byte[] result = null;

    Deflater deflater = new Deflater();
    deflater.setInput(uncompressed);
    deflater.finish();

    // Create an expandable byte array to hold the compressed data. 
    // You cannot use an array that's the same size as the original because 
    // there is no guarantee that the compressed data will be smaller than 
    // the uncompressed data. 

    ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressed.length);

    // Compress the data 
    byte[] buf = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        bos.write(buf, 0, count);
    }
    deflater.end();

    try {
        bos.close();
    } catch (IOException e) {
        MBLog.w(TAG, "Unable to close stream");
    }

    // Get the compressed data 

    result = bos.toByteArray();

    return result;
}

From source file:com.eryansky.common.utils.SysUtils.java

/**
  * //from  w  w w .  j av a 2s  . com
  * 
  * @param in_str
  *            
  * @return ?
  */
 public static byte[] zip_Str(String in_str) {
     byte[] input = new byte[0];
     try {
         input = in_str.getBytes("UTF-8");
     } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
     }
     ArrayList<Byte> al = new ArrayList<Byte>();

     byte[] output;
     Deflater compresser = new Deflater();
     compresser.setInput(input);
     compresser.finish();
     for (; !compresser.finished();) {
         output = new byte[100];
         compresser.deflate(output);
         for (int i = 0; i < output.length; i++) {
             al.add(new Byte(output[i]));
         }
     }
     output = new byte[al.size()];
     for (int i = 0; i < al.size(); i++) {
         output[i] = (al.get(i)).byteValue();
     }
     return output;
 }

From source file:org.adeptnet.auth.saml.SAMLClient.java

private byte[] deflate(final byte[] input) throws IOException {
    // deflate and base-64 encode it
    final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(input);//from  w  w w . j ava2 s  .com
    deflater.finish();

    byte[] tmp = new byte[8192];
    int count;

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while (!deflater.finished()) {
        count = deflater.deflate(tmp);
        bos.write(tmp, 0, count);
    }
    bos.close();
    deflater.end();

    return bos.toByteArray();
}

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

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