java.util.zip.Deflater

Here are the examples of the java api class java.util.zip.Deflater taken from open source projects.

1. DeflateCompressor#compressArray()

Project: cassandra
Source File: DeflateCompressor.java
View license
public int compressArray(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) {
    Deflater def = deflater.get();
    def.reset();
    def.setInput(input, inputOffset, inputLength);
    def.finish();
    if (def.needsInput())
        return 0;
    int len = def.deflate(output, outputOffset, maxOutputLength);
    assert def.finished();
    return len;
}

2. ToolsTest#testDecompressZlib()

Project: graylog2-server
Source File: ToolsTest.java
View license
@Test
public void testDecompressZlib() throws IOException {
    String testString = "Teststring 123";
    byte[] buffer = new byte[100];
    Deflater deflater = new Deflater();
    deflater.setInput(testString.getBytes());
    deflater.finish();
    deflater.deflate(buffer);
    deflater.end();
    assertEquals(testString, Tools.decompressZlib(buffer));
}

3. Zlib#deflate()

Project: Nukkit
Source File: Zlib.java
View license
public static byte[] deflate(byte[] data, int level) throws Exception {
    Deflater deflater = new Deflater(level);
    deflater.reset();
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    byte[] buf = new byte[1024];
    while (!deflater.finished()) {
        int i = deflater.deflate(buf);
        bos.write(buf, 0, i);
    }
    deflater.end();
    return bos.toByteArray();
}

4. MinimalPerfectHash#compress()

View license
/**
     * Compress the hash description using a Huffman coding.
     *
     * @param d the data
     * @return the compressed data
     */
private static byte[] compress(byte[] d) {
    Deflater deflater = new Deflater();
    deflater.setStrategy(Deflater.HUFFMAN_ONLY);
    deflater.setInput(d);
    deflater.finish();
    ByteArrayOutputStream out2 = new ByteArrayOutputStream(d.length);
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        out2.write(buffer, 0, count);
    }
    deflater.end();
    return out2.toByteArray();
}

5. PerfectHash#compress()

Project: ThriftyPaxos
Source File: PerfectHash.java
View license
/**
     * Compress the hash description using a Huffman coding.
     *
     * @param d the data
     * @return the compressed data
     */
private static byte[] compress(byte[] d) {
    Deflater deflater = new Deflater();
    deflater.setStrategy(Deflater.HUFFMAN_ONLY);
    deflater.setInput(d);
    deflater.finish();
    ByteArrayOutputStream out2 = new ByteArrayOutputStream(d.length);
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        out2.write(buffer, 0, count);
    }
    deflater.end();
    return out2.toByteArray();
}

6. CompressDeflate#compress()

Project: ThriftyPaxos
Source File: CompressDeflate.java
View license
@Override
public int compress(byte[] in, int inLen, byte[] out, int outPos) {
    Deflater deflater = new Deflater(level);
    deflater.setStrategy(strategy);
    deflater.setInput(in, 0, inLen);
    deflater.finish();
    int compressed = deflater.deflate(out, outPos, out.length - outPos);
    while (compressed == 0) {
        // the compressed length is 0, meaning compression didn't work
        // (sounds like a JDK bug)
        // try again, using the default strategy and compression level
        strategy = Deflater.DEFAULT_STRATEGY;
        level = Deflater.DEFAULT_COMPRESSION;
        return compress(in, inLen, out, outPos);
    }
    deflater.end();
    return outPos + compressed;
}

7. TestHelper#zlibCompress()

Project: graylog2-server
Source File: TestHelper.java
View license
public static byte[] zlibCompress(String what) {
    byte[] input = what.getBytes(StandardCharsets.UTF_8);
    // Compress the bytes
    byte[] output = new byte[4096];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    compresser.deflate(output);
    return output;
}

8. InflaterTest#deflate()

Project: j2objc
Source File: InflaterTest.java
View license
private static byte[] deflate(byte[] input, byte[] dictionary) {
    Deflater deflater = new Deflater();
    if (dictionary != null) {
        deflater.setDictionary(dictionary);
    }
    deflater.setInput(input);
    deflater.finish();
    ByteArrayOutputStream deflatedBytes = new ByteArrayOutputStream();
    byte[] buf = new byte[8];
    while (!deflater.finished()) {
        int byteCount = deflater.deflate(buf);
        deflatedBytes.write(buf, 0, byteCount);
    }
    deflater.end();
    return deflatedBytes.toByteArray();
}

9. CompressionSizeBenchmark#compress()

View license
private byte[] compress(byte[] bytes) {
    Deflater compressor = new Deflater();
    compressor.setLevel(compressionLevelMap.get(compressionLevel));
    compressor.setInput(bytes);
    compressor.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    return bos.toByteArray();
}

10. Utils#compress()

Project: TomP2P
Source File: Utils.java
View license
public static byte[] compress(byte[] input) {
    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_SPEED);
    // Give the compressor the data to compress
    compressor.setInput(input);
    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);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    // Get the compressed data
    return bos.toByteArray();
}

11. Utils#compress()

Project: bitsquare
Source File: Utils.java
View license
private static byte[] compress(byte[] input) {
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_SPEED);
    compressor.setInput(input);
    compressor.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[8192];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException ignored) {
    }
    return bos.toByteArray();
}

12. ZipPlugin#compress()

Project: openjdk
Source File: ZipPlugin.java
View license
static byte[] compress(byte[] bytesIn) {
    Deflater deflater = new Deflater();
    deflater.setInput(bytesIn);
    ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length);
    byte[] buffer = new byte[1024];
    deflater.finish();
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        stream.write(buffer, 0, count);
    }
    try {
        stream.close();
    } catch (IOException ex) {
        return bytesIn;
    }
    byte[] bytesOut = stream.toByteArray();
    deflater.end();
    return bytesOut;
}

13. ZlibCodec#compress()

Project: bioformats
Source File: ZlibCodec.java
View license
/* @see Codec#compress(byte[], CodecOptions) */
@Override
public byte[] compress(byte[] data, CodecOptions options) throws FormatException {
    if (data == null || data.length == 0)
        throw new IllegalArgumentException("No data to compress");
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    deflater.finish();
    byte[] buf = new byte[8192];
    ByteVector bytes = new ByteVector();
    int r = 0;
    // compress until eof reached
    while ((r = deflater.deflate(buf, 0, buf.length)) > 0) {
        bytes.add(buf, 0, r);
    }
    return bytes.toByteArray();
}

