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.intermine.api.lucene.KeywordSearch.java

private static LuceneIndexContainer restoreIndex(Database db)
        throws IOException, ClassNotFoundException, SQLException {

    long time = System.currentTimeMillis();
    InputStream is = MetadataManager.readLargeBinary(db, MetadataManager.SEARCH_INDEX);

    if (is == null) {
        LOG.warn("No search index stored in this DB.");
        return null;
    } else {//from ww  w  . j av  a  2 s  .c  o m
        GZIPInputStream gzipInput = new GZIPInputStream(is);
        ObjectInputStream objectInput = new ObjectInputStream(gzipInput);

        try {
            Object object = objectInput.readObject();

            if (object instanceof LuceneIndexContainer) {

                LOG.info("Successfully restored search index information" + " from database in "
                        + (System.currentTimeMillis() - time) + " ms");
                LOG.debug("Index: " + index);
                return (LuceneIndexContainer) object;
            } else {
                LOG.warn("Object from DB has wrong class:" + object.getClass().getName());
                return null;
            }
        } finally {
            objectInput.close();
            gzipInput.close();
            is.close();
        }
    }
}

From source file:com.mobilesorcery.sdk.builder.linux.PackageParser.java

/**
 * Extracts a package template and parses and replaces variables
 * in filenames and files.//from  w  w w. j av a2 s  .  c o  m
 *
 * @param o Output directory
 * @param i Input file
 *
 * @throws Exception If recursion is too deep, a variable isn't defined or
 *                   malformed meta data
 * @throws IOException Error reading inputstream
 * @throws ParseException Malformed JSON
 * @throws FileNotFoundException Could not open input file
 */
public void doProcessTarGZip(File o, File i)
        throws Exception, IOException, ParseException, FileNotFoundException {
    FileInputStream fis = new FileInputStream(i);
    GZIPInputStream gis = new GZIPInputStream(fis);
    TarArchiveInputStream tis = new TarArchiveInputStream(gis);

    // Remove any old data if any
    if (o.exists() == true)
        o.delete();

    // Find and parse meta data, this should always be the
    // first file, but it can' be assumed
    while (true) {
        ArchiveEntry e = tis.getNextEntry();
        if (e == null)
            break;

        if (e.getName().equals(".meta/.meta") == false)
            continue;

        doParseMeta(tis);
        break;
    }

    // Reset input
    tis.close();
    gis.close();
    fis.close();
    fis = new FileInputStream(i);
    gis = new GZIPInputStream(fis);
    tis = new TarArchiveInputStream(gis);

    // Process and extract files
    while (true) {
        File f;
        ArchiveEntry e = tis.getNextEntry();

        if (e == null)
            break;

        // Check if it's a script that we need to load and parse
        if (e.getName().contains(".meta") == true) {
            if (m_scriptMap.containsKey(e.getName()) == true) {
                String name = m_scriptMap.get(e.getName());
                String script = m_varResolver.doParseStream(tis);
                m_scriptMap.put(name, script);
                m_scriptMap.remove(e.getName());
            }
            continue;
        }

        // Store its permissions
        String n = m_varResolver.doResolveString(e.getName());
        m_filemodeMap.put(n, ((TarArchiveEntry) e).getMode());

        // Directory ?
        f = new File(o, n);
        if (e.isDirectory() == true) {
            if (f.exists() == false)
                f.mkdirs();
            continue;
        }

        // It's a file
        if (m_parseSet.contains(e.getName()) == true)
            m_varResolver.doParseCopyStream(f, tis);
        else
            BuilderUtil.getInstance().copyInputStreamToFile(f, tis, e.getSize());
    }
}

From source file:org.pentaho.di.trans.steps.sort.SortRows.java

