Example usage for java.util.zip GZIPInputStream close

List of usage examples for java.util.zip GZIPInputStream close

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:org.unitime.commons.hibernate.blob.XmlBlobType.java

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws SQLException {
    Blob blob = rs.getBlob(names[0]);
    if (blob == null)
        return null;
    try {/*from w  ww.  j a v a  2  s  .co m*/
        SAXReader reader = new SAXReader();
        GZIPInputStream gzipInput = new GZIPInputStream(blob.getBinaryStream());
        Document document = reader.read(gzipInput);
        gzipInput.close();
        return document;
    } catch (IOException e) {
        throw new HibernateException(e.getMessage(), e);
    } catch (DocumentException e) {
        throw new HibernateException(e.getMessage(), e);
    }
}

From source file:com.krawler.common.util.ByteUtil.java

/**
 * uncompress the supplied data using GZIPInputStream and return the
 * uncompressed data.//w  w w. j  a va  2  s  . c  om
 * 
 * @param data
 *            data to uncompress
 * @return uncompressed data
 */
public static byte[] uncompress(byte[] data) throws IOException {
    // TODO: optimize, this makes my head hurt
    ByteArrayOutputStream baos = null;
    ByteArrayInputStream bais = null;
    GZIPInputStream gis = null;
    try {
        int estimatedResultSize = data.length * 3;
        baos = new ByteArrayOutputStream(estimatedResultSize);
        bais = new ByteArrayInputStream(data);
        byte[] buffer = new byte[8192];
        gis = new GZIPInputStream(bais);

        int numRead;
        while ((numRead = gis.read(buffer, 0, buffer.length)) != -1) {
            baos.write(buffer, 0, numRead);
        }
        return baos.toByteArray();
    } finally {
        if (gis != null)
            gis.close();
        else if (bais != null)
            bais.close();
        if (baos != null)
            baos.close();
    }
}

From source file:sample.undertow.SampleUndertowApplicationTests.java

@Test
public void testCompression() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Accept-Encoding", "gzip");
    HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    RestTemplate restTemplate = new TestRestTemplate();
    ResponseEntity<byte[]> entity = restTemplate.exchange("http://localhost:" + this.port, HttpMethod.GET,
            requestEntity, byte[].class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()));
    try {//from w ww .j  a v a  2 s  . com
        assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))).isEqualTo("Hello World");
    } finally {
        inflater.close();
    }
}

From source file:sample.jetty.SampleJettyApplicationTests.java

@Test
public void testCompression() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Accept-Encoding", "gzip");
    HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

    ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, requestEntity,
            byte[].class);

    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);

    GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()));
    try {//from   w  w  w  .  ja  v a  2s.com
        //         assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))).isEqualTo("Hello World");
    } finally {
        inflater.close();
    }
}

From source file:org.apache.hadoop.hbase.rest.TestGzipFilter.java

@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);/* w w w  .j  ava  2  s .co  m*/
    os.close();
    byte[] value_1_gzip = bos.toByteArray();

    // input side filter

    Header[] headers = new Header[2];
    headers[0] = new Header("Content-Type", Constants.MIMETYPE_BINARY);
    headers[1] = new Header("Content-Encoding", "gzip");
    Response response = client.put(path, headers, value_1_gzip);
    assertEquals(response.getCode(), 200);

    HTable table = new HTable(TEST_UTIL.getConfiguration(), 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 Header("Accept", Constants.MIMETYPE_BINARY);
    headers[1] = new Header("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();
}

From source file:weka.server.JSONProtocol.java

/**
 * Decode a base64 string (containing compressed bytes) back into an object
 *
 * @param base64 the base64 string//w  w  w .j  av  a 2s.co m
 * @return an decoded and deserialized object
 * @throws Exception if a problem occurs
 */
public static Object decodeBase64(String base64) throws Exception {
    byte[] bytes = Base64.decodeBase64(base64.getBytes());

    byte[] result = null;
    Object resultObject = null;
    if (bytes != null && bytes.length > 0) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        BufferedInputStream bi = new BufferedInputStream(gzip);

        result = new byte[] {};

        byte[] extra = new byte[1000000];
        int nrExtra = bi.read(extra);

        while (nrExtra >= 0) {
            // add it to bytes...
            int newSize = result.length + nrExtra;
            byte[] tmp = new byte[newSize];
            for (int i = 0; i < result.length; i++) {
                tmp[i] = result[i];
            }
            for (int i = 0; i < nrExtra; i++) {
                tmp[result.length + i] = extra[i];
            }

            // change the result
            result = tmp;
            nrExtra = bi.read(extra);
        }
        bytes = result;
        gzip.close();
    }

    if (result != null && result.length > 0) {
        ByteArrayInputStream bis = new ByteArrayInputStream(result);
        ObjectInputStream ois = new ObjectInputStream(bis);
        resultObject = ois.readObject();
        ois.close();
    }

    return resultObject;
}

