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