Search in sources :

Example 16 with Deflater

use of java.util.zip.Deflater in project voltdb by VoltDB.

the class FileArchiver method archive.

/*
    public static void compressFile(String infilename, String outfilename,
                                    FileAccess storage) throws IOException {
        FileArchiver.archive(infilename, outfilename, storage, COMPRESSION_ZIP);
    }

    public static void decompressFile(String infilename, String outfilename,
                                      FileAccess storage) throws IOException {
        FileArchiver.unarchive(
                infilename, outfilename, storage, COMPRESSION_ZIP);
    }

    public static void copyFile(String infilename, String outfilename,
                                    FileAccess storage) throws IOException {
        FileArchiver.archive(
                infilename, outfilename, storage, COMPRESSION_NONE);
    }

    public static void restoreFile(String infilename, String outfilename,
                                      FileAccess storage) throws IOException {
        FileArchiver.unarchive(
                infilename, outfilename, storage, COMPRESSION_NONE);
    }
    */
public static void archive(String infilename, String outfilename, FileAccess storage, int compressionType) throws IOException {
    InputStream in = null;
    OutputStream f = null;
    DeflaterOutputStream deflater = null;
    boolean completed = false;
    // if there is no file
    if (!storage.isStreamElement(infilename)) {
        return;
    }
    try {
        byte[] b = new byte[COPY_BLOCK_SIZE];
        in = storage.openInputStreamElement(infilename);
        f = storage.openOutputStreamElement(outfilename);
        switch(compressionType) {
            case COMPRESSION_ZIP:
                f = deflater = new DeflaterOutputStream(f, new Deflater(Deflater.BEST_SPEED), b.length);
                break;
            case COMPRESSION_GZIP:
                f = deflater = new GZIPOutputStream(f, b.length);
                break;
            case COMPRESSION_NONE:
                break;
            default:
                throw new RuntimeException("FileArchiver" + compressionType);
        }
        while (true) {
            int l = in.read(b, 0, b.length);
            if (l == -1) {
                break;
            }
            f.write(b, 0, l);
        }
        completed = true;
    } catch (Throwable e) {
        throw FileUtil.toIOException(e);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (f != null) {
                if (deflater != null) {
                    deflater.finish();
                }
                f.close();
            }
            if (!completed && storage.isStreamElement(outfilename)) {
                storage.removeElement(outfilename);
            }
        } catch (Throwable e) {
            throw FileUtil.toIOException(e);
        }
    }
}
Also used : Deflater(java.util.zip.Deflater) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 17 with Deflater

use of java.util.zip.Deflater in project graylog2-server by Graylog2.

the class TestHelper method zlibCompress.

public static byte[] zlibCompress(String what, int level) throws IOException {
    final ByteArrayInputStream compressMe = new ByteArrayInputStream(what.getBytes(StandardCharsets.UTF_8));
    final ByteArrayOutputStream compressedMessage = new ByteArrayOutputStream();
    try (DeflaterOutputStream out = new DeflaterOutputStream(compressedMessage, new Deflater(level))) {
        ByteStreams.copy(compressMe, out);
    }
    return compressedMessage.toByteArray();
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 18 with Deflater

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

the class DoubleHistogramTest method testDoubleHistogramSerialization.

void testDoubleHistogramSerialization(DoubleHistogram histogram) throws Exception {
    histogram.recordValue(testValueLevel);
    histogram.recordValue(testValueLevel * 10);
    histogram.recordValueWithExpectedInterval(histogram.getCurrentHighestTrackableValue() - 1, histogram.getCurrentHighestTrackableValue() / 1000);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    ByteArrayInputStream bis = null;
    ObjectInput in = null;
    DoubleHistogram newHistogram = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(histogram);
        Deflater compresser = new Deflater();
        compresser.setInput(bos.toByteArray());
        compresser.finish();
        byte[] compressedOutput = new byte[1024 * 1024];
        int compressedDataLength = compresser.deflate(compressedOutput);
        System.out.println("Serialized form of " + histogram.getClass() + " with internalHighestToLowestValueRatio = " + histogram.getHighestToLowestValueRatio() + "\n and a numberOfSignificantValueDigits = " + histogram.getNumberOfSignificantValueDigits() + " is " + bos.toByteArray().length + " bytes long. Compressed form is " + compressedDataLength + " bytes long.");
        System.out.println("   (estimated footprint was " + histogram.getEstimatedFootprintInBytes() + " bytes)");
        bis = new ByteArrayInputStream(bos.toByteArray());
        in = new ObjectInputStream(bis);
        newHistogram = (DoubleHistogram) in.readObject();
    } finally {
        if (out != null)
            out.close();
        bos.close();
        if (in != null)
            in.close();
        if (bis != null)
            bis.close();
    }
    assertNotNull(newHistogram);
    assertEqual(histogram, newHistogram);
}
Also used : Deflater(java.util.zip.Deflater)

Example 19 with Deflater

use of java.util.zip.Deflater in project MVPArms by JessYanCoding.

the class ZipHelper method compressForZlib.

/**
     * zlib compress 2 byte
     *
     * @param bytesToCompress
     * @return
     */
public static byte[] compressForZlib(byte[] bytesToCompress) {
    Deflater deflater = new Deflater();
    deflater.setInput(bytesToCompress);
    deflater.finish();
    byte[] bytesCompressed = new byte[Short.MAX_VALUE];
    int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed);
    byte[] returnValues = new byte[numberOfBytesAfterCompression];
    System.arraycopy(bytesCompressed, 0, returnValues, 0, numberOfBytesAfterCompression);
    return returnValues;
}
Also used : Deflater(java.util.zip.Deflater)

Example 20 with Deflater

use of java.util.zip.Deflater in project undertow by undertow-io.

the class CompressionUtilsTest method setup.

@Before
public void setup() throws Exception {
    compress = new Deflater(Deflater.BEST_SPEED, true);
    decompress = new Inflater(true);
}
Also used : Deflater(java.util.zip.Deflater) Inflater(java.util.zip.Inflater) 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