Search in sources :

Example 56 with Deflater

use of java.util.zip.Deflater in project okio by square.

the class DeflaterSinkTest method closeWithExceptionWhenWritingAndClosing.

/**
   * 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()");
}
Also used : Deflater(java.util.zip.Deflater) IOException(java.io.IOException) Test(org.junit.Test)

Example 57 with Deflater

use of java.util.zip.Deflater in project bilibili-android-client by HotBitmapGG.

the class BiliDanmukuCompressionTools method compress.

public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
    Deflater compressor = new Deflater();
    try {
        // 将当前压缩级别设置为指定值。
        compressor.setLevel(compressionLevel);
        compressor.setInput(value, offset, length);
        // 调用时,指示压缩应当以输入缓冲区的当前内容结尾。
        compressor.finish();
        // Compress the data
        final byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            // 如果已到达压缩数据输出流的结尾,则返回 true。
            int count = compressor.deflate(buf);
            // 使用压缩数据填充指定缓冲区。
            bos.write(buf, 0, count);
        }
    } finally {
        // 关闭解压缩器并放弃所有未处理的输入。
        compressor.end();
    }
    return bos.toByteArray();
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 58 with Deflater

use of java.util.zip.Deflater in project jodd by oblac.

the class ZipUtil method zlib.

/**
	 * 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);
}
Also used : Deflater(java.util.zip.Deflater) FileOutputStream(java.io.FileOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) FileInputStream(java.io.FileInputStream)

Example 59 with Deflater

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

the class OldAndroidZipStressTest method testZipDeflateInflateStress.

/**
     * Native memory allocated by Deflater in system_server. The fix reduced
     * some internal ZLIB buffers in size, so this test is trying to execute a
     * lot of deflating to ensure that things are still working properly.
     * http://b/1185084
     */
public void testZipDeflateInflateStress() throws Exception {
    final int DATA_SIZE = 16384;
    // Seed makes test reproducible
    Random random = new Random(42);
    // Outer loop selects "mode" of test.
    for (int j = 1; j <= 2; j++) {
        byte[] input = new byte[DATA_SIZE];
        if (j == 1) {
            // Totally random content
            random.nextBytes(input);
        } else {
            // Random contents with longer repetitions
            int pos = 0;
            while (pos < input.length) {
                byte what = (byte) random.nextInt(256);
                int howMany = random.nextInt(32);
                if (pos + howMany >= input.length) {
                    howMany = input.length - pos;
                }
                Arrays.fill(input, pos, pos + howMany, what);
                pos += howMany;
            }
        }
        // Inner loop tries all 9 compression levels.
        for (int i = 1; i <= 9; i++) {
            System.out.println("ZipDeflateInflateStress test (" + j + "," + i + ")...");
            // Just to make sure...
            byte[] zipped = new byte[2 * DATA_SIZE];
            Deflater deflater = new Deflater(i);
            deflater.setInput(input);
            deflater.finish();
            deflater.deflate(zipped);
            deflater.end();
            byte[] output = new byte[DATA_SIZE];
            Inflater inflater = new Inflater();
            inflater.setInput(zipped);
            inflater.finished();
            inflater.inflate(output);
            inflater.end();
            assertEquals(input, output);
        }
    }
}
Also used : Random(java.util.Random) Deflater(java.util.zip.Deflater) Inflater(java.util.zip.Inflater)

Example 60 with Deflater

use of java.util.zip.Deflater in project Dragonet-Legacy by DragonetMC.

the class BatchPacket method encode.

@Override
public void encode() {
    try {
        //We don't waste our memory
        setShouldSendImmidate(true);
        //Combine all packets
        ByteArrayOutputStream packetCombinerData = new ByteArrayOutputStream();
        PEBinaryWriter packetCombiner = new PEBinaryWriter(packetCombinerData);
        for (PEPacket pk : packets) {
            pk.encode();
            packetCombiner.writeInt(pk.getData().length);
            packetCombiner.write(pk.getData());
        }
        Deflater def = new Deflater(7);
        def.reset();
        def.setInput(packetCombinerData.toByteArray());
        def.finish();
        byte[] deflateBuffer = new byte[65535];
        int size = def.deflate(deflateBuffer);
        deflateBuffer = Arrays.copyOfRange(deflateBuffer, 0, size);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(bos);
        writer.writeByte((byte) (this.pid() & 0xFF));
        writer.writeInt(deflateBuffer.length);
        writer.write(deflateBuffer);
        this.setData(bos.toByteArray());
    } catch (IOException e) {
    }
}
Also used : PEBinaryWriter(org.dragonet.utilities.io.PEBinaryWriter) Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

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