14. CompressionUtils#compress()

Project: kylin
Source File: CompressionUtils.java
View license
public static byte[] compress(byte[] data) throws IOException {
    long startTime = System.currentTimeMillis();
    Deflater deflater = new Deflater(1);
    deflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        // returns the generated code... index
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    logger.debug("Original: " + data.length + " bytes. " + "Compressed: " + output.length + " byte. Time: " + (System.currentTimeMillis() - startTime));
    return output;
}

15. DeflaterUtils#deflate()

View license
public static byte[] deflate(byte[] inputBytes) {
    /*
		 * For smaller inputs, the size of deflated content can be bigger than the input.
	     * So, adding some buffer for that.
		 */
    int additionalBufferSize = 10;
    int deflaterBufferSize = inputBytes.length + additionalBufferSize;
    // Deflate the content
    byte[] deflatedBytes = new byte[deflaterBufferSize];
    // Set nowrap to true, to ignore the ZLIB headers
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(inputBytes);
    deflater.finish();
    int number_deflated_bytes = deflater.deflate(deflatedBytes);
    return Arrays.copyOf(deflatedBytes, number_deflated_bytes);
}

16. Utils#compress()

Project: Anki-Android
Source File: Utils.java
View license
/**
     * Compress data.
     * @param bytesToCompress is the byte array to compress.
     * @return a compressed byte array.
     * @throws java.io.IOException
     */
public static byte[] compress(byte[] bytesToCompress, int comp) throws IOException {
    // Compressor with highest level of compression.
    Deflater compressor = new Deflater(comp, true);
    // Give the compressor the data to compress.
    compressor.setInput(bytesToCompress);
    compressor.finish();
    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(bytesToCompress.length);
    // Compress the data
    byte[] buf = new byte[65536];
    while (!compressor.finished()) {
        bos.write(buf, 0, compressor.deflate(buf));
    }
    bos.close();
    // Get the compressed data
    return bos.toByteArray();
}

17. TezUtils#compressBytesInflateDeflate()

Project: incubator-tez
Source File: TezUtils.java
View license
private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    deflater.setInput(inBytes);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
    deflater.finish();
    byte[] buffer = new byte[1024 * 8];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        bos.write(buffer, 0, count);
    }
    byte[] output = bos.toByteArray();
    return output;
}

18. TezUtilsInternal#compressBytesInflateDeflate()

Project: tez
Source File: TezUtilsInternal.java
View license
private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    deflater.setInput(inBytes);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
    deflater.finish();
    byte[] buffer = new byte[1024 * 8];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        bos.write(buffer, 0, count);
    }
    byte[] output = bos.toByteArray();
    return output;
}

19. DeflaterSinkTest#closeWithExceptionWhenWritingAndClosing()

Project: okio
Source File: DeflaterSinkTest.java
View license
/**
   * This test deflates a single segment of without compression because that's
   * the easiest way to force close() to emit a large amount of data to the
   * underlying sink.
   */
@Test
public void closeWithExceptionWhenWritingAndClosing() throws IOException {
    MockSink mockSink = new MockSink();
    mockSink.scheduleThrow(0, new IOException("first"));
    mockSink.scheduleThrow(1, new IOException("second"));
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.NO_COMPRESSION);
    DeflaterSink deflaterSink = new DeflaterSink(mockSink, deflater);
    deflaterSink.write(new Buffer().writeUtf8(repeat('a', Segment.SIZE)), Segment.SIZE);
    try {
        deflaterSink.close();
        fail();
    } catch (IOException expected) {
        assertEquals("first", expected.getMessage());
    }
    mockSink.assertLogContains("close()");
}

20. DeflateCompressor#compress()

View license
public static byte[] compress(byte[] input) throws IOException {
    // Destination where compressed data will be stored.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Create a compressor.
    Deflater deflater = createDeflater();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater);
    // Compress the data.
    //
    // Some other implementations such as Jetty and Tyrus use
    // Deflater.deflate(byte[], int, int, int) with Deflate.SYNC_FLUSH,
    // but this implementation does not do it intentionally because the
    // method and the constant value are not available before Java 7.
    dos.write(input, 0, input.length);
    dos.close();
    // Release the resources held by the compressor.
    deflater.end();
    // Retrieve the compressed data.
    return baos.toByteArray();
}

21. DeflateSerializer#write()

Project: kryo
Source File: DeflateSerializer.java
View license
public void write(Kryo kryo, Output output, Object object) {
    Deflater deflater = new Deflater(compressionLevel, noHeaders);
    OutputChunked outputChunked = new OutputChunked(output, 256);
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(outputChunked, deflater);
    Output deflaterOutput = new Output(deflaterStream, 256);
    kryo.writeObject(deflaterOutput, object, serializer);
    deflaterOutput.flush();
    try {
        deflaterStream.finish();
    } catch (IOException ex) {
        throw new KryoException(ex);
    }
    outputChunked.endChunks();
}

22. TestDataGen#testCompressibility()

Project: linkbench
Source File: TestDataGen.java
View license
private void testCompressibility(MotifDataGenerator gen, int blockSize, int blocks) throws IOException {
    long seed = System.nanoTime();
    System.err.println("seed = " + seed);
    Random rng = new Random(seed);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
    DeflaterOutputStream gzipOut = new DeflaterOutputStream(byteOut, def);
    byte block[] = new byte[blockSize];
    for (int i = 0; i < blocks; i++) {
        gen.fill(rng, block);
        gzipOut.write(block);
    }
    gzipOut.close();
    byte compressed[] = byteOut.toByteArray();
    int origLen = blockSize * blocks;
    System.err.format("%dx%d blocks.  Compressed %d bytes to %d: %.2f.  Bound: %.2f\n", blocks, blockSize, origLen, compressed.length, compressed.length / (double) origLen, gen.estMaxCompression());
}

23. ZipUtil#zlib()

Project: jodd
Source File: ZipUtil.java
View license
/**
	 * Compresses a file into zlib archive.
	 */