Object[] getBuffer() throws KettleValueException {
    Object[] retval;// w  w w.  j  a  v  a  2  s .co m

    // Open all files at once and read one row from each file...
    if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) {
        if (log.isBasic()) {
            logBasic(BaseMessages.getString(PKG, "SortRows.Basic.OpeningTempFiles", data.files.size()));
        }

        try {
            for (int f = 0; f < data.files.size() && !isStopped(); f++) {
                FileObject fileObject = data.files.get(f);
                String filename = KettleVFS.getFilename(fileObject);
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SortRows.Detailed.OpeningTempFile", filename));
                }
                InputStream fi = KettleVFS.getInputStream(fileObject);
                DataInputStream di;
                data.fis.add(fi);
                if (data.compressFiles) {
                    di = getDataInputStream(new GZIPInputStream(new BufferedInputStream(fi)));
                } else {
                    di = new DataInputStream(new BufferedInputStream(fi, 50000));
                }
                data.dis.add(di);

                // How long is the buffer?
                int buffersize = data.bufferSizes.get(f);

                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SortRows.Detailed.FromFileExpectingRows", filename,
                            buffersize));
                }

                if (buffersize > 0) {
                    Object[] row = data.outputRowMeta.readData(di);
                    data.rowbuffer.add(row); // new row from input stream
                    data.tempRows.add(new RowTempFile(row, f));
                }
            }

            // Sort the data row buffer
            Collections.sort(data.tempRows, data.comparator);
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "SortRows.Error.ErrorReadingBackTempFiles"), e);
        }
    }

    if (data.files.size() == 0) {
        // read from in-memory processing

        if (data.getBufferIndex < data.buffer.size()) {
            retval = data.buffer.get(data.getBufferIndex);
            data.getBufferIndex++;
        } else {
            retval = null;
        }
    } else {
        // read from disk processing

        if (data.rowbuffer.size() == 0) {
            retval = null;
        } else {
            // We now have "filenr" rows waiting: which one is the smallest?
            //
            if (log.isRowLevel()) {
                for (int i = 0; i < data.rowbuffer.size() && !isStopped(); i++) {
                    Object[] b = data.rowbuffer.get(i);
                    logRowlevel(BaseMessages.getString(PKG, "SortRows.RowLevel.PrintRow", i,
                            data.outputRowMeta.getString(b)));
                }
            }

            RowTempFile rowTempFile = data.tempRows.remove(0);
            retval = rowTempFile.row;
            int smallest = rowTempFile.fileNumber;

            // now get another Row for position smallest

            FileObject file = data.files.get(smallest);
            DataInputStream di = data.dis.get(smallest);
            InputStream fi = data.fis.get(smallest);

            try {
                Object[] row2 = data.outputRowMeta.readData(di);
                RowTempFile extra = new RowTempFile(row2, smallest);

                int index = Collections.binarySearch(data.tempRows, extra, data.comparator);
                if (index < 0) {
                    data.tempRows.add(index * (-1) - 1, extra);
                } else {
                    data.tempRows.add(index, extra);
                }
            } catch (KettleFileException fe) { // empty file or EOF mostly
                GZIPInputStream gzfi = (data.compressFiles) ? data.gzis.get(smallest) : null;
                try {
                    di.close();
                    fi.close();
                    if (gzfi != null) {
                        gzfi.close();
                    }
                    file.delete();
                } catch (IOException e) {
                    logError(BaseMessages.getString(PKG, "SortRows.Error.UnableToCloseFile", smallest,
                            file.toString()));
                    setErrors(1);
                    stopAll();
                    return null;
                }

                data.files.remove(smallest);
                data.dis.remove(smallest);
                data.fis.remove(smallest);

                if (gzfi != null) {
                    data.gzis.remove(smallest);
                }

                // Also update all file numbers in in data.tempRows if they are larger
                // than smallest.
                //
                for (RowTempFile rtf : data.tempRows) {
                    if (rtf.fileNumber > smallest) {
                        rtf.fileNumber--;
                    }
                }
            } catch (SocketTimeoutException e) {
                throw new KettleValueException(e); // should never happen on local files
            }
        }
    }
    return retval;
}

From source file:com.github.tomakehurst.wiremock.StandaloneAcceptanceTest.java

/**
 * Decompress the binary gzipped content into a String.
 *
 * @param content the gzipped content to decompress
 * @return decompressed String./*from ww  w .j a  va2  s . c o  m*/
 */
