use of java.util.zip.GZIPInputStream in project camel by apache.
the class GzipDataFormat method unmarshal.
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
GZIPInputStream unzipInput = null;
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
try {
unzipInput = new GZIPInputStream(inputStream);
IOHelper.copy(unzipInput, osb);
return osb.build();
} finally {
// must close all input streams
IOHelper.close(osb, unzipInput, inputStream);
}
}
use of java.util.zip.GZIPInputStream in project hadoop by apache.
the class WritableUtils method readCompressedByteArray.
public static byte[] readCompressedByteArray(DataInput in) throws IOException {
int length = in.readInt();
if (length == -1)
return null;
byte[] buffer = new byte[length];
// could/should use readFully(buffer,0,length)?
in.readFully(buffer);
GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buffer, 0, buffer.length));
byte[] outbuf = new byte[length];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
while ((len = gzi.read(outbuf, 0, outbuf.length)) != -1) {
bos.write(outbuf, 0, len);
}
byte[] decompressed = bos.toByteArray();
bos.close();
gzi.close();
return decompressed;
}
use of java.util.zip.GZIPInputStream in project hadoop by apache.
the class TestCodec method verifyGzipFile.
private void verifyGzipFile(String filename, String msg) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(filename))));
try {
String line = r.readLine();
assertEquals("Got invalid line back from " + filename, msg, line);
} finally {
r.close();
new File(filename).delete();
}
}
use of java.util.zip.GZIPInputStream in project hbase by apache.
the class Base64 method decode.
/**
* Decodes data from Base64 notation, automatically detecting gzip-compressed
* data and decompressing it.
*
* @param s the string to decode
* @param options options for decode
* @see Base64#URL_SAFE
* @see Base64#ORDERED
* @return the decoded data
* @since 1.4
*/
public static byte[] decode(String s, int options) {
byte[] bytes;
try {
bytes = s.getBytes(PREFERRED_ENCODING);
} catch (UnsupportedEncodingException uee) {
bytes = s.getBytes();
}
// end catch
// Decode
bytes = decode(bytes, 0, bytes.length, options);
if (bytes != null && bytes.length >= 4) {
int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
if (GZIPInputStream.GZIP_MAGIC == head) {
GZIPInputStream gzis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
gzis = new GZIPInputStream(new ByteArrayInputStream(bytes));
byte[] buffer = new byte[2048];
for (int length; (length = gzis.read(buffer)) >= 0; ) {
baos.write(buffer, 0, length);
}
// end while: reading input
// No error? Get new bytes.
bytes = baos.toByteArray();
} catch (IOException e) {
// Just return originally-decoded bytes
} finally {
try {
baos.close();
} catch (Exception e) {
LOG.error("error closing ByteArrayOutputStream", e);
}
if (gzis != null) {
try {
gzis.close();
} catch (Exception e) {
LOG.error("error closing GZIPInputStream", e);
}
}
}
// end finally
}
// end if: gzipped
}
return bytes;
}
use of java.util.zip.GZIPInputStream in project hbase by apache.
the class TestGzipFilter method testGzipFilter.
@Test
public void testGzipFilter() throws Exception {
String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream os = new GZIPOutputStream(bos);
os.write(VALUE_1);
os.close();
byte[] value_1_gzip = bos.toByteArray();
// input side filter
Header[] headers = new Header[2];
headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_BINARY);
headers[1] = new BasicHeader("Content-Encoding", "gzip");
Response response = client.put(path, headers, value_1_gzip);
assertEquals(response.getCode(), 200);
Table table = TEST_UTIL.getConnection().getTable(TABLE);
Get get = new Get(Bytes.toBytes(ROW_1));
get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
assertNotNull(value);
assertTrue(Bytes.equals(value, VALUE_1));
// output side filter
headers[0] = new BasicHeader("Accept", Constants.MIMETYPE_BINARY);
headers[1] = new BasicHeader("Accept-Encoding", "gzip");
response = client.get(path, headers);
assertEquals(response.getCode(), 200);
ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
GZIPInputStream is = new GZIPInputStream(bis);
value = new byte[VALUE_1.length];
is.read(value, 0, VALUE_1.length);
assertTrue(Bytes.equals(value, VALUE_1));
is.close();
table.close();
testScannerResultCodes();
}
Aggregations