Search in sources :

Example 36 with Deflater

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

the class AbstractHistogram method encodeIntoCompressedByteBuffer.

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

Example 37 with Deflater

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

the class CompressedArray method compress.

/**
 * 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();
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 38 with Deflater

use of java.util.zip.Deflater in project jjwt by jwtk.

the class DeflateCompressionCodec method doCompress.

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

Example 39 with Deflater

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

the class ScreenVideoEncoder method encodePixelsSVC2.

public static byte[] encodePixelsSVC2(int[] pixels, int width, int height) {
    changePixelScanFromBottomLeftToTopRight(pixels, width, height);
    if (paletteIndex == null)
        createPaletteIndex();
    try {
        // TODO calibrate initial size
        ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
        for (int i = 0; i < pixels.length; i++) {
            writeAs15_7(pixels[i], baos1);
        }
        // baos2 contains everything from IMAGEBLOCKV2 except the DataSize field
        // TODO calibrate initial size
        ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
        // IMAGEFORMAT:
        // ColorDepth: UB[2] 10 (15/7 hybrid color image); HasDiffBlocks: UB[1] 0; Zlib prime stuff (2 bits) not used (0)
        baos2.write(16);
        // No ImageBlockHeader (IMAGEDIFFPOSITION, IMAGEPRIMEPOSITION)
        Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
        DeflaterOutputStream deflateroutputstream = new DeflaterOutputStream(baos2, deflater);
        deflateroutputstream.write(baos1.toByteArray());
        deflateroutputstream.finish();
        byte[] dataBuffer = baos2.toByteArray();
        // DataSize field
        int dataSize = dataBuffer.length;
        // TODO calibrate initial size
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        writeShort(((OutputStream) (baos)), dataSize);
        // Data
        baos.write(dataBuffer, 0, dataSize);
        return baos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
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) IOException(java.io.IOException)

Example 40 with Deflater

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

the class ZipWriterTest method setup.

@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");
}
Also used : Random(java.util.Random) Deflater(java.util.zip.Deflater) CRC32(java.util.zip.CRC32) Before(org.junit.Before)

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