public static File zlib(File file) throws IOException {
    if (file.isDirectory()) {
        throw new IOException("Can't zlib folder");
    }
    FileInputStream fis = new FileInputStream(file);
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    String zlibFileName = file.getAbsolutePath() + ZLIB_EXT;
    DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(zlibFileName), deflater);
    try {
        StreamUtil.copy(fis, dos);
    } finally {
        StreamUtil.close(dos);
        StreamUtil.close(fis);
    }
    return new File(zlibFileName);
}

24. DeflateCompressionCodec#doCompress()

Project: jjwt
Source File: DeflateCompressionCodec.java
View license
@Override
public byte[] doCompress(byte[] payload) throws IOException {
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    ByteArrayOutputStream outputStream = null;
    DeflaterOutputStream deflaterOutputStream = null;
    try {
        outputStream = new ByteArrayOutputStream();
        deflaterOutputStream = new DeflaterOutputStream(outputStream, deflater, true);
        deflaterOutputStream.write(payload, 0, payload.length);
        deflaterOutputStream.flush();
        return outputStream.toByteArray();
    } finally {
        Objects.nullSafeClose(outputStream, deflaterOutputStream);
    }
}

25. OldAndroidDeflateTest#bigTest()

Project: j2objc
Source File: OldAndroidDeflateTest.java
View license
/*
     * "step" determines how compressible the data is.
     *
     * Note we must set "nowrap" to false, or the Adler-32 doesn't get
     * computed.
     */
private void bigTest(int step, int expectedAdler) throws UnsupportedEncodingException, DataFormatException {
    byte[] input = new byte[128 * 1024];
    byte[] comp = new byte[128 * 1024 + 512];
    byte[] output = new byte[128 * 1024 + 512];
    Inflater inflater = new Inflater(false);
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false);
    createSample(input, step);
    compress(deflater, input, comp);
    expand(inflater, comp, (int) deflater.getBytesWritten(), output);
    assertEquals(inflater.getBytesWritten(), input.length);
    assertEquals(deflater.getAdler(), inflater.getAdler());
    assertEquals(deflater.getAdler(), expectedAdler);
}

26. DeflateCompressor#streamOutput()

Project: elassandra
Source File: DeflateCompressor.java
View license
@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
    out.writeBytes(HEADER);
    final boolean nowrap = true;
    final Deflater deflater = new Deflater(LEVEL, nowrap);
    final boolean syncFlush = true;
    OutputStream compressedOut = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    compressedOut = new BufferedOutputStream(compressedOut, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {

        private boolean closed = false;

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed == false) {
                    // important to release native memory
                    deflater.end();
                    closed = true;
                }
            }
        }
    };
}

27. CompressedArray#compress()

Project: graphhopper
Source File: CompressedArray.java
View license
/**
     * Compresses the specified byte range using the specified compressionLevel (constants are
     * defined in java.util.zip.Deflater).
     */
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) {
    /* 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(length);
    Deflater compressor = new Deflater();
    try {
        compressor.setLevel(compressionLevel);
        compressor.setInput(value, offset, length);
        compressor.finish();
        final byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
    } finally {
        compressor.end();
    }
    return bos.toByteArray();
}

28. JPEGTranscoder#deflate()

Project: flex-falcon
Source File: JPEGTranscoder.java
View license
public static byte[] deflate(byte[] buf) throws IOException {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    DAByteArrayOutputStream out = new DAByteArrayOutputStream();
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(out, deflater);
    try {
        deflaterStream.write(buf, 0, buf.length);
        deflaterStream.finish();
        deflater.end();
    } finally {
        IOUtils.closeQuietly(deflaterStream);
    }
    return out.getDirectByteArray();
}

29. Native#compress()

Project: erjang
Source File: Native.java
View license
@BIF
public static EBinary compress(EObject bin) {
    EBinary b = bin.testBinary();
    if (b == null) {
        EBitString bin2 = ErlConvert.iolist_to_binary(bin);
        b = bin2.testBinary();
        if (b == null) {
            throw ERT.badarg(bin);
        }
    }
    Deflater defl = new Deflater();
    BARR bos = new BARR();
    DeflaterOutputStream dos = new DeflaterOutputStream(bos, defl);
    try {
        b.writeTo(dos);
        dos.close();
    } catch (IOException e) {
        throw new InternalError("should not happen");
    }
    return bos.asBinary();
}

30. DeflateCompressor#streamOutput()

View license
@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
    out.writeBytes(HEADER);
    final boolean nowrap = true;
    final Deflater deflater = new Deflater(LEVEL, nowrap);
    final boolean syncFlush = true;
    OutputStream compressedOut = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    compressedOut = new BufferedOutputStream(compressedOut, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {

        final AtomicBoolean closed = new AtomicBoolean(false);

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed.compareAndSet(false, true)) {
                    // important to release native memory
                    deflater.end();
                }
            }
        }
    };
}

31. IDATChunk#getContentPayload()

View license
public byte[] getContentPayload() {
    byte[] payload = new byte[(width + 1) * height];
    for (int i = 0; i < height; i++) {
        int offset = i * (width + 1);
        //NO filter on this line
        payload[offset++] = 0;
        for (int j = 0; j < width; j++) {
            payload[offset + j] = (byte) (127);
        }
    }
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION);
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream((width + 1) * height);
    DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes, deflater);
    try {
        compBytes.write(payload);
        compBytes.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    byte[] compPayload = outBytes.toByteArray();
    return compPayload;
}

32. ZipWriterTest#setup()

Project: bazel
Source File: ZipWriterTest.java
View license
@Before
public void setup() throws IOException {
    rand = new Random();
    cal = Calendar.getInstance();
    cal.clear();
    // Zip files have 7-bit year resolution.
    cal.set(Calendar.YEAR, rand.nextInt(128) + 1980);
    cal.set(Calendar.MONTH, rand.nextInt(12));
    cal.set(Calendar.DAY_OF_MONTH, rand.nextInt(29));
    cal.set(Calendar.HOUR_OF_DAY, rand.nextInt(24));
    cal.set(Calendar.MINUTE, rand.nextInt(60));
    // Zip files have 2 second resolution.
    cal.set(Calendar.SECOND, rand.nextInt(30) * 2);
    crc = new CRC32();
    deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    test = tmp.newFile("test.zip");
}

33. AstSerializer#serialize()

Project: openjdk
Source File: AstSerializer.java
View license
static byte[] serialize(final FunctionNode fn) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Deflater deflater = new Deflater(COMPRESSION_LEVEL);
    try (final ObjectOutputStream oout = new ObjectOutputStream(new DeflaterOutputStream(out, deflater))) {
        oout.writeObject(fn);
    } catch (final IOException e) {
        throw new AssertionError("Unexpected exception serializing function", e);
    } finally {
        deflater.end();
    }
    return out.toByteArray();
}

34. CompressionUtil#compressByteArray()

Project: commons-jcs
Source File: CompressionUtil.java
View license
/**
     * Compress the byte array passed
     * <p>
     * @param input byte array
     * @param bufferLength buffer length
     * @return compressed byte array
     * @throws IOException thrown if we can't close the output stream
     */
public static byte[] compressByteArray(byte[] input, int bufferLength) throws IOException {
    // 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);
    compressor.finish();
    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    // Compress the data
    byte[] buf = new byte[bufferLength];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ )
    compressor.end();
    bos.close();
    // Get the compressed data
    return bos.toByteArray();
}

