Search in sources :

Example 1 with GZIPInputStream

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 2 with GZIPInputStream

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream)

Example 3 with GZIPInputStream

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();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) SequenceFile(org.apache.hadoop.io.SequenceFile) MapFile(org.apache.hadoop.io.MapFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with GZIPInputStream

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with GZIPInputStream

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();
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Result(org.apache.hadoop.hbase.client.Result) Response(org.apache.hadoop.hbase.rest.client.Response) GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Get(org.apache.hadoop.hbase.client.Get) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Aggregations

GZIPInputStream (java.util.zip.GZIPInputStream)376 InputStream (java.io.InputStream)144 IOException (java.io.IOException)125 ByteArrayInputStream (java.io.ByteArrayInputStream)120 FileInputStream (java.io.FileInputStream)98 ByteArrayOutputStream (java.io.ByteArrayOutputStream)77 InputStreamReader (java.io.InputStreamReader)57 File (java.io.File)56 BufferedReader (java.io.BufferedReader)45 BufferedInputStream (java.io.BufferedInputStream)41 Test (org.junit.Test)41 FileOutputStream (java.io.FileOutputStream)30 URL (java.net.URL)25 InflaterInputStream (java.util.zip.InflaterInputStream)25 OutputStream (java.io.OutputStream)24 GZIPOutputStream (java.util.zip.GZIPOutputStream)21 ObjectInputStream (java.io.ObjectInputStream)19 HttpURLConnection (java.net.HttpURLConnection)19 URLConnection (java.net.URLConnection)17 HashMap (java.util.HashMap)15