From source file:com.navercorp.pinpoint.web.filter.Base64.java

public static byte[] decode(String s, int options) {
    byte[] bytes;
    try {/* w  w w  .  j  ava  2  s .c  om*/
        bytes = s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException var21) {
        bytes = s.getBytes();
    }

    bytes = decode(bytes, 0, bytes.length, options);
    if (bytes != null && bytes.length >= 4) {
        int head = bytes[0] & 255 | bytes[1] << 8 & '\uff00';
        if (35615 == head) {
            GZIPInputStream gzis = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            try {
                gzis = new GZIPInputStream(new ByteArrayInputStream(bytes));
                byte[] buffer = new byte[2048];

                int length;
                while ((length = gzis.read(buffer)) >= 0) {
                    baos.write(buffer, 0, length);
                }

                bytes = baos.toByteArray();
            } catch (IOException var22) {
                ;
            } finally {
                try {
                    baos.close();
                } catch (Exception var20) {
                    LOG.error("error closing ByteArrayOutputStream", var20);
                }

                if (gzis != null) {
                    try {
                        gzis.close();
                    } catch (Exception var19) {
                        LOG.error("error closing GZIPInputStream", var19);
                    }
                }

            }
        }
    }

    return bytes;
}

From source file:org.unitime.timetable.solver.ui.TimetableInfoUtil.java

public TimetableInfo loadFromFile(String name) {
    try {//from   w  w  w . j a  va 2  s.  c o m
        File file = new File(ApplicationProperties.getBlobFolder(), name);
        if (!file.exists())
            return null;
        sLog.info("Loading info " + name + " from " + file + " (" + file.length() + " bytes)");
        Document document = null;
        GZIPInputStream gzipInput = null;
        try {
            gzipInput = new GZIPInputStream(new FileInputStream(file));
            document = (new SAXReader()).read(gzipInput);
        } finally {
            if (gzipInput != null)
                gzipInput.close();
        }
        Element root = document.getRootElement();
        String infoClassName = root.getName();
        Class infoClass = Class.forName(infoClassName);
        TimetableInfo info = (TimetableInfo) infoClass.getConstructor(new Class[] {})
                .newInstance(new Object[] {});
        info.load(root);
        return info;
    } catch (Exception e) {
        sLog.warn("Failed to load info " + name + ": " + e.getMessage(), e);
        return null;
    }
}

From source file:org.opensextant.util.FileUtility.java

/**
 *
 * @param filepath//  w  w w  .ja va2 s.  c o m
 *            path to file
 * @return text buffer, UTF-8 decoded
 * @throws IOException
 *             on error
 */
public static String readGzipFile(String filepath) throws IOException {
    if (filepath == null) {
        return null;
    }

    final FileInputStream instream = new FileInputStream(filepath);
    final GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(instream), ioBufferSize);

    final byte[] inputBytes = new byte[ioBufferSize];
    final StringBuilder buf = new StringBuilder();

    int readcount = 0;
    while ((readcount = gzin.read(inputBytes, 0, ioBufferSize)) != -1) {
        buf.append(new String(inputBytes, 0, readcount, default_encoding));
    }
    instream.close();
    gzin.close();

    return buf.toString();

}

From source file:ca.efendi.datafeeds.messaging.FtpSubscriptionMessageListener.java

private void unzip(FtpSubscription ftpSubscription, final InputStream is) {
    try {//from  w  w  w.  j a  va 2s . c  om
        final GZIPInputStream gzis = new GZIPInputStream(is);
        parse(ftpSubscription, gzis);
        gzis.close();
    } catch (final IOException e) {
        _log.error(e);
    } catch (final XMLStreamException e) {
        _log.error(e);
    }
}