35. DeflateCompressor#compress()

View license
public int compress(byte[] input, int inputOffset, int inputLength, ICompressor.WrappedArray output, int outputOffset) {
    Deflater def = deflater.get();
    def.reset();
    def.setInput(input, inputOffset, inputLength);
    def.finish();
    if (def.needsInput())
        return 0;
    int offs = outputOffset;
    while (true) {
        offs += def.deflate(output.buffer, offs, output.buffer.length - offs);
        if (def.finished()) {
            return offs - outputOffset;
        } else {
            // We're not done, output was too small. Increase it and continue
            byte[] newBuffer = new byte[(output.buffer.length * 4) / 3 + 1];
            System.arraycopy(output.buffer, 0, newBuffer, 0, offs);
            output.buffer = newBuffer;
        }
    }
}

36. ComponentResource#compress()

Project: idecore
Source File: ComponentResource.java
View license
protected static final byte[] compress(byte[] input) {
    // 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);
    compressor.finish();
    // the uncompressed data.
    try (final QuietCloseable<ByteArrayOutputStream> c = QuietCloseable.make(new ByteArrayOutputStream(input.length))) {
        final ByteArrayOutputStream bos = c.get();
        // Compress the data
        byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        // Get the compressed data
        return bos.toByteArray();
    }
}

37. Gzipper#zip()

Project: CrossBow
Source File: Gzipper.java
View license
public static byte[] zip(byte[] data) {
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length + 1);
    //write a placeholder byte for the flag
    baos.write(FLAG_UNCOMPRESSED);
    byte[] buf = new byte[1024];
    while (!deflater.finished()) {
        int byteCount = deflater.deflate(buf);
        baos.write(buf, 0, byteCount);
    }
    deflater.end();
    byte[] compressed = baos.toByteArray();
    if (compressed.length < data.length) {
        //compressed is smaller, change the front flag
        compressed[0] = FLAG_COMPRESSED;
        return compressed;
    } else {
        //compression was no help add flag to data and return
        byte[] finalData = new byte[data.length + 1];
        finalData[0] = FLAG_COMPRESSED;
        System.arraycopy(data, 0, finalData, 1, data.length);
        return finalData;
    }
}

38. DeflateCompressor#compressBuffer()

Project: cassandra
Source File: DeflateCompressor.java
View license
public void compressBuffer(ByteBuffer input, ByteBuffer output) {
    Deflater def = deflater.get();
    def.reset();
    byte[] buffer = FBUtilities.getThreadLocalScratchBuffer();
    // Use half the buffer for input, half for output.
    int chunkLen = buffer.length / 2;
    while (input.remaining() > chunkLen) {
        input.get(buffer, 0, chunkLen);
        def.setInput(buffer, 0, chunkLen);
        while (!def.needsInput()) {
            int len = def.deflate(buffer, chunkLen, chunkLen);
            output.put(buffer, chunkLen, len);
        }
    }
    int inputLength = input.remaining();
    input.get(buffer, 0, inputLength);
    def.setInput(buffer, 0, inputLength);
    def.finish();
    while (!def.finished()) {
        int len = def.deflate(buffer, chunkLen, chunkLen);
        output.put(buffer, chunkLen, len);
    }
}

39. AbstractHistogram#encodeIntoCompressedByteBuffer()

Project: voltdb
Source File: AbstractHistogram.java
View license
/**
     * Encode this histogram in compressed form into a byte array
     * @param targetBuffer The buffer to encode into
     * @param compressionLevel Compression level (for java.util.zip.Deflater).
     * @return The number of bytes written to the buffer
     */
@Override
public synchronized int encodeIntoCompressedByteBuffer(final ByteBuffer targetBuffer, final int compressionLevel) {
    int neededCapacity = getNeededByteBufferCapacity(countsArrayLength);
    if (intermediateUncompressedByteBuffer == null || intermediateUncompressedByteBuffer.capacity() < neededCapacity) {
        intermediateUncompressedByteBuffer = ByteBuffer.allocate(neededCapacity).order(BIG_ENDIAN);
    }
    intermediateUncompressedByteBuffer.clear();
    int initialTargetPosition = targetBuffer.position();
    final int uncompressedLength = encodeIntoByteBuffer(intermediateUncompressedByteBuffer);
    targetBuffer.putInt(getCompressedEncodingCookie());
    // Placeholder for compressed contents length
    targetBuffer.putInt(0);
    Deflater compressor = new Deflater(compressionLevel);
    compressor.setInput(intermediateUncompressedByteBuffer.array(), 0, uncompressedLength);
    compressor.finish();
    byte[] targetArray;
    if (targetBuffer.hasArray()) {
        targetArray = targetBuffer.array();
    } else {
        if (intermediateUncompressedByteArray == null || intermediateUncompressedByteArray.length < targetBuffer.capacity()) {
            intermediateUncompressedByteArray = new byte[targetBuffer.capacity()];
        }
        targetArray = intermediateUncompressedByteArray;
    }
    int compressedTargetOffset = initialTargetPosition + 8;
    int compressedDataLength = compressor.deflate(targetArray, compressedTargetOffset, targetArray.length - compressedTargetOffset);
    compressor.end();
    if (!targetBuffer.hasArray()) {
        targetBuffer.put(targetArray, compressedTargetOffset, compressedDataLength);
    }
    // Record the compressed length
    targetBuffer.putInt(initialTargetPosition + 4, compressedDataLength);
    int bytesWritten = compressedDataLength + 8;
    targetBuffer.position(initialTargetPosition + bytesWritten);
    return bytesWritten;
}

