Example usage for java.util.zip Deflater SYNC_FLUSH

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

Introduction

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

Prototype

int SYNC_FLUSH

To view the source code for java.util.zip Deflater SYNC_FLUSH.

Click Source Link

Document

Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

Usage

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.  java  2 s  .  co  m*/

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

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   w  w w  .  java 2s.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:divconq.ctp.stream.GzipStream.java

protected void deflate(ByteBuf out) {
    int numBytes = 0;

    do {//  w w  w  .jav a2s. c o m
        byte[] o = out.array();

        numBytes = this.deflater.deflate(o, out.arrayOffset() + out.writerIndex(), out.writableBytes(),
                Deflater.SYNC_FLUSH);

        out.writerIndex(out.writerIndex() + numBytes);
    } while (numBytes > 0);
}