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:ca.farrelltonsolar.classic.PVOutputUploader.java

private Bundle deserializeBundle(byte[] data) {
    Bundle bundle = null;/*from   ww  w  .j av a  2s  . c om*/
    final Parcel parcel = Parcel.obtain();
    try {
        final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        final byte[] buffer = new byte[1024];
        final GZIPInputStream zis = new GZIPInputStream(new ByteArrayInputStream(data));
        int len = 0;
        while ((len = zis.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        zis.close();
        parcel.unmarshall(byteBuffer.toByteArray(), 0, byteBuffer.size());
        parcel.setDataPosition(0);
        bundle = parcel.readBundle();
    } catch (IOException ex) {
        Log.w(getClass().getName(), String.format("deserializeBundle failed ex: %s", ex));
        bundle = null;
    } finally {
        parcel.recycle();
    }
    return bundle;
}

From source file:org.physical_web.physicalweb.Utils.java

/**
 * Out-of-place Gunzips from src to dest.
 * @param src file containing gzipped information.
 * @param dest file to place decompressed information.
 * @return File that has decompressed information.
 *///www.  j  av  a2s.co  m
public static File gunzip(File src, File dest) {

    byte[] buffer = new byte[1024];

    try {

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

        FileOutputStream out = new FileOutputStream(dest);

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

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

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

From source file:org.pentaho.di.cluster.HttpUtil.java

/**
 * Base 64 decode, unzip and extract text using {@link Const#XML_ENCODING} predefined charset value for byte-wise
 * multi-byte character handling./*from  w  w w  . j a v a2s.  c  o  m*/
 *
 * @param loggingString64
 *          base64 zip archive string representation
 * @return text from zip archive
 * @throws IOException
 */
public static String decodeBase64ZippedString(String loggingString64) throws IOException {
    if (loggingString64 == null || loggingString64.isEmpty()) {
        return "";
    }
    StringWriter writer = new StringWriter();
    // base 64 decode
    byte[] bytes64 = Base64.decodeBase64(loggingString64.getBytes());
    // unzip to string encoding-wise
    ByteArrayInputStream zip = new ByteArrayInputStream(bytes64);

    GZIPInputStream unzip = null;
    InputStreamReader reader = null;
    BufferedInputStream in = null;
    try {
        unzip = new GZIPInputStream(zip, HttpUtil.ZIP_BUFFER_SIZE);
        in = new BufferedInputStream(unzip, HttpUtil.ZIP_BUFFER_SIZE);
        // PDI-4325 originally used xml encoding in servlet
        reader = new InputStreamReader(in, Const.XML_ENCODING);
        writer = new StringWriter();

        // use same buffer size
        char[] buff = new char[HttpUtil.ZIP_BUFFER_SIZE];
        for (int length = 0; (length = reader.read(buff)) > 0;) {
            writer.write(buff, 0, length);
        }
    } finally {
        // close resources
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                // Suppress
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // Suppress
            }
        }
        if (unzip != null) {
            try {
                unzip.close();
            } catch (IOException e) {
                // Suppress
            }
        }
    }
    return writer.toString();
}

From source file:forge.quest.io.QuestDataIO.java

/**
 * <p>/* ww w. j  a v  a2 s  .c  o m*/
 * loadData.
 * </p>
 *
 * @param xmlSaveFile
 *            &emsp; {@link java.io.File}
 * @return {@link forge.quest.data.QuestData}
 */
public static QuestData loadData(final File xmlSaveFile) {
    try {
        QuestData data;

        final GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
        final StringBuilder xml = new StringBuilder();
        final char[] buf = new char[1024];
        final InputStreamReader reader = new InputStreamReader(zin);
        while (reader.ready()) {
            final int len = reader.read(buf);
            if (len == -1) {
                break;
            } // when end of stream was reached
            xml.append(buf, 0, len);
        }

        zin.close();

        String bigXML = xml.toString();
        data = (QuestData) QuestDataIO.getSerializer(true).fromXML(bigXML);

        if (data.getVersionNumber() != QuestData.CURRENT_VERSION_NUMBER) {
            try {
                QuestDataIO.updateSaveFile(data, bigXML, xmlSaveFile.getName().replace(".dat", ""));
            } catch (final Exception e) {
                //BugReporter.reportException(e);
                throw new RuntimeException(e);
            }
        }

        return data;
    } catch (final Exception ex) {
        //BugReporter.reportException(ex, "Error loading Quest Data");
        throw new RuntimeException(ex);
    }
}

From source file:uk.ac.cam.cl.dtg.picky.parser.pcap2.PcapParser.java

@Override
public void open(File file) throws IOException {
    this.file = file;
    LOG.info("opening " + file);

    tmpFolder.mkdirs();/*w  ww . ja v  a  2 s  .c o m*/

    try {
        if (file.getName().toLowerCase().endsWith(".gz")) {
            LOG.info("decompressing " + file);
            tempFile = new File(tmpFolder, file.getName() + ".tmp");
            tempFile.delete();
            tempFile.deleteOnExit();

            GZIPInputStream stream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
            FileUtils.copyInputStreamToFile(stream, tempFile);
            stream.close();

            pcap = Pcaps.openOffline(tempFile.getAbsolutePath());
        } else {
            pcap = Pcaps.openOffline(file.getAbsolutePath());
        }

        globalHeader = readGlobalHeader(file);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.panet.imeta.www.SlaveServerJobStatus.java

public SlaveServerJobStatus(Node jobStatusNode) {
    this();//from ww  w .  j av a 2  s.  co m
    jobName = XMLHandler.getTagValue(jobStatusNode, "jobname");
    statusDescription = XMLHandler.getTagValue(jobStatusNode, "status_desc");
    errorDescription = XMLHandler.getTagValue(jobStatusNode, "error_desc");

    String loggingString64 = XMLHandler.getTagValue(jobStatusNode, "logging_string");

    // This is a Base64 encoded GZIP compressed stream of data.
    //
    try {
        byte[] bytes = new byte[] {};
        if (loggingString64 != null)
            bytes = Base64.decodeBase64(loggingString64.getBytes());
        if (bytes.length > 0) {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            GZIPInputStream gzip = new GZIPInputStream(bais);
            int c;
            StringBuffer buffer = new StringBuffer();
            while ((c = gzip.read()) != -1)
                buffer.append((char) c);
            gzip.close();
            loggingString = buffer.toString();
        } else {
            loggingString = "";
        }
    } catch (IOException e) {
        loggingString = "Unable to decode logging from remote server : " + e.toString() + Const.CR
                + Const.getStackTracker(e) + Const.CR;
    }

    // get the result object, if there is any...
    //
    Node resultNode = XMLHandler.getSubNode(jobStatusNode, Result.XML_TAG);
    if (resultNode != null) {
        try {
            result = new Result(resultNode);
        } catch (IOException e) {
            loggingString += "Unable to serialize result object as XML" + Const.CR + Const.getStackTracker(e)
                    + Const.CR;
        }
    }
}

From source file:sequential.analysis.SequentialAnalysis.java

public void unGunzipFile(String compressedFile, String decompressedFile) {
    byte[] buffer = new byte[1024];
    try {//from w  w  w .  j  a v  a  2  s  . c o m
        FileInputStream fileIn = new FileInputStream(compressedFile);
        GZIPInputStream gZIPInputStream = new GZIPInputStream(fileIn);
        FileOutputStream fileOutputStream = new FileOutputStream(decompressedFile);
        int bytes_read;
        while ((bytes_read = gZIPInputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, bytes_read);
        }
        gZIPInputStream.close();
        fileOutputStream.close();
        getCsvData(decompressedFile);
        System.out.println("Success!");
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:net.yacy.kelondro.util.FileUtils.java

/**
 * This function determines if a byte array is gzip compressed and uncompress it
 *
 * @param source properly gzip compressed byte array
 * @return uncompressed byte array/* w  w w. j ava2 s. c o m*/
 * @throws IOException
 */
public static byte[] uncompressGZipArray(byte[] source) throws IOException {
    if (source == null) {
        return null;
    }

    // support of gzipped data (requested by roland)
    /* "Bitwise OR of signed byte value
     *
     * [...] Values loaded from a byte array are sign extended to 32 bits before
     * any any bitwise operations are performed on the value. Thus, if b[0]
     * contains the value 0xff, and x is initially 0, then the code ((x <<
     * 8) | b[0]) will sign extend 0xff to get 0xffffffff, and thus give the
     * value 0xffffffff as the result. [...]" findbugs description of BIT_IOR_OF_SIGNED_BYTE
     */
    if ((source.length > 1) && (((source[1] << 8) | (source[0] & 0xff)) == GZIPInputStream.GZIP_MAGIC)) {
        System.out.println("DEBUG: uncompressGZipArray - uncompressing source");
        try {
            final ByteArrayInputStream byteInput = new ByteArrayInputStream(source);
            final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(source.length / 5);
            final GZIPInputStream zippedContent = new GZIPInputStream(byteInput);
            final byte[] data = new byte[1024];
            int read = 0;

            // reading gzip file and store it uncompressed
            while ((read = zippedContent.read(data, 0, 1024)) != -1) {
                byteOutput.write(data, 0, read);
            }
            zippedContent.close();
            byteOutput.close();

            source = byteOutput.toByteArray();
        } catch (final Exception e) {
            if (!e.getMessage().equals("Not in GZIP format")) {
                throw new IOException(e.getMessage());
            }
        }
    }

    return source;
}

From source file:org.hupo.psi.mi.psicquic.ws.IndexBasedPsicquicRestServiceTest.java

@Test
public void testGetByQuery_bin() throws Exception {
    ResponseImpl response = (ResponseImpl) service.getByQuery("FANCD1", "tab25-bin", "0",
            String.valueOf(Integer.MAX_VALUE), "n");

    PsicquicStreamingOutput pso = ((GenericEntity<PsicquicStreamingOutput>) response.getEntity()).getEntity();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    pso.write(baos);//from  ww  w.ja  v a2  s  .  c o m

    // gunzip the output
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    GZIPInputStream gzipInputStream = new GZIPInputStream(bais);

    ByteArrayOutputStream mitabOut = new ByteArrayOutputStream();

    byte[] buf = new byte[1024];
    int len;
    while ((len = gzipInputStream.read(buf)) > 0)
        mitabOut.write(buf, 0, len);

    gzipInputStream.close();
    mitabOut.close();

    Assert.assertEquals(12, mitabOut.toString().split("\n").length);
}