Search in sources :

Example 26 with Deflater

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

the class OldAndroidDeflateTest method bigTest.

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

Example 27 with Deflater

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

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)

Example 28 with Deflater

use of java.util.zip.Deflater in project trex-stateless-gui by cisco-system-traffic-generator.

the class CompressionUtils method compress.

public static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    return output;
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 29 with Deflater

use of java.util.zip.Deflater in project ddf by codice.

the class RestSecurity method deflateAndBase64Encode.

/**
     * Deflates a value and Base64 encodes the result.
     *
     * @param value value to deflate and Base64 encode
     * @return String
     * @throws IOException if the value cannot be converted
     */
public static String deflateAndBase64Encode(String value) throws IOException {
    ByteArrayOutputStream valueBytes = new ByteArrayOutputStream();
    try (OutputStream tokenStream = new DeflaterOutputStream(valueBytes, new Deflater(Deflater.DEFLATED, GZIP_COMPATIBLE))) {
        tokenStream.write(value.getBytes(StandardCharsets.UTF_8));
        tokenStream.close();
        return Base64.getEncoder().encodeToString(valueBytes.toByteArray());
    }
}
Also used : Deflater(java.util.zip.Deflater) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 30 with Deflater

use of java.util.zip.Deflater in project geode by apache.

the class CliUtil method compressBytes.

public static DeflaterInflaterData compressBytes(byte[] input) {
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    byte[] buffer = new byte[100];
    byte[] result = new byte[0];
    int compressedDataLength = 0;
    int totalCompressedDataLength = 0;
    do {
        byte[] newResult = new byte[result.length + buffer.length];
        System.arraycopy(result, 0, newResult, 0, result.length);
        compressedDataLength = compresser.deflate(buffer);
        totalCompressedDataLength += compressedDataLength;
        System.arraycopy(buffer, 0, newResult, result.length, buffer.length);
        result = newResult;
    } while (compressedDataLength != 0);
    return new DeflaterInflaterData(totalCompressedDataLength, result);
}
Also used : Deflater(java.util.zip.Deflater)

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