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:nl.nn.adapterframework.util.Misc.java

public static byte[] compress(byte[] input) throws IOException {

    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);//from  w w  w  . ja va  2s  .  c o  m
    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(input.length);

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

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

From source file:fr.eoit.util.dumper.DumperTest.java

@Test
public void testCompression() {

    byte[] strBytes = COPYRIGHTS.getBytes();

    byte[] output = new byte[8096];
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
    compresser.setInput(strBytes);//from w w  w  .  j a  v  a 2  s  .c o m
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compresser.end();

    String inputString = new String(Hex.encodeHex(strBytes));
    String hexString = new String(Arrays.copyOf(output, compressedDataLength));

    int i = 0;
    i++;
}

From source file:com.ctriposs.r2.filter.compression.DeflateCompressor.java

@Override
public byte[] deflate(InputStream data) throws CompressionException {
    byte[] input;
    try {//w  w  w.  j av  a  2 s  .c  om
        input = IOUtils.toByteArray(data);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM,
                e);
    }

    Deflater zlib = new Deflater();
    zlib.setInput(input);
    zlib.finish();

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];

    int bytesRead;
    while (!zlib.finished()) {
        bytesRead = zlib.deflate(temp);

        if (bytesRead == 0) {
            if (!zlib.needsInput()) {
                throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
            } else {
                break;
            }
        }
        output.write(temp, 0, bytesRead);
    }
    zlib.end();

    return output.toByteArray();
}

From source file:org.hyperic.hq.livedata.agent.commands.LiveData_result.java

private String compress(String s) throws IOException {
    Deflater compressor = new Deflater();
    compressor.setInput(s.getBytes("UTF-8"));
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);// ww w  .ja v  a 2  s.  com
    }

    bos.close();

    byte[] compressedData = bos.toByteArray();
    return Base64.encode(compressedData);
}

From source file:com.moesol.keys.EncodeUidsTest.java

public void testCompress() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10000; i++) {
        UUID id = UUID.randomUUID();
        sb.append(id);// w w w  .j  a v  a 2 s.  c  o m
        sb.append(',');
    }
    String result = sb.toString();
    //      System.out.println("val=" + result);

    Deflater deflate = new Deflater();
    try {
        byte[] compressed = new byte[512000];
        deflate.setInput(result.getBytes());
        deflate.finish();
        System.out.printf("in=%d out=%d%n", deflate.getBytesRead(), deflate.getBytesWritten());
        deflate.deflate(compressed);
        System.out.printf("in=%d out=%d%n", deflate.getBytesRead(), deflate.getBytesWritten());
    } finally {
        deflate.end();
    }
}

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

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(data);//w w  w .j  a v a  2  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:Comman.Tool.java

public byte[] Image_compress(final byte[] data) {
    if (data == null || data.length == 0) {
        return new byte[0];
    }/*from w  w w  .  ja  va2s  . c  om*/

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream(data.length)) {
        final Deflater deflater = new Deflater();
        deflater.setInput(data);

        deflater.finish();
        final byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            out.write(buffer, 0, deflater.deflate(buffer));
        }

        return out.toByteArray();
    } catch (final IOException e) {
        System.err.println("Compression failed! Returning the original data...");
        return data;
    }
}

From source file:de.triology.cas.logout.LogoutUriEnabledLogoutManagerImpl.java

/**
 * Create a logout message for front channel logout.
 *
 * @param logoutRequest the logout request.
 * @return a front SAML logout message.//from   www. j av a 2  s.  c o  m
 */
public String createFrontChannelLogoutMessage(final LogoutRequest logoutRequest) {
    final String logoutMessage = this.logoutMessageBuilder.create(logoutRequest);
    final Deflater deflater = new Deflater();
    deflater.setInput(logoutMessage.getBytes(ASCII));
    deflater.finish();
    final byte[] buffer = new byte[logoutMessage.length()];
    final int resultSize = deflater.deflate(buffer);
    final byte[] output = new byte[resultSize];
    System.arraycopy(buffer, 0, output, 0, resultSize);
    return Base64.encodeBase64String(output);
}

From source file:com.newrelic.agent.android.harvest.HarvestConnection.java

private byte[] deflate(String str) {
    Deflater deflater = new Deflater();
    deflater.setInput(str.getBytes());//ww w  . j  av a 2  s .c o m
    deflater.finish();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] bArr = new byte[AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD];
    while (!deflater.finished()) {
        int deflate = deflater.deflate(bArr);
        if (deflate <= 0) {
            this.log.error("HTTP request contains an incomplete payload");
        }
        byteArrayOutputStream.write(bArr, 0, deflate);
    }
    deflater.end();
    return byteArrayOutputStream.toByteArray();
}

From source file:org.dragonet.net.ClientChunkManager.java

/**
 * Send a single chunk to the client/*from  w  w w  . ja va2 s  .c o  m*/
 *
 * @param chunkX The chunk X coordinate
 * @param chunkZ The chunk Z coordinate
 */
private synchronized void sendChunk(int chunkX, int chunkZ) {
    try {
        GlowChunkSnapshot chunk = this.getSession().getPlayer().getWorld().getChunkAt(chunkX, chunkZ)
                .getChunkSnapshot();
        ByteArrayOutputStream totalData = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(totalData);
        if (writer.getEndianness() == PEBinaryUtils.BIG_ENDIAN) {
            writer.switchEndianness();
        }
        writer.writeInt(chunkX);
        writer.writeInt(chunkZ);
        for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                for (int y = 0; y < 128; y++) {
                    writer.writeByte((byte) (this.getSession().getTranslator()
                            .translateBlockToPE(chunk.getBlockTypeId(x, y, z)) & 0xFF));
                }
            }
        }
        writer.write(new byte[16384]);
        for (int i = 0; i < 16384; i++) {
            writer.writeByte((byte) 0xF0);
        }
        for (int i = 0; i < 16384; i++) {
            writer.writeByte((byte) 0x11);
        }
        for (int i = 0; i < 256; i++) {
            writer.writeByte((byte) 0x00);
        }
        for (int i = 0; i < 256; i++) {
            writer.writeByte((byte) 0x00);
            writer.writeByte((byte) 0x85);
            writer.writeByte((byte) 0xB2);
            writer.writeByte((byte) 0x4A);
        }
        Deflater deflater = new Deflater(2);
        deflater.reset();
        deflater.setInput(totalData.toByteArray());
        deflater.finish();
        byte[] bufferDeflate = new byte[65536];
        int deflatedSize = deflater.deflate(bufferDeflate);
        FullChunkPacket packet = new FullChunkPacket();
        packet.compressedData = ArrayUtils.subarray(bufferDeflate, 0, deflatedSize);
        this.getSession().send(packet);
    } catch (IOException e) {
    }
}