40. CompressionUtilTest#testDeflaterReader2()

View license
@Test
public void testDeflaterReader2() throws Exception {
    String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
    byte[] input = inputString.getBytes(StandardCharsets.UTF_8);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    AtomicLong counter = new AtomicLong(0);
    byte[] buffer = new byte[7];
    ArrayList<Integer> zipHolder = new ArrayList<>();
    try (DeflaterReader reader = new DeflaterReader(inputStream, counter)) {
        int n = reader.read(buffer);
        while (n != -1) {
            for (int i = 0; i < n; i++) {
                zipHolder.add((int) buffer[i]);
            }
            n = reader.read(buffer);
        }
    }
    assertEquals(input.length, counter.get());
    byte[] allCompressed = new byte[zipHolder.size()];
    for (int i = 0; i < allCompressed.length; i++) {
        allCompressed[i] = (byte) zipHolder.get(i).intValue();
    }
    byte[] output = new byte[30];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compareByteArray(allCompressed, output, compressedDataLength);
}

41. CompressUtil#compress()

Project: Mycat-Server
Source File: CompressUtil.java
View license
/**
	 * ???mysql???????zlib ??
	 *  
	 * @param data
	 * @return
	 */
public static byte[] compress(byte[] data) {
    byte[] output = null;
    Deflater compresser = new Deflater();
    compresser.setInput(data);
    compresser.finish();
    ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    byte[] result = new byte[1024];
    try {
        while (!compresser.finished()) {
            int length = compresser.deflate(result);
            out.write(result, 0, length);
        }
        output = out.toByteArray();
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
        compresser.end();
    }
    return output;
}

42. OldAndroidDeflateTest#simpleTest()

Project: j2objc
Source File: OldAndroidDeflateTest.java
View license
/*
     * Simple inflate/deflate test, taken from the reference docs for the
     * Inflater/Deflater classes.
     */
private void simpleTest() throws UnsupportedEncodingException, DataFormatException {
    // Encode a String into bytes
    String inputString = "blahblahblah??";
    byte[] input = inputString.getBytes("UTF-8");
    // Compress the bytes
    byte[] output = new byte[100];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output, 0, compressedDataLength);
    byte[] result = new byte[100];
    int resultLength = decompresser.inflate(result);
    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");
    assertEquals(inputString, outputString);
    assertEquals(compresser.getAdler(), decompresser.getAdler());
    decompresser.end();
}

43. CompressionUtilTest#testDeflaterReader()

View license
@Test
public void testDeflaterReader() throws Exception {
    String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
    byte[] input = inputString.getBytes(StandardCharsets.UTF_8);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    AtomicLong counter = new AtomicLong(0);
    ArrayList<Integer> zipHolder = new ArrayList<>();
    try (DeflaterReader reader = new DeflaterReader(inputStream, counter)) {
        int b = reader.read();
        while (b != -1) {
            zipHolder.add(b);
            b = reader.read();
        }
    }
    assertEquals(input.length, counter.get());
    byte[] allCompressed = new byte[zipHolder.size()];
    for (int i = 0; i < allCompressed.length; i++) {
        allCompressed[i] = (byte) zipHolder.get(i).intValue();
    }
    byte[] output = new byte[30];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compareByteArray(allCompressed, output, compressedDataLength);
}

44. CompressionUtilTest#testInflaterWriter()

View license
@Test
public void testInflaterWriter() throws Exception {
    String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
    byte[] input = inputString.getBytes(StandardCharsets.UTF_8);
    byte[] output = new byte[30];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    byte[] zipBytes = new byte[compressedDataLength];
    System.arraycopy(output, 0, zipBytes, 0, compressedDataLength);
    ByteArrayInputStream byteInput = new ByteArrayInputStream(zipBytes);
    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
    byte[] zipBuffer = new byte[12];
    try (InflaterWriter writer = new InflaterWriter(byteOutput)) {
        int n = byteInput.read(zipBuffer);
        while (n > 0) {
            writer.write(zipBuffer, 0, n);
            n = byteInput.read(zipBuffer);
        }
    }
    byte[] outcome = byteOutput.toByteArray();
    String outStr = new String(outcome);
    assertEquals(inputString, outStr);
}

45. CompressionUtilTest#testInflaterReader()

View license
@Test
public void testInflaterReader() throws Exception {
    String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
    byte[] input = inputString.getBytes(StandardCharsets.UTF_8);
    byte[] output = new byte[30];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    byte[] zipBytes = new byte[compressedDataLength];
    System.arraycopy(output, 0, zipBytes, 0, compressedDataLength);
    ByteArrayInputStream byteInput = new ByteArrayInputStream(zipBytes);
    ArrayList<Integer> holder = new ArrayList<>();
    try (InflaterReader inflater = new InflaterReader(byteInput)) {
        int read = inflater.read();
        while (read != -1) {
            holder.add(read);
            read = inflater.read();
        }
    }
    byte[] result = new byte[holder.size()];
    for (int i = 0; i < result.length; i++) {
        result[i] = holder.get(i).byteValue();
    }
    String txt = new String(result);
    assertEquals(inputString, txt);
}

46. ArchiveTool#compress()

