Search in sources :

Example 21 with Deflater

use of java.util.zip.Deflater in project intellij-community by JetBrains.

the class ConnectionStreams method setGzipped.

// Actions ================================================================
// Actions ================================================================
public void setGzipped() throws IOException {
    loggedWriter.flush();
    loggedOutputStream.flush();
    deflaterOutputStream = new DeflaterOutputStream(connection.getOutputStream(), new Deflater(6));
    setOutputStream(deflaterOutputStream);
    setInputStream(new InflaterInputStream(connection.getInputStream()));
}
Also used : Deflater(java.util.zip.Deflater) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 22 with Deflater

use of java.util.zip.Deflater in project teaTime by ancfdy.

the class AppZLibMgr method compress.

/**
     * 压缩
     *
     * @param data
     *
     * @return byte[] 压缩后的数据
     */
public static byte[] compress(byte[] data) {
    byte[] output = new byte[0];
    Deflater compresser = new Deflater();
    compresser.reset();
    compresser.setInput(data);
    compresser.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!compresser.finished()) {
            int i = compresser.deflate(buf);
            bos.write(buf, 0, i);
        }
        output = bos.toByteArray();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    } finally {
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    compresser.end();
    return output;
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 23 with Deflater

use of java.util.zip.Deflater in project eiger by wlloyd.

the class DeflateCompressor method compress.

public int compress(byte[] input, int inputOffset, int inputLength, ICompressor.WrappedArray output, int outputOffset) throws IOException {
    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;
        }
    }
}
Also used : Deflater(java.util.zip.Deflater)

Example 24 with Deflater

use of java.util.zip.Deflater in project robovm by robovm.

the class DeflaterOutputStreamTest method testSyncFlushDeflater.

/**
     * 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();
}
Also used : Field(java.lang.reflect.Field) Deflater(java.util.zip.Deflater) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) EOFException(java.io.EOFException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 25 with Deflater

use of java.util.zip.Deflater in project robovm by robovm.

the class InflaterTest method deflate.

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();
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Deflater (java.util.zip.Deflater)85 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)25 IOException (java.io.IOException)18 Test (org.junit.Test)13 Inflater (java.util.zip.Inflater)9 OutputStream (java.io.OutputStream)7 InflaterInputStream (java.util.zip.InflaterInputStream)7 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 Field (java.lang.reflect.Field)3 CRC32 (java.util.zip.CRC32)3 BufferedOutputStream (java.io.BufferedOutputStream)2 CharArrayReader (java.io.CharArrayReader)2 DataOutputStream (java.io.DataOutputStream)2 FileInputStream (java.io.FileInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Reader (java.io.Reader)2