use of java.util.zip.Deflater in project voltdb by VoltDB.
the class ScriptWriterZipped method openFile.
protected void openFile() {
try {
FileAccess fa = database.getFileAccess();
java.io.OutputStream fos = fa.openOutputStreamElement(outFile);
outDescriptor = fa.getFileSync(fos);
fileStreamOut = new DeflaterOutputStream(fos, new Deflater(Deflater.DEFAULT_COMPRESSION), bufferSize);
} catch (IOException e) {
throw Error.error(ErrorCode.FILE_IO_ERROR, ErrorCode.M_Message_Pair, new Object[] { e.toString(), outFile });
}
}
use of java.util.zip.Deflater in project voltdb by VoltDB.
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 simba-os by cegeka.
the class SAMLServiceImpl method encodeSAMLRequest.
protected String encodeSAMLRequest(byte[] pSAMLRequest) throws RuntimeException {
Base64 base64Encoder = new Base64();
try {
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
DeflaterOutputStream def = new DeflaterOutputStream(byteArray, deflater);
def.write(pSAMLRequest);
def.close();
byteArray.close();
String stream = new String(base64Encoder.encode(byteArray.toByteArray()));
return stream.trim();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.util.zip.Deflater in project ddf by codice.
the class SimpleSignTest method deflateAndBase64Encode.
/**
* Deflates a value and Base64 encodes the result. This code is copied from RestSecurity because it would cause a circular dependency to use it directly..
*
* @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, true))) {
tokenStream.write(value.getBytes(StandardCharsets.UTF_8));
tokenStream.close();
return Base64.getEncoder().encodeToString(valueBytes.toByteArray());
}
}
use of java.util.zip.Deflater in project gatk by broadinstitute.
the class IntelInflaterDeflaterIntegrationTest method deflateInflateWithIntel.
@Test
public void deflateInflateWithIntel() throws DataFormatException {
if (!isIntelInflaterDeflaterSupported()) {
throw new SkipException("IntelInflater/IntelDeflater not available on this platform");
}
// create buffers and random input
final int LEN = 64 * 1024;
final byte[] input = new RandomDNA().nextBases(LEN);
final byte[] compressed = new byte[2 * LEN];
final byte[] result = new byte[LEN];
final IntelInflaterFactory intelInflaterFactory = new IntelInflaterFactory();
final IntelDeflaterFactory intelDeflaterFactory = new IntelDeflaterFactory();
Assert.assertTrue(intelInflaterFactory.usingIntelInflater());
Assert.assertTrue(intelDeflaterFactory.usingIntelDeflater());
for (int i = 0; i < 10; i++) {
// create deflater with compression level i
final Deflater deflater = intelDeflaterFactory.makeDeflater(i, true);
// setup deflater
deflater.setInput(input);
deflater.finish();
// compress data
int compressedBytes = 0;
// so this loop should always finish in one iteration
while (!deflater.finished()) {
compressedBytes = deflater.deflate(compressed, 0, compressed.length);
}
deflater.end();
// decompress and check output == input
Inflater inflater = intelInflaterFactory.makeInflater(true);
inflater.setInput(compressed, 0, compressedBytes);
inflater.inflate(result);
inflater.end();
Assert.assertEquals(input, result);
// clear compressed and result buffers for next iteration
Arrays.fill(compressed, (byte) 0);
Arrays.fill(result, (byte) 0);
}
}
Aggregations