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:pro.foundev.GzipCompressionOfColumnExample.java

private static String fromByteBuffer(ByteBuffer byteBuffer) {
    if (byteBuffer == null) {
        return null;
    }/*from   www .  j a  v  a 2s. c o  m*/
    GZIPInputStream gzipInputStream = null;
    ByteArrayOutputStream baos = null;
    ByteBuffer dup = ByteBuffer.allocate(byteBuffer.limit());
    dup.clear();
    dup.put(byteBuffer);
    dup.flip();

    try {
        gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(dup.array(), 0, dup.limit()));

        baos = new ByteArrayOutputStream();
        for (int value = 0; value != -1;) {
            value = gzipInputStream.read();
            if (value != -1) {
                baos.write(value);
            }
        }
        gzipInputStream.close();
        baos.flush();
        baos.close();
        return new String(baos.toByteArray(), charset);
    } catch (IOException e) {
        throw new RuntimeException("Error decompressing column data", e);
    } finally {
        if (gzipInputStream != null) {
            try {
                gzipInputStream.close();
            } catch (IOException e) {
            }
        }
        if (baos != null) {
            try {
                baos.flush();
                baos.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.apache.carbondata.core.util.ObjectSerializationUtil.java

/**
 * Converts Base64 string to object.//w  w w .  j a  v  a2 s  . c o  m
 *
 * @param objectString serialized object in string format
 * @return Object after convert string to object
 * @throws IOException
 */
public static Object convertStringToObject(String objectString) throws IOException {
    if (objectString == null) {
        return null;
    }

    byte[] bytes = CarbonUtil.decodeStringToBytes(objectString);

    ByteArrayInputStream bais = null;
    GZIPInputStream gis = null;
    ObjectInputStream ois = null;

    try {
        bais = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(bais);
        ois = new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(), gis);
        return ois.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException("Could not read object", e);
    } finally {
        try {
            if (ois != null) {
                ois.close();
            }
            if (gis != null) {
                gis.close();
            }
            if (bais != null) {
                bais.close();
            }
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}

From source file:net.sf.ehcache.distribution.PayloadUtil.java

/**
 * The fastest Ungzip implementation. See PageInfoTest in ehcache-constructs.
 * A high performance implementation, although not as fast as gunzip3.
 * gunzips 100000 of ungzipped content in 9ms on the reference machine.
 * It does not use a fixed size buffer and is therefore suitable for arbitrary
 * length arrays./*from   w  ww.  j  a  v a  2  s . c  om*/
 *
 * @param gzipped
 * @return a plain, uncompressed byte[]
 */
public static byte[] ungzip(final byte[] gzipped) {
    byte[] ungzipped = new byte[0];
    try {
        final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped));
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length);
        final byte[] buffer = new byte[PayloadUtil.MTU];
        int bytesRead = 0;
        while (bytesRead != -1) {
            bytesRead = inputStream.read(buffer, 0, PayloadUtil.MTU);
            if (bytesRead != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
        }
        ungzipped = byteArrayOutputStream.toByteArray();
        inputStream.close();
        byteArrayOutputStream.close();
    } catch (IOException e) {
        LOG.fatal("Could not ungzip. Heartbeat will not be working. " + e.getMessage());
    }
    return ungzipped;
}

From source file:eu.diacron.crawlservice.app.Util.java

private static String unzipWarcFile(String input_gzip_file) {

    String output_file = input_gzip_file.split(".gz")[0];

    byte[] buffer = new byte[1024];

    try {/*from www.  j  a v a 2 s . co m*/

        GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(input_gzip_file));

        FileOutputStream out = new FileOutputStream(output_file);

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

        gzis.close();
        out.close();

        System.out.println("Done");

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return output_file;
}

From source file:org.apache.myfaces.shared_impl.util.StateUtils.java

public static byte[] decompress(byte[] bytes) {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[2048];
    int length;/*from w ww .  j  a  v a 2  s.c o m*/

    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        while ((length = gis.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }

        byte[] moreBytes = baos.toByteArray();
        baos.close();
        bais.close();
        gis.close();
        baos = null;
        bais = null;
        gis = null;
        return moreBytes;
    } catch (IOException e) {
        throw new FacesException(e);
    }
}

From source file:org.helm.notation2.tools.MolfileEncoder.java

/**
 * method to decompress the givenmolfile input
 *
 * @param str the molefile input can be in base64 format or in the gzipped
 *          Base64 format/*  w w w .  j  a v a2s . c  om*/
 * @return molfile
 * @throws EncoderException
 */
private static String decompress(String str) throws EncoderException {
    /* First base64 decode the string */
    String result = null;
    byte[] bytes = Base64.decode(str);
    GZIPInputStream zi = null;
    try {
        zi = new GZIPInputStream(new ByteArrayInputStream(bytes));
        InputStreamReader reader = new InputStreamReader(zi);
        BufferedReader in = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder();
        String read;
        while ((read = in.readLine()) != null) {
            sb.append(read + "\n");
        }

        String molfile = sb.toString();
        if (molfile.endsWith("\n$$$$\n")) {
            molfile = molfile.replace("\n$$$$\n", "");
        }
        reader.close();
        in.close();
        zi.close();
        return molfile;

    } catch (IOException e) {
        LOG.info("Molfile was not in gzipped");
        result = new String(bytes);
    } finally {
        IOUtils.closeQuietly(zi);
    }
    return result;

}

From source file:org.opendatakit.common.utils.WebUtils.java

/**
 * Decode a safeEncode() string./*w w w. j  av  a  2  s  .  co m*/
 * 
 * @param encodedWebsafeString
 * @return rawString
 */
public static String safeDecode(String encodedWebsafeString) {
    if (encodedWebsafeString == null || encodedWebsafeString.length() == 0) {
        return encodedWebsafeString;
    }

    try {
        ByteArrayInputStream in = new ByteArrayInputStream(
                Base64.decodeBase64(encodedWebsafeString.getBytes(CharEncoding.UTF_8)));
        GZIPInputStream gzip = new GZIPInputStream(in);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int ch = gzip.read();
        while (ch >= 0) {
            out.write(ch);
            ch = gzip.read();
        }
        gzip.close();
        out.flush();
        out.close();
        return new String(out.toByteArray(), CharEncoding.UTF_8);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    }
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Decompresses the GZIP file back to its original form.
 *
 * @param aGzipPathFileName The GZIP file name.
 * @param anOutputPathFileName The output file name.
 *
 * @throws IOException Related to opening the file streams and
 * related read/write operations./*  w w w  . ja v  a2s  .c o m*/
 */
public static void ungzipFile(String aGzipPathFileName, String anOutputPathFileName) throws IOException {
    int byteCount;

    FileInputStream fileInputStream = new FileInputStream(aGzipPathFileName);
    GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
    FileOutputStream fileOutputStream = new FileOutputStream(anOutputPathFileName);

    byte[] ioBuf = new byte[FILE_IO_BUFFER_SIZE];
    byteCount = gzipInputStream.read(ioBuf);
    while (byteCount > 0) {
        fileOutputStream.write(ioBuf, 0, byteCount);
        byteCount = gzipInputStream.read(ioBuf);
    }

    fileOutputStream.close();
    gzipInputStream.close();
}

From source file:org.wso2.carbon.analytics.spark.core.util.CompressedEventAnalyticsUtils.java

/**
 * Decompress a compressed event string.
 * //from   w ww. jav  a 2  s. c  o  m
 * @param str   Compressed string
 * @return      Decompressed string
 */
public static ByteArrayInputStream decompress(String str) {
    ByteArrayInputStream byteInputStream = null;
    GZIPInputStream gzipInputStream = null;
    try {
        byteInputStream = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(str));
        gzipInputStream = new GZIPInputStream(byteInputStream);
        byte[] unzippedBytes = IOUtils.toByteArray(gzipInputStream);
        return new ByteArrayInputStream(unzippedBytes);
    } catch (IOException e) {
        throw new RuntimeException("Error occured while decompressing events string: " + e.getMessage(), e);
    } finally {
        try {
            if (byteInputStream != null) {
                byteInputStream.close();
            }
            if (gzipInputStream != null) {
                gzipInputStream.close();
            }
        } catch (IOException e) {
            log.error("Error occured while closing streams: " + e.getMessage(), e);
        }
    }
}

From source file:com.ning.billing.beatrix.osgi.SetupBundleWithAssertion.java

private static File unGzip(final File inputFile, final File outputDir) throws IOException {

    GZIPInputStream in = null;
    FileOutputStream out = null;/*  w ww .j  a v  a  2 s  . co m*/

    try {
        final File outputFile = new File(outputDir,
                inputFile.getName().substring(0, inputFile.getName().length() - 3));

        in = new GZIPInputStream(new FileInputStream(inputFile));
        out = new FileOutputStream(outputFile);

        for (int c = in.read(); c != -1; c = in.read()) {
            out.write(c);
        }
        return outputFile;

    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}