Search in sources :

Example 71 with Deflater

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 });
    }
}
Also used : Deflater(java.util.zip.Deflater) FileAccess(org.hsqldb_voltpatches.lib.FileAccess) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) IOException(java.io.IOException)

Example 72 with Deflater

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

Example 73 with Deflater

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);
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 74 with Deflater

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

Example 75 with Deflater

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);
    }
}
Also used : IntelDeflaterFactory(com.intel.gkl.compression.IntelDeflaterFactory) IntelDeflater(com.intel.gkl.compression.IntelDeflater) Deflater(java.util.zip.Deflater) IntelInflaterFactory(com.intel.gkl.compression.IntelInflaterFactory) RandomDNA(org.broadinstitute.hellbender.utils.RandomDNA) Inflater(java.util.zip.Inflater) IntelInflater(com.intel.gkl.compression.IntelInflater) SkipException(org.testng.SkipException) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

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