Search in sources :

Example 6 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 7 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 8 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)

Example 9 with DeflaterOutputStream

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

the class CompressionService method gzipBytes.

public static byte[] gzipBytes(byte[] rawBytes) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(rawBytes.length);
    DeflaterOutputStream dos = new DeflaterOutputStream(bos);
    dos.write(rawBytes);
    dos.close();
    return bos.toByteArray();
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with DeflaterOutputStream

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

the class Encoder method compressAndBase64Encode.

public static String compressAndBase64Encode(byte[] bytes) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
        DeflaterOutputStream dos = new DeflaterOutputStream(baos);
        dos.write(bytes);
        dos.close();
        byte[] outBytes = baos.toByteArray();
        return Base64.encodeToString(outBytes, false);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DeflaterOutputStream (java.util.zip.DeflaterOutputStream)76 ByteArrayOutputStream (java.io.ByteArrayOutputStream)50 Deflater (java.util.zip.Deflater)25 IOException (java.io.IOException)24 OutputStream (java.io.OutputStream)13 InflaterInputStream (java.util.zip.InflaterInputStream)9 DataOutputStream (java.io.DataOutputStream)8 GZIPOutputStream (java.util.zip.GZIPOutputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BufferedOutputStream (java.io.BufferedOutputStream)3 EOFException (java.io.EOFException)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 ImageException (cbit.image.ImageException)2 DeflateCompressor (com.linkedin.r2.filter.compression.streaming.DeflateCompressor)2 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 PipedInputStream (java.io.PipedInputStream)2