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));
}
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);
}
}
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);
}
}
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);
}
}
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();
}
}
}
Aggregations