private String decompress(byte[] content) {
    GZIPInputStream gin = null;
    try {
        gin = new GZIPInputStream(new ByteArrayInputStream(content));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buf = new byte[8192];

        int read = -1;
        while ((read = gin.read(buf)) != -1) {
            baos.write(buf, 0, read);
        }

        return new String(baos.toByteArray(), Charset.forName(UTF_8.name()));

    } catch (IOException e) {
        return null;
    } finally {
        if (gin != null)
            try {
                gin.close();
            } catch (IOException e) {

            }
    }
}

From source file:fr.gouv.finances.cp.xemelios.importers.batch.BatchRealImporter.java

private ImportContent files(final String extension, final String titreEtat) {
    ImportContent ic = new ImportContent();
    Vector<File> ret = new Vector<File>();
    ret.addAll(files);//from  www . j a  va  2 s  . co m
    // on regarde si l'un des fichiers a importer est un zip
    for (int i = 0; i < ret.size(); i++) {
        if (ret.get(i).getName().toLowerCase().endsWith(".zip")) {
            if (ret.get(i).exists()) {
                ZipFile zf = null;
                try {
                    zf = new ZipFile(ret.get(i));
                    for (Enumeration<? extends ZipEntry> enumer = zf.entries(); enumer.hasMoreElements();) {
                        ZipEntry ze = enumer.nextElement();
                        if (!ze.isDirectory()) {
                            String fileName = ze.getName();
                            String entryName = fileName.toLowerCase();
                            fileName = fileName.replace(File.pathSeparatorChar, '_')
                                    .replace(File.separatorChar, '_').replace(':', '|').replace('\'', '_')
                                    .replace('/', '_');
                            logger.debug(entryName);
                            if (PJRef.isPJ(ze)) {
                                PJRef pj = new PJRef(ze);
                                File tmpFile = pj.writeTmpFile(FileUtils.getTempDir(), zf);
                                ic.pjs.add(pj);
                                filesToDrop.add(tmpFile);
                            } else if ((entryName.endsWith(extension.toLowerCase())
                                    || entryName.endsWith(".xml")) && !fileName.startsWith("_")) {
                                // on decompresse le fichier dans le
                                // repertoire temporaire, comme ca il sera
                                // supprime en quittant
                                InputStream is = zf.getInputStream(ze);
                                BufferedInputStream bis = new BufferedInputStream(is);
                                File output = new File(FileUtils.getTempDir(), fileName);
                                BufferedOutputStream bos = new BufferedOutputStream(
                                        new FileOutputStream(output));
                                byte[] buffer = new byte[1024];
                                int read = bis.read(buffer);
                                while (read > 0) {
                                    bos.write(buffer, 0, read);
                                    read = bis.read(buffer);
                                }
                                bos.flush();
                                bos.close();
                                bis.close();
                                ic.filesToImport.add(output);
                                filesToDrop.add(output);
                            }
                        }
                    }
                    zf.close();
                } catch (ZipException zEx) {
                    System.out.println(
                            "Le fichier " + ret.get(i).getName() + " n'est pas une archive ZIP valide.");
                } catch (IOException ioEx) {
                    ioEx.printStackTrace();
                } finally {
                    if (zf != null) {
                        try {
                            zf.close();
                        } catch (Throwable t) {
                        }
                    }
                }
            }
        } else if (ret.get(i).getName().toLowerCase().endsWith(".gz")) {
            try {
                String fileName = ret.get(i).getName();
                fileName = fileName.substring(0, fileName.length() - 3);
                File output = new File(FileUtils.getTempDir(), fileName);
                GZIPInputStream gis = new GZIPInputStream(new FileInputStream(ret.get(i)));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
                byte[] buffer = new byte[1024];
                int read = gis.read(buffer);
                while (read > 0) {
                    bos.write(buffer, 0, read);
                    read = gis.read(buffer);
                }
                bos.flush();
                bos.close();
                gis.close();
                ic.filesToImport.add(output);
                filesToDrop.add(output);
            } catch (IOException ioEx) {
                // nothing to do
            }
        } else {
            ic.filesToImport.add(ret.get(i));
            // dans ce cas l, on ne le supprime pas
        }
    }
    return ic;
}

From source file:weka.gui.beans.TimeSeriesForecasting.java

