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;
}
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();
}
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);
}
}
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);
}
}
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");
}
Aggregations