Search in sources :

Example 11 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project tomcat by apache.

the class TestGzipOutputFilter method testFlushingWithGzip.

/*
     * Test the interaction between gzip and flushing. The idea is to: 1. create
     * a internal output buffer, response, and attach an active gzipoutputfilter
     * to the output buffer 2. set the output stream of the internal buffer to
     * be a ByteArrayOutputStream so we can inspect the output bytes 3. write a
     * chunk out using the gzipoutputfilter and invoke a flush on the
     * InternalOutputBuffer 4. read from the ByteArrayOutputStream to find out
     * what's being written out (flushed) 5. find out what's expected by writing
     * to GZIPOutputStream and close it (to force flushing) 6. Compare the size
     * of the two arrays, they should be close (instead of one being much
     * shorter than the other one)
     *
     * @throws Exception
     */
@Test
public void testFlushingWithGzip() throws Exception {
    // set up response, InternalOutputBuffer, and ByteArrayOutputStream
    Response res = new Response();
    TesterOutputBuffer tob = new TesterOutputBuffer(res, 8 * 1024);
    res.setOutputBuffer(tob);
    // set up GzipOutputFilter to attach to the TesterOutputBuffer
    GzipOutputFilter gf = new GzipOutputFilter();
    tob.addFilter(gf);
    tob.addActiveFilter(gf);
    // write a chunk out
    byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
    tob.doWrite(ByteBuffer.wrap(d));
    // flush the InternalOutputBuffer
    tob.flush();
    // read from the ByteArrayOutputStream to find out what's being written
    // out (flushed)
    byte[] dataFound = tob.toByteArray();
    // find out what's expected by writing to GZIPOutputStream and close it
    // (to force flushing)
    ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
    GZIPOutputStream gos = new GZIPOutputStream(gbos);
    gos.write(d);
    gos.close();
    // read the expected data
    byte[] dataExpected = gbos.toByteArray();
    // most of the data should have been flushed out
    assertTrue(dataFound.length >= (dataExpected.length - 20));
}
Also used : Response(org.apache.coyote.Response) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 12 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project storm by apache.

the class GzipSerializationDelegate method serialize.

@Override
public byte[] serialize(Object object) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(bos);
        ObjectOutputStream oos = new ObjectOutputStream(gos);
        oos.writeObject(object);
        oos.close();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 13 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project storm by apache.

the class Utils method gzip.

public static byte[] gzip(byte[] data) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream out = new GZIPOutputStream(bos);
        out.write(data);
        out.close();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 14 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project storm by apache.

the class Utils method toCompressedJsonConf.

public static byte[] toCompressedJsonConf(Map<String, Object> stormConf) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        OutputStreamWriter out = new OutputStreamWriter(new GZIPOutputStream(bos));
        JSONValue.writeJSONString(stormConf, out);
        out.close();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 15 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project druid by druid-io.

the class HdfsDataSegmentPullerTest method testGZ.

@Test
public void testGZ() throws IOException, SegmentLoadingException {
    final Path zipPath = new Path("/tmp/testZip.gz");
    final File outTmpDir = com.google.common.io.Files.createTempDir();
    final File outFile = new File(outTmpDir, "testZip");
    outFile.delete();
    final URI uri = URI.create(uriBase.toString() + zipPath.toString());
    try (final OutputStream outputStream = miniCluster.getFileSystem().create(zipPath)) {
        try (final OutputStream gzStream = new GZIPOutputStream(outputStream)) {
            try (final InputStream inputStream = new ByteArrayInputStream(pathByteContents)) {
                ByteStreams.copy(inputStream, gzStream);
            }
        }
    }
    try {
        Assert.assertFalse(outFile.exists());
        puller.getSegmentFiles(uri, outTmpDir);
        Assert.assertTrue(outFile.exists());
        Assert.assertArrayEquals(pathByteContents, Files.readAllBytes(outFile.toPath()));
    } finally {
        if (outFile.exists()) {
            outFile.delete();
        }
        if (outTmpDir.exists()) {
            outTmpDir.delete();
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) File(java.io.File) URI(java.net.URI) Test(org.junit.Test)

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)322 ByteArrayOutputStream (java.io.ByteArrayOutputStream)135 FileOutputStream (java.io.FileOutputStream)96 IOException (java.io.IOException)93 OutputStream (java.io.OutputStream)66 File (java.io.File)62 Test (org.junit.Test)48 BufferedOutputStream (java.io.BufferedOutputStream)30 FileInputStream (java.io.FileInputStream)28 OutputStreamWriter (java.io.OutputStreamWriter)27 GZIPInputStream (java.util.zip.GZIPInputStream)25 InputStream (java.io.InputStream)24 ByteBuffer (java.nio.ByteBuffer)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 BufferedWriter (java.io.BufferedWriter)18 DropBoxManager (android.os.DropBoxManager)15 BufferedReader (java.io.BufferedReader)11 DataOutputStream (java.io.DataOutputStream)11 FileNotFoundException (java.io.FileNotFoundException)11 InputStreamReader (java.io.InputStreamReader)11