protected static final byte[] decodeFromBase64(String string) throws Exception {

    byte[] bytes;
    if (string == null) {
        bytes = new byte[] {};
    } else {//from w w w . jav  a2  s .c  o m
        bytes = Base64.decodeBase64(string.getBytes());
    }
    if (bytes.length > 0) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        BufferedInputStream bi = new BufferedInputStream(gzip);
        byte[] 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();
    }

    return bytes;
}

From source file:weka.core.converters.cassandra.CassandraColumnMetaData.java

protected static final byte[] decodeFromBase64(String string) throws Exception {
    byte[] bytes;
    if (string == null) {
        bytes = new byte[] {};
    } else {/* w  ww.j  a v a 2 s.  c om*/
        bytes = Base64.decodeBase64(string.getBytes());
    }
    if (bytes.length > 0) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        BufferedInputStream bi = new BufferedInputStream(gzip);
        byte[] 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();
    }

    return bytes;
}

From source file:net.sf.ehcache.constructs.web.PageInfoTest.java

private byte[] ungzip5(final byte[] gzip) throws IOException {
    int size = 0;
    int counter = 0;
    GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(gzip));
    int bytesRead = 0;
    byte[] buffer = new byte[32768];
    byte[] tempBuffer = new byte[4096];
    counter = 0;//from  ww  w .j  a va2 s. co m
    while (bytesRead != -1) {
        bytesRead = gzipInputStream.read(tempBuffer);
        if (bytesRead != -1) {
            if (buffer.length < counter + bytesRead) {
                byte[] newBuffer = new byte[buffer.length + 32768];
                System.arraycopy(buffer, 0, newBuffer, 0, counter);
                buffer = newBuffer;
            }
            System.arraycopy(tempBuffer, 0, buffer, counter, bytesRead);
            counter += bytesRead;
        }
    }
    gzipInputStream.close();
    size = counter;
    byte[] unzipped = new byte[size];
    System.arraycopy(buffer, 0, unzipped, 0, counter);
    return unzipped;
}

From source file:parquet.hadoop.ParquetInputSplit.java

String decompressString(String str) {
    byte[] decoded = Base64.decodeBase64(str);
    ByteArrayInputStream obj = new ByteArrayInputStream(decoded);
    GZIPInputStream gzip = null;
    String outStr = "";
    try {/*from  w ww.  j  a  v a 2  s.  co m*/
        gzip = new GZIPInputStream(obj);
        BufferedReader reader = new BufferedReader(new InputStreamReader(gzip, "UTF-8"));
        char[] buffer = new char[1024];
        int n = 0;
        StringBuilder sb = new StringBuilder();
        while (-1 != (n = reader.read(buffer))) {
            sb.append(buffer, 0, n);
        }
        outStr = sb.toString();
    } catch (IOException e) {
        // Not really sure how we can get here. I guess the best thing to do is to croak.
        LOG.error("Unable to uncompress InputSplit string " + str, e);
        throw new RuntimeException("Unable to uncompress InputSplit String", e);
    } finally {
        if (null != gzip) {
            try {
                gzip.close();
            } catch (IOException e) {
                LOG.error("Unable to uncompress InputSplit string " + str, e);
                throw new RuntimeException("Unable to uncompress InputSplit String", e);
            }
        }
    }
    return outStr;
}

From source file:org.pentaho.di.core.xml.XMLHandler.java

/**
 * Convert a XML encoded binary string back to binary format
 *
 * @param string/*from ww w .  j  ava2  s  .  c  o m*/
 *          the (Byte64/GZip) encoded string
 * @return the decoded binary (byte[]) object
 * @throws KettleException
 *           In case there is a decoding error
 */
public static byte[] stringToBinary(String string) throws KettleException {
    try {
        byte[] bytes;
        if (string == null) {
            bytes = new byte[] {};
        } else {
            bytes = Base64.decodeBase64(string.getBytes());
        }
        if (bytes.length > 0) {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            GZIPInputStream gzip = new GZIPInputStream(bais);
            BufferedInputStream bi = new BufferedInputStream(gzip);
            byte[] 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();
        }

        return bytes;
    } catch (Exception e) {
        throw new KettleException("Error converting string to binary", e);
    }
}