Search in sources :

Example 46 with Deflater

use of java.util.zip.Deflater in project rest.li by linkedin.

the class DeflateCompressor method deflate.

@Override
public byte[] deflate(InputStream data) throws CompressionException {
    byte[] input;
    try {
        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();
}
Also used : Deflater(java.util.zip.Deflater) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 47 with Deflater

use of java.util.zip.Deflater in project bazel by bazelbuild.

the class ProfilerTest method testUnsupportedProfilerRecord.

@Test
public void testUnsupportedProfilerRecord() throws Exception {
    Path dataFile = cacheDir.getRelative("profile5.dat");
    profiler.start(ProfiledTaskKinds.ALL, dataFile.getOutputStream(), "phase test", false, BlazeClock.instance(), BlazeClock.instance().nanoTime());
    profiler.startTask(ProfilerTask.TEST, "outer task");
    profiler.logEvent(ProfilerTask.EXCEPTION, "inner task");
    profiler.completeTask(ProfilerTask.TEST);
    profiler.startTask(ProfilerTask.SCANNER, "outer task 2");
    profiler.logSimpleTask(Profiler.nanoTimeMaybe(), ProfilerTask.TEST, "inner task 2");
    profiler.completeTask(ProfilerTask.SCANNER);
    profiler.stop();
    // Validate our test profile.
    ProfileInfo info = ProfileInfo.loadProfile(dataFile);
    info.calculateStats();
    assertFalse(info.isCorruptedOrIncomplete());
    assertEquals(2, info.getStatsForType(ProfilerTask.TEST, info.rootTasksById).count);
    assertEquals(0, info.getStatsForType(ProfilerTask.UNKNOWN, info.rootTasksById).count);
    // Now replace "TEST" type with something unsupported - e.g. "XXXX".
    InputStream in = new InflaterInputStream(dataFile.getInputStream(), new Inflater(false), 65536);
    byte[] buffer = new byte[60000];
    int len = in.read(buffer);
    in.close();
    // Validate that file was completely decoded.
    assertTrue(len < buffer.length);
    String content = new String(buffer, ISO_8859_1);
    int infoIndex = content.indexOf("TEST");
    assertTrue(infoIndex > 0);
    content = content.substring(0, infoIndex) + "XXXX" + content.substring(infoIndex + 4);
    OutputStream out = new DeflaterOutputStream(dataFile.getOutputStream(), new Deflater(Deflater.BEST_SPEED, false), 65536);
    out.write(content.getBytes(ISO_8859_1));
    out.close();
    // Validate that XXXX records were classified as UNKNOWN.
    info = ProfileInfo.loadProfile(dataFile);
    info.calculateStats();
    assertFalse(info.isCorruptedOrIncomplete());
    assertEquals(0, info.getStatsForType(ProfilerTask.TEST, info.rootTasksById).count);
    assertEquals(1, info.getStatsForType(ProfilerTask.SCANNER, info.rootTasksById).count);
    assertEquals(1, info.getStatsForType(ProfilerTask.EXCEPTION, info.rootTasksById).count);
    assertEquals(2, info.getStatsForType(ProfilerTask.UNKNOWN, info.rootTasksById).count);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Deflater(java.util.zip.Deflater) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Inflater(java.util.zip.Inflater) Test(org.junit.Test)

Example 48 with Deflater

use of java.util.zip.Deflater in project mapdb by jankotek.

the class SerializerCompressionDeflateWrapper method valueArraySerialize.

@Override
public void valueArraySerialize(DataOutput2 out, Object vals) throws IOException {
    DataOutput2 out2 = new DataOutput2();
    serializer.valueArraySerialize(out2, vals);
    if (out2.pos == 0)
        return;
    byte[] tmp = new byte[out2.pos + 41];
    int newLen;
    try {
        Deflater deflater = new Deflater(compressLevel);
        if (dictionary != null) {
            deflater.setDictionary(dictionary);
        }
        deflater.setInput(out2.buf, 0, out2.pos);
        deflater.finish();
        newLen = deflater.deflate(tmp);
    //LZF.get().compress(out2.buf,out2.pos,tmp,0);
    } catch (IndexOutOfBoundsException e) {
        //larger after compression
        newLen = 0;
    }
    if (newLen >= out2.pos || newLen == 0) {
        //compression adds size, so do not compress
        out.packInt(0);
        out.write(out2.buf, 0, out2.pos);
        return;
    }
    //unpacked size, zero indicates no compression
    out.packInt(out2.pos + 1);
    out.write(tmp, 0, newLen);
}
Also used : Deflater(java.util.zip.Deflater)

Example 49 with Deflater

use of java.util.zip.Deflater in project j2objc by google.

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 50 with Deflater

use of java.util.zip.Deflater in project j2objc by google.

the class OldAndroidDeflateTest method simpleTest.

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

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