Project: ThriftyPaxos
Source File: ArchiveTool.java
View license
private static void compress(String fromDir, String toFile) throws IOException {
    final long start = System.currentTimeMillis();
    final AtomicBoolean title = new AtomicBoolean();
    long size = getSize(new File(fromDir), new Runnable() {

        int count;

        long lastTime = start;

        @Override
        public void run() {
            count++;
            if (count % 1000 == 0) {
                long now = System.currentTimeMillis();
                if (now - lastTime > 3000) {
                    if (!title.getAndSet(true)) {
                        System.out.println("Counting files");
                    }
                    System.out.print(count + " ");
                    lastTime = now;
                }
            }
        }
    });
    if (title.get()) {
        System.out.println();
    }
    System.out.println("Compressing " + size / MB + " MB");
    InputStream in = getDirectoryInputStream(fromDir);
    String temp = toFile + ".temp";
    OutputStream out = new BufferedOutputStream(new FileOutputStream(toFile), 1024 * 1024);
    Deflater def = new Deflater();
    def.setLevel(Deflater.BEST_SPEED);
    out = new BufferedOutputStream(new DeflaterOutputStream(out, def), 1024 * 1024);
    sort(in, out, temp, size);
    in.close();
    out.close();
    System.out.println();
    System.out.println("Compressed to " + new File(toFile).length() / MB + " MB in " + (System.currentTimeMillis() - start) / 1000 + " seconds");
    System.out.println();
}

47. BinaryCasSerDes6#collectAndZip()

Project: uima-uimaj
Source File: BinaryCasSerDes6.java
View license
/**
   * Method:
   *   write with deflation into a single byte array stream
   *     skip if not worth deflating
   *     skip the Slot_Control stream
   *     record in the Slot_Control stream, for each deflated stream:
   *       the Slot index
   *       the number of compressed bytes
   *       the number of uncompressed bytes
   *   add to header:  
   *     nbr of compressed entries
   *     the Slot_Control stream size
   *     the Slot_Control stream
   *     all the zipped streams
   *
   * @throws IOException passthru
   */
private void collectAndZip() throws IOException {
    ByteArrayOutputStream baosZipped = new ByteArrayOutputStream(4096);
    Deflater deflater = new Deflater(compressLevel.lvl, true);
    deflater.setStrategy(compressStrategy.strat);
    int nbrEntries = 0;
    List<Integer> idxAndLen = new ArrayList<Integer>();
    for (int i = 0; i < baosZipSources.length; i++) {
        ByteArrayOutputStream baos = baosZipSources[i];
        if (baos != null) {
            nbrEntries++;
            dosZipSources[i].close();
            long startTime = System.currentTimeMillis();
            int zipBufSize = Math.max(1024, baos.size() / 100);
            deflater.reset();
            DeflaterOutputStream cds = new DeflaterOutputStream(baosZipped, deflater, zipBufSize);
            baos.writeTo(cds);
            cds.close();
            idxAndLen.add(i);
            if (doMeasurements) {
                idxAndLen.add((int) (sm.statDetails[i].afterZip = deflater.getBytesWritten()));
                idxAndLen.add((int) (sm.statDetails[i].beforeZip = deflater.getBytesRead()));
                sm.statDetails[i].zipTime = System.currentTimeMillis() - startTime;
            } else {
                idxAndLen.add((int) deflater.getBytesWritten());
                idxAndLen.add((int) deflater.getBytesRead());
            }
        }
    }
    // write number of entries
    serializedOut.writeInt(nbrEntries);
    for (int i = 0; i < idxAndLen.size(); ) {
        serializedOut.write(idxAndLen.get(i++));
        serializedOut.writeInt(idxAndLen.get(i++));
        serializedOut.writeInt(idxAndLen.get(i++));
    }
    // write Compressed info
    baosZipped.writeTo(serializedOut);
}

48. DeflaterOutputStreamTest#testSyncFlushDeflater()

View license
/**
     * Confirm that a DeflaterOutputStream constructed with Deflater
     * with flushParm == SYNC_FLUSH does not need to to be flushed.
     *
     * http://b/4005091
     */
public void testSyncFlushDeflater() throws Exception {
    Deflater def = new Deflater();
    Field f = def.getClass().getDeclaredField("flushParm");
    f.setAccessible(true);
    f.setInt(def, Deflater.SYNC_FLUSH);
    final int deflaterBufferSize = 512;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, def, deflaterBufferSize);
    // make output buffer large enough that even if compressed it
    // won't all fit within the deflaterBufferSize.
    final int outputBufferSize = 128 * deflaterBufferSize;
    byte[] output = new byte[outputBufferSize];
    for (int i = 0; i < output.length; i++) {
        output[i] = (byte) i;
    }
    dos.write(output);
    byte[] compressed = baos.toByteArray();
    // this main reason for this assert is to make sure that the
    // compressed byte count is larger than the
    // deflaterBufferSize. However, when the original bug exists,
    // it will also fail because the compressed length will be
    // exactly the length of the deflaterBufferSize.
    assertTrue("compressed=" + compressed.length + " but deflaterBufferSize=" + deflaterBufferSize, compressed.length > deflaterBufferSize);
    // assert that we returned data matches the input exactly.
    ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
    InflaterInputStream iis = new InflaterInputStream(bais);
    byte[] input = new byte[output.length];
    int total = 0;
    while (true) {
        int n = iis.read(input, total, input.length - total);
        if (n == -1) {
            break;
        }
        total += n;
        if (total == input.length) {
            try {
                iis.read();
                fail();
            } catch (EOFException expected) {
                break;
            }
        }
    }
    assertEquals(output.length, total);
    assertTrue(Arrays.equals(input, output));
    // ensure Deflater.finish has not been called at any point
    // during the test, since that would lead to the results being
    // flushed even without SYNC_FLUSH being used
    assertFalse(def.finished());
    // Quieten CloseGuard.
    def.end();
    iis.close();
}

49. DeflaterSinkTest#multipleSegmentsWithoutCompression()

Project: okio
Source File: DeflaterSinkTest.java
View license
@Test
public void multipleSegmentsWithoutCompression() throws IOException {
    Buffer buffer = new Buffer();
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.NO_COMPRESSION);
    DeflaterSink deflaterSink = new DeflaterSink(buffer, deflater);
    int byteCount = Segment.SIZE * 4;
    deflaterSink.write(new Buffer().writeUtf8(repeat('a', byteCount)), byteCount);
    deflaterSink.close();
    assertEquals(repeat('a', byteCount), inflate(buffer).readUtf8(byteCount));
}

50. Dictionary#save()

Project: freeplane
Source File: Dictionary.java
View license
/**
	 * Save this dictionary to the OutputStream. The data will be compressed. After finish the OutputStream is closed.
	 * @param stream the OutputStream
	 * @throws IOException if an I/O error occurs.
	 */
