Example usage for java.util.zip Deflater finish

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

Introduction

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

Prototype

boolean finish

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

Click Source Link

Usage

From source file:org.sonar.microbenchmark.SerializationBenchmarkTest.java

private File zipFile(File input) throws Exception {
    File zipFile = new File(input.getAbsolutePath() + ".zip");
    Deflater deflater = new Deflater();
    byte[] content = FileUtils.readFileToByteArray(input);
    deflater.setInput(content);//from   w  w  w. j  av a2 s.com
    try (OutputStream outputStream = new FileOutputStream(zipFile)) {
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer); // returns the generated code... index
            outputStream.write(buffer, 0, count);
        }
    }
    deflater.end();

    return zipFile;
}

From source file:org.jmangos.realm.network.packet.wow.client.CMSG_REQUEST_ACCOUNT_DATA.java

@Override
protected void runImpl() {

    if (this.type > AccountDataType.NUM_ACCOUNT_DATA_TYPES.getValue()) {
        return;/*from   w  w w.  jav a2s .c o m*/
    }
    final HashMap<Integer, AccountData> adata = new HashMap<Integer, AccountData>(); // getAccount().getAccountData();
                                                                                     // /*
                                                                                     // disabled
                                                                                     // by
                                                                                     // paalgyula
                                                                                     // */
    if (adata.containsKey(this.type)) {
        final Deflater compressor = new Deflater();
        final byte[] dataToCompress = adata.get(this.type).getData().getBytes(Charset.forName("UTF-8"));

        compressor.setInput(dataToCompress);
        compressor.finish();
        final ByteArrayOutputStream bos = new ByteArrayOutputStream(dataToCompress.length);

        final byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            final int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        try {
            bos.close();
        } catch (final IOException e) {
        }
        // FIXME NEED COMPLETE
        @SuppressWarnings("unused")
        final byte[] compressedData = bos.toByteArray();

    }
}

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 a2  s .  co  m*/
        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);//w  ww  .  ja  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: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  av a 2s  . co  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.apache.qpid.multiconsumer.AMQTest.java

private String compressString(String string) throws Exception {
    long start = System.currentTimeMillis();
    byte[] input = string.getBytes();
    Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);
    compressor.setInput(input);//from  ww w  .  j ava  2s  . co  m
    compressor.finish();

    // Get byte array from output of compressor
    ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int cnt = compressor.deflate(buf);
        baos.write(buf, 0, cnt);
    }
    baos.close();
    byte[] output = baos.toByteArray();

    // Convert byte array into String
    byte[] base64 = Base64.encodeBase64(output);
    String sComp = new String(base64, UTF8);

    long diff = System.currentTimeMillis() - start;
    System.out.println(
            "Compressed text from " + input.length + " to " + sComp.getBytes().length + " in " + diff + " ms");
    System.out.println("Compressed text = '" + sComp + "'");

    return sComp;
}

From source file:org.graylog.plugins.beats.BeatsFrameDecoderTest.java

private ChannelBuffer buildCompressedFrame(byte[] payload, int compressionLevel) {
    final Deflater deflater = new Deflater(compressionLevel);
    deflater.setInput(payload);/*from   www  .  ja  va 2 s.co m*/
    deflater.finish();

    final byte[] compressedPayload = new byte[1024];
    final int compressedPayloadLength = deflater.deflate(compressedPayload);
    deflater.end();

    final ChannelBuffer buffer = ChannelBuffers.buffer(6 + compressedPayloadLength);
    buffer.writeByte('2');
    buffer.writeByte('C');
    // Compressed payload length
    buffer.writeInt(compressedPayloadLength);
    // Compressed payload
    buffer.writeBytes(compressedPayload, 0, compressedPayloadLength);
    return buffer;
}

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

public void compress() {
    if (!compressed) {
        if (data != null) {
            Deflater deflater = new Deflater();
            deflater.setInput(data);/*w  ww.  j  a  v a 2  s  . c  om*/
            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: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 .  j a  va  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);
        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:net.fenyo.mail4hotspot.dns.Msg.java

private byte[] process(final AdvancedServices advancedServices, final Inet4Address address)
        throws GeneralException {
    // log.debug("processing message of type " + input_buffer[0]);
    // for (byte b : input_buffer) log.debug("received byte: [" + b + "]");

    if (input_buffer.length == 0)
        throw new GeneralException("invalid size");
    if (input_buffer[0] == 0) {
        // UTF-8 message type

        final ByteBuffer bb = ByteBuffer.allocate(input_buffer.length - 1);
        bb.put(input_buffer, 1, input_buffer.length - 1);
        bb.position(0);//  ww w .j ava  2 s . c o  m
        final String query = Charset.forName("UTF-8").decode(bb).toString();
        // log.debug("RECEIVED query: [" + query + "]");

        final String reply = advancedServices.processQueryFromClient(query, address);

        // this buffer may not be backed by an accessible byte array, so we do not use Charset.forName("UTF-8").encode(reply).array() to fill output_buffer
        final ByteBuffer ob = Charset.forName("UTF-8").encode(reply);
        ob.get(output_buffer = new byte[ob.limit()]);

        output_size = output_buffer.length;

    } else {
        // binary message type
        // log.debug("processing binary message");

        final ByteBuffer bb = ByteBuffer.allocate(input_buffer[0]);
        bb.put(input_buffer, 1, input_buffer[0]);
        bb.position(0);
        final String query = Charset.forName("UTF-8").decode(bb).toString();
        //      log.debug("RECEIVED query: [" + query + "]");

        final BinaryMessageReply reply = advancedServices.processBinaryQueryFromClient(query,
                Arrays.copyOfRange(input_buffer, input_buffer[0] + 1, input_buffer.length), address);

        // this buffer may not be backed by an accessible byte array, so we do not use Charset.forName("UTF-8").encode(reply).array() to fill string_part
        final ByteBuffer ob = Charset.forName("UTF-8").encode(reply.reply_string);
        final byte[] string_part = new byte[ob.limit()];
        ob.get(string_part);

        if (string_part.length > 255)
            throw new GeneralException("string_part too long");
        output_buffer = new byte[string_part.length + reply.reply_data.length + 1];
        output_buffer[0] = (byte) string_part.length;
        for (int i = 0; i < string_part.length; i++)
            output_buffer[i + 1] = string_part[i];
        for (int i = 0; i < reply.reply_data.length; i++)
            output_buffer[string_part.length + i + 1] = reply.reply_data[i];
        output_size = output_buffer.length;
    }

    synchronized (compressed) {
        // http://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#deflate(byte[])
        // log.debug("processing binary message: length before compressing: " + output_buffer.length);
        final Deflater compresser = new Deflater();
        compresser.setInput(output_buffer);
        compresser.finish();
        final int nbytes = compresser.deflate(compressed);
        //         log.debug("RET: " + nbytes);
        //         log.debug("COMPRESSED: " + compressed.length);
        // log.debug("processing binary message: length after compressing: " + nbytes);
        if (compressed.length == nbytes) {
            log.error("compressed buffer too small...");
            throw new GeneralException("compressed buffer too small...");
        }
        output_buffer = Arrays.copyOf(compressed, nbytes);
        output_size = output_buffer.length;
    }

    synchronized (is_processed) {
        is_processed = true;
    }

    return new byte[] { 'E', 0 }; // 'E'rror 0 == OK
}