Example usage for java.util.zip Deflater deflate

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

Introduction

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

Prototype

public int deflate(byte[] output, int off, int len, int flush) 

Source Link

Document

Compresses the input data and fills the specified buffer with compressed data.

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  ww  w .  ja v  a2 s.c om*/

    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:de.hofuniversity.iisys.neo4j.websock.query.encoding.safe.TSafeDeflateJsonQueryHandler.java

@Override
public ByteBuffer encode(final WebsockQuery query) throws EncodeException {
    ByteBuffer result = null;// ww  w . j  av  a 2  s  . com

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

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

        int read = 0;
        int totalSize = 0;
        final List<byte[]> buffers = new LinkedList<byte[]>();

        final byte[] buffer = new byte[BUFFER_SIZE];
        read = deflater.deflate(buffer, 0, BUFFER_SIZE, Deflater.SYNC_FLUSH);
        while (read > 0) {
            totalSize += read;
            buffers.add(Arrays.copyOf(buffer, read));
            read = deflater.deflate(buffer, 0, BUFFER_SIZE, Deflater.SYNC_FLUSH);
        }

        result = fuse(buffers, totalSize);

        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;
}