public void save(final OutputStream stream) throws IOException {
    final Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);
    final DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
    for (int i = 0; i < size; i++) {
        zip.write(tree[i]);
        zip.write(tree[i] >> 8);
    }
    zip.flush();
    zip.close();
}

51. PigSplit#write()

Project: pig
Source File: PigSplit.java
View license
@Override
@SuppressWarnings("unchecked")
public void write(DataOutput os) throws IOException {
    os.writeBoolean(disableCounter);
    os.writeBoolean(isMultiInputs);
    os.writeInt(totalSplits);
    os.writeInt(splitIndex);
    os.writeInt(inputIndex);
    writeObject(targetOps, os);
    os.writeInt(wrappedSplits.length);
    Set<String> splitClassNameSet = new HashSet<String>();
    //first get the distinct split class name set
    for (int i = 0; i < wrappedSplits.length; i++) {
        splitClassNameSet.add(wrappedSplits[i].getClass().getName());
    }
    List<String> distinctSplitClassList = new ArrayList<String>();
    distinctSplitClassList.addAll(splitClassNameSet);
    boolean nonFileSplit = distinctSplitClassList.size() > 1 || (!distinctSplitClassList.contains(FILESPLIT_CLASSNAME));
    //write the distinct number of split class name
    os.writeInt(distinctSplitClassList.size());
    //write each classname once
    for (int i = 0; i < distinctSplitClassList.size(); i++) {
        os.writeUTF(distinctSplitClassList.get(i));
    }
    SerializationFactory sf = new SerializationFactory(conf);
    if (wrappedSplits.length <= 0) {
        return;
    }
    boolean compress = nonFileSplit && conf.getBoolean(PigConfiguration.PIG_COMPRESS_INPUT_SPLITS, PigConfiguration.PIG_COMPRESS_INPUT_SPLITS_DEFAULT);
    WritableByteArray byteStream = null;
    Deflater deflater = null;
    DataOutputStream dos = null;
    if (compress) {
        byteStream = new WritableByteArray(16384);
        deflater = new Deflater(Deflater.BEST_COMPRESSION);
        dos = new DataOutputStream(new DeflaterOutputStream(byteStream, deflater));
    }
    DataOutput dataOut = compress ? dos : os;
    for (int i = 0; i < wrappedSplits.length; i++) {
        //find out the index of the split class name
        int index = distinctSplitClassList.indexOf(wrappedSplits[i].getClass().getName());
        dataOut.writeInt(index);
        Serializer s = sf.getSerializer(wrappedSplits[i].getClass());
        //Checks if Serializer is NULL or not before calling open() method on it.
        if (s == null) {
            throw new IllegalArgumentException("Could not find Serializer for class " + wrappedSplits[i].getClass() + ". InputSplits must implement Writable.");
        }
        s.open((OutputStream) dataOut);
        // The correct call sequence for Serializer is, we shall open, then serialize, but we shall not close
        s.serialize(wrappedSplits[i]);
    }
    if (compress) {
        //Get the compressed serialized bytes and write them
        dos.close();
        os.writeInt(byteStream.getLength());
        os.write(byteStream.getData(), 0, byteStream.getLength());
        deflater.end();
    }
}

52. CompressionZlib#init()

Project: mina-sshd
Source File: CompressionZlib.java
View license
@Override
public void init(Type type, int level) {
    compresser = new Deflater(level);
    decompresser = new Inflater();
}

53. DeflateUtil#encode()

Project: keycloak
Source File: DeflateUtil.java
View license
/**
     * Apply DEFLATE encoding
     *
     * @param message
     *
     * @return
     *
     * @throws IOException
     */
public static byte[] encode(byte[] message) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(baos, deflater);
    deflaterStream.write(message);
    deflaterStream.finish();
    return baos.toByteArray();
}

54. SevenZNativeHeapTest#testEndDeflaterOnCloseStream()

View license
@Test
public void testEndDeflaterOnCloseStream() throws Exception {
    final Deflater deflater = PowerMockito.spy(new Deflater());
    PowerMockito.whenNew(Deflater.class).withAnyArguments().thenReturn(deflater);
    final OutputStream outputStream = deflateDecoder.encode(new ByteArrayOutputStream(), 9);
    outputStream.close();
    Mockito.verify(deflater).end();
}

55. StreamCompressor#create()

View license
/**
     * Create a stream compressor with the given compression level.
     *
     * @param compressionLevel The {@link Deflater}  compression level
     * @param bs               The ScatterGatherBackingStore to receive output
     * @return A stream compressor
     */
public static StreamCompressor create(final int compressionLevel, final ScatterGatherBackingStore bs) {
    final Deflater deflater = new Deflater(compressionLevel, true);
    return new ScatterGatherBackingStoreCompressor(deflater, bs);
}

56. CAFS#write()

Project: bnd
Source File: CAFS.java
View license
/**
	 * Store an input stream in the CAFS while calculating and returning the
	 * SHA-1 code.
	 * 
	 * @param in The input stream to store.
	 * @return The SHA-1 code.
	 * @throws Exception if anything goes wrong
	 */
public SHA1 write(InputStream in) throws Exception {
    Deflater deflater = new Deflater();
    MessageDigest md = MessageDigest.getInstance(ALGORITHM);
    DigestInputStream din = new DigestInputStream(in, md);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DeflaterOutputStream dout = new DeflaterOutputStream(bout, deflater);
    copy(din, dout);
    synchronized (store) {
        // First check if it already exists
        SHA1 sha1 = new SHA1(md.digest());
        long search = index.search(sha1.digest());
        if (search > 0)
            return sha1;
        byte[] compressed = bout.toByteArray();
        // we need to append this file to our store,
        // which requires a lock. However, we are in a race
        // so others can get the lock between us getting
        // the length and someone else getting the lock.
        // So we must verify after we get the lock that the
        // length was unchanged.
        FileLock lock = null;
        try {
            long insertPoint;
            int recordLength = compressed.length + HEADERLENGTH;
            while (true) {
                insertPoint = store.length();
                lock = channel.lock(insertPoint, recordLength, false);
                if (store.length() == insertPoint)
                    break;
                // We got the wrong lock, someone else
                // got in between reading the length
                // and locking
                lock.release();
            }
            int totalLength = deflater.getTotalIn();
            store.seek(insertPoint);
            update(sha1.digest(), compressed, totalLength);
            index.insert(sha1.digest(), insertPoint);
            return sha1;
        } finally {
            if (lock != null)
                lock.release();
        }
    }
}

