Search in sources :

Example 1 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project elasticsearch by elastic.

the class DeflateCompressor method streamOutput.

@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
    out.writeBytes(HEADER);
    final boolean nowrap = true;
    final Deflater deflater = new Deflater(LEVEL, nowrap);
    final boolean syncFlush = true;
    OutputStream compressedOut = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    compressedOut = new BufferedOutputStream(compressedOut, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {

        final AtomicBoolean closed = new AtomicBoolean(false);

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed.compareAndSet(false, true)) {
                    // important to release native memory
                    deflater.end();
                }
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Deflater(java.util.zip.Deflater) OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project jvm-serializers by eishay.

the class BenchmarkBase method compressDeflate.

protected static byte[] compressDeflate(byte[] data) {
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
        DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
        compresser.write(data, 0, data.length);
        compresser.finish();
        compresser.flush();
        return bout.toByteArray();
    } catch (IOException ex) {
        AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
        ae.initCause(ex);
        throw ae;
    }
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 3 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project hadoop by apache.

the class TestDFSShell method textTest.

private void textTest(Path root, Configuration conf) throws Exception {
    PrintStream bak = null;
    try {
        final FileSystem fs = root.getFileSystem(conf);
        fs.mkdirs(root);
        // Test the gzip type of files. Magic detection.
        OutputStream zout = new GZIPOutputStream(fs.create(new Path(root, "file.gz")));
        Random r = new Random();
        bak = System.out;
        ByteArrayOutputStream file = new ByteArrayOutputStream();
        for (int i = 0; i < 1024; ++i) {
            char c = Character.forDigit(r.nextInt(26) + 10, 36);
            file.write(c);
            zout.write(c);
        }
        zout.close();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
        String[] argv = new String[2];
        argv[0] = "-text";
        argv[1] = new Path(root, "file.gz").toString();
        int ret = ToolRunner.run(new FsShell(conf), argv);
        assertEquals("'-text " + argv[1] + " returned " + ret, 0, ret);
        assertTrue("Output doesn't match input", Arrays.equals(file.toByteArray(), out.toByteArray()));
        // Create a sequence file with a gz extension, to test proper
        // container detection. Magic detection.
        SequenceFile.Writer writer = SequenceFile.createWriter(conf, SequenceFile.Writer.file(new Path(root, "file.gz")), SequenceFile.Writer.keyClass(Text.class), SequenceFile.Writer.valueClass(Text.class));
        writer.append(new Text("Foo"), new Text("Bar"));
        writer.close();
        out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
        argv = new String[2];
        argv[0] = "-text";
        argv[1] = new Path(root, "file.gz").toString();
        ret = ToolRunner.run(new FsShell(conf), argv);
        assertEquals("'-text " + argv[1] + " returned " + ret, 0, ret);
        assertTrue("Output doesn't match input", Arrays.equals("Foo\tBar\n".getBytes(), out.toByteArray()));
        out.reset();
        // Test deflate. Extension-based detection.
        OutputStream dout = new DeflaterOutputStream(fs.create(new Path(root, "file.deflate")));
        byte[] outbytes = "foo".getBytes();
        dout.write(outbytes);
        dout.close();
        out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
        argv = new String[2];
        argv[0] = "-text";
        argv[1] = new Path(root, "file.deflate").toString();
        ret = ToolRunner.run(new FsShell(conf), argv);
        assertEquals("'-text " + argv[1] + " returned " + ret, 0, ret);
        assertTrue("Output doesn't match input", Arrays.equals(outbytes, out.toByteArray()));
        out.reset();
        // Test a simple codec. Extension based detection. We use
        // Bzip2 cause its non-native.
        CompressionCodec codec = ReflectionUtils.newInstance(BZip2Codec.class, conf);
        String extension = codec.getDefaultExtension();
        Path p = new Path(root, "file." + extension);
        OutputStream fout = new DataOutputStream(codec.createOutputStream(fs.create(p, true)));
        byte[] writebytes = "foo".getBytes();
        fout.write(writebytes);
        fout.close();
        out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
        argv = new String[2];
        argv[0] = "-text";
        argv[1] = new Path(root, p).toString();
        ret = ToolRunner.run(new FsShell(conf), argv);
        assertEquals("'-text " + argv[1] + " returned " + ret, 0, ret);
        assertTrue("Output doesn't match input", Arrays.equals(writebytes, out.toByteArray()));
        out.reset();
        // Test a plain text.
        OutputStream pout = fs.create(new Path(root, "file.txt"));
        writebytes = "bar".getBytes();
        pout.write(writebytes);
        pout.close();
        out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
        argv = new String[2];
        argv[0] = "-text";
        argv[1] = new Path(root, "file.txt").toString();
        ret = ToolRunner.run(new FsShell(conf), argv);
        assertEquals("'-text " + argv[1] + " returned " + ret, 0, ret);
        assertTrue("Output doesn't match input", Arrays.equals(writebytes, out.toByteArray()));
        out.reset();
    } finally {
        if (null != bak) {
            System.setOut(bak);
        }
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Text(org.apache.hadoop.io.Text) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Random(java.util.Random) SequenceFile(org.apache.hadoop.io.SequenceFile) GZIPOutputStream(java.util.zip.GZIPOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) CompressionCodec(org.apache.hadoop.io.compress.CompressionCodec)

Example 4 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project jersey by jersey.

the class DeflateEncoder method encode.

@Override
public OutputStream encode(String contentEncoding, OutputStream entityStream) throws IOException {
    // some implementations don't support the correct deflate
    // so we have a property to configure the incorrect deflate (no zlib wrapper) should be used
    // let's check that
    Object value = config.getProperty(MessageProperties.DEFLATE_WITHOUT_ZLIB);
    boolean deflateWithoutZLib;
    if (value instanceof String) {
        deflateWithoutZLib = Boolean.valueOf((String) value);
    } else if (value instanceof Boolean) {
        deflateWithoutZLib = (Boolean) value;
    } else {
        deflateWithoutZLib = false;
    }
    return deflateWithoutZLib ? new DeflaterOutputStream(entityStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true)) : new DeflaterOutputStream(entityStream);
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 5 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project dropwizard by dropwizard.

the class BiDiGzipHandlerTest method testDecompressDeflateRequestGzipCompatible.

@Test
public void testDecompressDeflateRequestGzipCompatible() throws Exception {
    request.setMethod("POST");
    request.setURI("/banner");
    request.setHeader(HttpHeaders.CONTENT_ENCODING, "deflate");
    request.setHeader(HttpHeaders.CONTENT_TYPE, PLAIN_TEXT_UTF_8);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DeflaterOutputStream deflate = new DeflaterOutputStream(baos, new Deflater(-1, true))) {
        Resources.copy(Resources.getResource("assets/new-banner.txt"), deflate);
    }
    request.setContent(baos.toByteArray());
    HttpTester.Response response = HttpTester.parseResponse(servletTester.getResponses(request.generate()));
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContent()).isEqualTo("Banner has been updated");
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Aggregations

DeflaterOutputStream (java.util.zip.DeflaterOutputStream)151 ByteArrayOutputStream (java.io.ByteArrayOutputStream)100 Deflater (java.util.zip.Deflater)61 IOException (java.io.IOException)52 OutputStream (java.io.OutputStream)33 DataOutputStream (java.io.DataOutputStream)21 InflaterInputStream (java.util.zip.InflaterInputStream)14 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)8 GZIPOutputStream (java.util.zip.GZIPOutputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 FileInputStream (java.io.FileInputStream)6 BufferedOutputStream (java.io.BufferedOutputStream)5 InputStream (java.io.InputStream)5 ObjectOutputStream (java.io.ObjectOutputStream)5 ByteArrayOutputStream (org.fusesource.hawtbuf.ByteArrayOutputStream)5 Saml2Exception (org.springframework.security.saml2.Saml2Exception)5 ByteBuffer (java.nio.ByteBuffer)4 EOFException (java.io.EOFException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3