Example usage for java.util.zip Deflater end

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

Introduction

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

Prototype

public void end() 

Source Link

Document

Closes the compressor and discards any unprocessed input.

Usage

From source file:acp.sdk.SecureUtil.java

/**
 * .//from   w  ww. j  a v a  2s.com
 * 
 * @param inputByte
 *            ?byte[]
 * @return ??
 * @throws IOException
 */
public static byte[] deflater(final byte[] inputByte) throws IOException {
    int compressedDataLength = 0;
    Deflater compresser = new Deflater();
    compresser.setInput(inputByte);
    compresser.finish();
    ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
    byte[] result = new byte[1024];
    try {
        while (!compresser.finished()) {
            compressedDataLength = compresser.deflate(result);
            o.write(result, 0, compressedDataLength);
        }
    } finally {
        o.close();
    }
    compresser.end();
    return o.toByteArray();
}

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);/*  w w w .  j a  v  a2s  .  c  o  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:com.uber.hoodie.common.HoodieJsonPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater, true);
    try {//from   w ww . j a  v a2  s . c  o m
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
        // Its important to call this.
        // Deflater takes off-heap native memory and does not release until GC kicks in
        deflater.end();
    }
    return baos.toByteArray();
}

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  va  2s . co  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:com.alibaba.citrus.service.requestcontext.session.valueencoder.AbstractSessionValueEncoder.java

private byte[] compress(byte[] data) throws SessionValueEncoderException {
    if (!doCompress()) {
        return data;
    }//from  ww  w  .jav a  2 s .  c o  m

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater def = new Deflater(Deflater.BEST_COMPRESSION, false);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, def);

    try {
        dos.write(data);
    } catch (Exception e) {
        throw new SessionValueEncoderException(e);
    } finally {
        try {
            dos.close();
        } catch (IOException e) {
        }

        def.end();
    }

    return baos.toByteArray();
}

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  www .  ja v a  2s.  c o m
    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:com.alibaba.citrus.service.requestcontext.session.encoder.AbstractSerializationEncoder.java

/** ? */
public String encode(Map<String, Object> attrs, StoreContext storeContext) throws SessionEncoderException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // 1. ?/* w w  w .jav  a2 s  . c om*/
    // 2. 
    Deflater def = new Deflater(Deflater.BEST_COMPRESSION, false);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, def);

    try {
        serializer.serialize(assertNotNull(attrs, "objectToEncode is null"), dos);
    } catch (Exception e) {
        throw new SessionEncoderException("Failed to encode session state", e);
    } finally {
        try {
            dos.close();
        } catch (IOException e) {
        }

        def.end();
    }

    byte[] plaintext = baos.toByteArray().toByteArray();

    // 3. 
    byte[] cryptotext = encrypt(plaintext);

    // 4. base64?
    try {
        String encodedValue = new String(Base64.encodeBase64(cryptotext, false), "ISO-8859-1");

        return URLEncoder.encode(encodedValue, "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        throw new SessionEncoderException("Failed to encode session state", e);
    }
}

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Write a string to the data output. In case the string length exceeds LITERAL_COMPRESS_LENGTH, uses a LZW
 * compressed format, otherwise writes the plain bytes.
 *
 * @param out      output destination to write to
 * @param content  string to write//  ww w.j  a  va  2  s. c om
 * @throws IOException
 */
private static void writeContent(DataOutput out, String content) throws IOException {
    if (content.length() > LITERAL_COMPRESS_LENGTH) {
        // temporary buffer of the size of bytes in the content string (assuming that the compressed data will fit into it)
        byte[] data = content.getBytes("UTF-8");
        byte[] buffer = new byte[data.length];

        Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, true);
        compressor.setInput(data);
        compressor.finish();

        int length = compressor.deflate(buffer);

        // only use compressed version if it is smaller than the number of bytes used by the string
        if (length < buffer.length) {
            log.debug("compressed string with {} bytes; compression ratio {}", data.length,
                    (double) length / data.length);

            out.writeByte(MODE_COMPRESSED);
            out.writeInt(data.length);
            out.writeInt(length);
            out.write(buffer, 0, length);
        } else {
            log.warn("compressed length exceeds string buffer: {} > {}", length, buffer.length);

            out.writeByte(MODE_DEFAULT);
            DataIO.writeString(out, content);
        }

        compressor.end();
    } else {
        out.writeByte(MODE_DEFAULT);
        DataIO.writeString(out, content);
    }
}

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

private byte[] deflate(String str) {
    Deflater deflater = new Deflater();
    deflater.setInput(str.getBytes());//from   ww  w  .j  av  a 2 s  . co 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:com.ctriposs.r2.filter.compression.DeflateCompressor.java

@Override
public byte[] deflate(InputStream data) throws CompressionException {
    byte[] input;
    try {//from w w w  .  j  a va  2 s. c o  m
        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();
}