57. ZipReaderTest#testRawFileData()

Project: bazel
Source File: ZipReaderTest.java
View license
@Test
public void testRawFileData() throws IOException {
    CRC32 crc = new CRC32();
    Deflater deflator = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
        ZipEntry foo = new ZipEntry("foo");
        foo.setComment("foo comment.");
        foo.setMethod(ZipEntry.DEFLATED);
        zout.putNextEntry(foo);
        zout.write("foo".getBytes(UTF_8));
        zout.closeEntry();
        ZipEntry bar = new ZipEntry("bar");
        bar.setComment("bar comment.");
        bar.setMethod(ZipEntry.STORED);
        bar.setSize("bar".length());
        bar.setCompressedSize("bar".length());
        crc.reset();
        crc.update("bar".getBytes(UTF_8));
        bar.setCrc(crc.getValue());
        zout.putNextEntry(bar);
        zout.write("bar".getBytes(UTF_8));
        zout.closeEntry();
    }
    try (ZipReader reader = new ZipReader(test, UTF_8)) {
        ZipFileEntry fooEntry = reader.getEntry("foo");
        InputStream fooIn = reader.getRawInputStream(fooEntry);
        byte[] fooData = new byte[10];
        fooIn.read(fooData);
        byte[] expectedFooData = new byte[10];
        deflator.reset();
        deflator.setInput("foo".getBytes(UTF_8));
        deflator.finish();
        deflator.deflate(expectedFooData);
        assertThat(fooData).isEqualTo(expectedFooData);
        ZipFileEntry barEntry = reader.getEntry("bar");
        InputStream barIn = reader.getRawInputStream(barEntry);
        byte[] barData = new byte[3];
        barIn.read(barData);
        byte[] expectedBarData = "bar".getBytes(UTF_8);
        assertThat(barData).isEqualTo(expectedBarData);
        assertThat(barIn.read()).isEqualTo(-1);
        assertThat(barIn.read(barData)).isEqualTo(-1);
        assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);
        thrown.expect(IOException.class);
        thrown.expectMessage("Reset is not supported on this type of stream.");
        barIn.reset();
    }
}

58. CompressionUtilsTest#setup()

Project: undertow
Source File: CompressionUtilsTest.java
View license
@Before
public void setup() throws Exception {
    compress = new Deflater(Deflater.BEST_SPEED, true);
    decompress = new Inflater(true);
}

59. ZipReaderTest#testZipEntryFields()

Project: bazel
Source File: ZipReaderTest.java
View license
@Test
public void testZipEntryFields() throws IOException {
    CRC32 crc = new CRC32();
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    // 2/3/1995 04:05:06
    long date = 791784306000L;
    byte[] extra = new ExtraData((short) 0xaa, new byte[] { (byte) 0xbb, (byte) 0xcd }).getBytes();
    byte[] tmp = new byte[128];
    try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
        ZipEntry foo = new ZipEntry("foo");
        foo.setComment("foo comment.");
        foo.setMethod(ZipEntry.DEFLATED);
        foo.setTime(date);
        foo.setExtra(extra);
        zout.putNextEntry(foo);
        zout.write("foo".getBytes(UTF_8));
        zout.closeEntry();
        ZipEntry bar = new ZipEntry("bar");
        bar.setComment("bar comment.");
        bar.setMethod(ZipEntry.STORED);
        bar.setSize("bar".length());
        bar.setCompressedSize("bar".length());
        crc.reset();
        crc.update("bar".getBytes(UTF_8));
        bar.setCrc(crc.getValue());
        zout.putNextEntry(bar);
        zout.write("bar".getBytes(UTF_8));
        zout.closeEntry();
    }
    try (ZipReader reader = new ZipReader(test, UTF_8)) {
        ZipFileEntry fooEntry = reader.getEntry("foo");
        assertThat(fooEntry.getName()).isEqualTo("foo");
        assertThat(fooEntry.getComment()).isEqualTo("foo comment.");
        assertThat(fooEntry.getMethod()).isEqualTo(Compression.DEFLATED);
        assertThat(fooEntry.getVersion()).isEqualTo(Compression.DEFLATED.getMinVersion());
        assertThat(fooEntry.getTime()).isEqualTo(date);
        assertThat(fooEntry.getSize()).isEqualTo("foo".length());
        deflater.reset();
        deflater.setInput("foo".getBytes(UTF_8));
        deflater.finish();
        assertThat(fooEntry.getCompressedSize()).isEqualTo(deflater.deflate(tmp));
        crc.reset();
        crc.update("foo".getBytes(UTF_8));
        assertThat(fooEntry.getCrc()).isEqualTo(crc.getValue());
        assertThat(fooEntry.getExtra().getBytes()).isEqualTo(extra);
        ZipFileEntry barEntry = reader.getEntry("bar");
        assertThat(barEntry.getName()).isEqualTo("bar");
        assertThat(barEntry.getComment()).isEqualTo("bar comment.");
        assertThat(barEntry.getMethod()).isEqualTo(Compression.STORED);
        assertThat(barEntry.getVersion()).isEqualTo(Compression.STORED.getMinVersion());
        assertDateAboutNow(new Date(barEntry.getTime()));
        assertThat(barEntry.getSize()).isEqualTo("bar".length());
        assertThat(barEntry.getCompressedSize()).isEqualTo("bar".length());
        crc.reset();
        crc.update("bar".getBytes(UTF_8));
        assertThat(barEntry.getCrc()).isEqualTo(crc.getValue());
        assertThat(barEntry.getExtra().getBytes()).isEqualTo(new byte[] {});
    }
}

60. Compresser#deflate()

Project: zstack
Source File: Compresser.java
View license
public static byte[] deflate(byte[] input, int bufferSize) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(input.length);
    Deflater def = new Deflater();
    DeflaterOutputStream dos = new DeflaterOutputStream(out, def, bufferSize);
    dos.write(input, 0, input.length);
    dos.finish();
    dos.close();
    byte[] ret = out.toByteArray();
    out.close();
    return ret;
}