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.gephi.desktop.importer.DesktopImportControllerUI.java

/**
 * Uncompress a GZIP file./* w  w w  .j  av  a2s .c o  m*/
 */
private static File getGzFile(FileObject in, File out, boolean isTar) throws IOException {

    // Stream buffer
    final int BUFF_SIZE = 8192;
    final byte[] buffer = new byte[BUFF_SIZE];

    GZIPInputStream inputStream = null;
    FileOutputStream outStream = null;

    try {
        inputStream = new GZIPInputStream(new FileInputStream(in.getPath()));
        outStream = new FileOutputStream(out);

        if (isTar) {
            // Read Tar header
            int remainingBytes = readTarHeader(inputStream);

            // Read content
            ByteBuffer bb = ByteBuffer.allocateDirect(4 * BUFF_SIZE);
            byte[] tmpCache = new byte[BUFF_SIZE];
            int nRead, nGet;
            while ((nRead = inputStream.read(tmpCache)) != -1) {
                if (nRead == 0) {
                    continue;
                }
                bb.put(tmpCache);
                bb.position(0);
                bb.limit(nRead);
                while (bb.hasRemaining() && remainingBytes > 0) {
                    nGet = Math.min(bb.remaining(), BUFF_SIZE);
                    nGet = Math.min(nGet, remainingBytes);
                    bb.get(buffer, 0, nGet);
                    outStream.write(buffer, 0, nGet);
                    remainingBytes -= nGet;
                }
                bb.clear();
            }
        } else {
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, len);
            }
        }
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outStream != null) {
            outStream.close();
        }
    }

    return out;
}

From source file:edu.dfci.cccb.mev.dataset.domain.gzip.GzipTsvInput.java

@Override
public InputStream input() throws IOException {
    if (log.isDebugEnabled())
        log.debug(url);/*  w  ww . j  a  v a2 s .  c om*/

    GZIPInputStream zis = null;
    BufferedOutputStream dest = null;
    BufferedInputStream bis = null;
    File unzipped = new TemporaryFile();
    try {
        URLConnection urlc = url.openConnection();
        zis = new GZIPInputStream(urlc.getInputStream());

        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk

        FileOutputStream fos = new FileOutputStream(unzipped);
        if (log.isDebugEnabled())
            log.debug("Unzipping SOFT file to " + unzipped.getAbsolutePath());

        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        return new FileInputStream(unzipped);

    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioe) {
            }
        }
        if (dest != null) {
            try {
                dest.close();
            } catch (IOException ioe) {
            }
        }
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException ioe) {
            }
        }
    }

}

From source file:de.zib.scalaris.TransactionSingleOpTest.java

/**
 * Tests how long it takes to read a large string with different compression
 * schemes./*from  ww w  .  j a v a2s  .co m*/
 *
 * @param compressed
 *            how to compress
 * @param key
 *            the key to append to the {@link #testTime}
 *
 * @throws ConnectionException
 * @throws UnknownException
 * @throws AbortException
 * @throws NotFoundException
 * @throws IOException
 */
protected void testReadLargeString(final int compression, final String key)
        throws ConnectionException, UnknownException, AbortException, NotFoundException, IOException {
    final StringBuilder sb = new StringBuilder(testData.length * 8 * 100);
    for (int i = 0; i < 100; ++i) {
        for (final String data : testData) {
            sb.append(data);
        }
    }
    final String expected = sb.toString();

    final TransactionSingleOp conn = new TransactionSingleOp();
    conn.setCompressed(true);
    switch (compression) {
    case 1:
        conn.setCompressed(false);
    case 2:
        conn.write(testTime + key, expected);
        break;
    case 3:
        conn.setCompressed(false);
    case 4:
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final GZIPOutputStream gos = new GZIPOutputStream(bos);
        gos.write(expected.getBytes("UTF-8"));
        gos.flush();
        gos.close();
        conn.write(testTime + key, new Base64(0).encodeToString(bos.toByteArray()));
        break;
    default:
        return;
    }

    try {
        for (int i = 0; i < 500; ++i) {
            String actual = conn.read(testTime + key).stringValue();
            if (compression >= 3) {
                final byte[] packed = new Base64(0).decode(actual);
                final ByteArrayOutputStream unpacked = new ByteArrayOutputStream();
                final ByteArrayInputStream bis = new ByteArrayInputStream(packed);
                final GZIPInputStream gis = new GZIPInputStream(bis);
                final byte[] bbuf = new byte[256];
                int read = 0;
                while ((read = gis.read(bbuf)) >= 0) {
                    unpacked.write(bbuf, 0, read);
                }
                gis.close();
                actual = new String(unpacked.toString("UTF-8"));
            }
            assertEquals(expected, actual);
        }
    } finally {
        conn.closeConnection();
    }
}

From source file:agilejson.Base64.java

/**
 * Decodes data from Base64 notation, automatically detecting gzip-compressed
 * data and decompressing it.//ww w  .  j a v  a2 s . com
 * 
 * @param s the string to decode
 * @param options
 * @see Base64#URL_SAFE
 * @see Base64#ORDERED
 * @return the decoded data
 * @since 1.4
 */
public static byte[] decode(String s, int options) {
    byte[] bytes = null;
    try {
        bytes = s.getBytes(PREFERRED_ENCODING);

    } catch (UnsupportedEncodingException uee) {
        bytes = s.getBytes();
    } // end catch

    // Decode

    bytes = decode(bytes, 0, bytes.length, options);

    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)

    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 = 0; (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
    } // end if: bytes.length >= 2

    return bytes;
}

From source file:org.apache.hadoop.hbase.util.Base64.java

/**
 * Decodes data from Base64 notation, automatically detecting gzip-compressed
 * data and decompressing it./*  ww  w  . ja v a2 s. c o  m*/
 *
 * @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);

    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)

    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
    } // end if: bytes.length >= 2

    return bytes;
}

From source file:configuration.Util.java

/**
 * This un-GZIP a the input_filename to the output_filename
 * @param input_filename/*from w  ww . ja v  a  2 s  .  co m*/
 * @param output_filename
 * @return True if success
 */
public static boolean ungzip(String input_filename, String output_filename) {
    try {
        GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(input_filename));
        OutputStream out = new FileOutputStream(output_filename);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = gzipInputStream.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        gzipInputStream.close();
        out.close();
        return true;
    } catch (Exception e) {
        System.out.println("ungzip Failed!");
        System.out.println(e);
        e.printStackTrace();
        return false;
    }
}

From source file:XmldapCertsAndKeys.java

/**
 * Decodes data from Base64 notation, automatically detecting
 * gzip-compressed data and decompressing it.
 * //from w ww .j  a v  a  2  s  . c om
 * @param s
 *            the string to decode
 * @return the decoded data
 * @since 1.4
 */
public static byte[] decode(String s) {
    byte[] bytes;
    try {
        bytes = s.getBytes(PREFERRED_ENCODING);
    } // end try
    catch (java.io.UnsupportedEncodingException uee) {
        bytes = s.getBytes();
    } // end catch
      // </change>

    // Decode
    bytes = decode(bytes, 0, bytes.length);

    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)
    if (bytes != null && bytes.length >= 4) {

        int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
        if (java.util.zip.GZIPInputStream.GZIP_MAGIC == head) {
            java.io.ByteArrayInputStream bais = null;
            java.util.zip.GZIPInputStream gzis = null;
            java.io.ByteArrayOutputStream baos = null;
            byte[] buffer = new byte[2048];
            int length = 0;

            try {
                baos = new java.io.ByteArrayOutputStream();
                bais = new java.io.ByteArrayInputStream(bytes);
                gzis = new java.util.zip.GZIPInputStream(bais);

                while ((length = gzis.read(buffer)) >= 0) {
                    baos.write(buffer, 0, length);
                } // end while: reading input

                // No error? Get new bytes.
                bytes = baos.toByteArray();

            } // end try
            catch (java.io.IOException e) {
                // Just return originally-decoded bytes
            } // end catch
            finally {
                try {
                    baos.close();
                } catch (Exception e) {
                }
                try {
                    gzis.close();
                } catch (Exception e) {
                }
                try {
                    bais.close();
                } catch (Exception e) {
                }
            } // end finally

        } // end if: gzipped
    } // end if: bytes.length >= 2

    return bytes;
}

From source file:InstallJars.java

public void installGZip(URLConnection conn, String target, boolean doExpand, boolean doRun) throws IOException {
    // doRun not used on htis type
    if (doExpand) {
        String ctype = conn.getContentType();
        if (!ctype.equals("application/x-tar")) {
            throw new IllegalArgumentException("Unkexpected content type - " + ctype);
        }/*ww w  .  j a va 2s.  c  o m*/
        print("Expanding GZIP file to " + target + " from " + conn.getURL().toExternalForm());
        prepDirs(target, false);
        GZIPInputStream zis = new GZIPInputStream(
                new BufferedInputStream(conn.getInputStream(), BLOCK_SIZE * BLOCK_COUNT));
        try {
            // BufferedOutputStream os = new BufferedOutputStream(new
            // FileOutputStream(target), BLOCK_SIZE * BLOCK_COUNT);
            // try {
            // byte[] buf = new byte[bufferSize];
            // for (int size = zis.read(buf, 0, buf.length), count = 0;
            // size >= 0;
            // size = zis.read(buf, 0, buf.length), count++) {
            // //if (count % 4 == 0) print(".");
            // os.write(buf, 0, size);
            // }
            // }
            // finally {
            // try { os.flush(); os.close(); } catch (IOException ioe) {}
            // }
            pumpGZip(target, zis);
        } finally {
            try {
                zis.close();
            } catch (IOException ioe) {
            }
        }
        println();
    } else {
        print("Installing GZIP file " + target + " from " + conn.getURL().toExternalForm());
        copyStream(conn, target);
        println();
    }
}

From source file:com.sun.faces.renderkit.ResponseStateManagerImpl.java

public Object getTreeStructureToRestore(FacesContext context, String treeId) {
    StateManager stateManager = Util.getStateManager(context);

    Object structure = null;//from ww w  . j  av  a2  s.co  m
    Object state = null;
    ByteArrayInputStream bis = null;
    GZIPInputStream gis = null;
    ObjectInputStream ois = null;
    boolean compress = isCompressStateSet(context);

    Map requestParamMap = context.getExternalContext().getRequestParameterMap();

    String viewString = (String) requestParamMap.get(RIConstants.FACES_VIEW);
    if (viewString == null) {
        return null;
    }
    if (stateManager.isSavingStateInClient(context)) {
        byte[] bytes = Base64.decode(viewString.getBytes());
        try {
            bis = new ByteArrayInputStream(bytes);
            if (isCompressStateSet(context)) {
                if (log.isDebugEnabled()) {
                    log.debug("Deflating state before restoring..");
                }
                gis = new GZIPInputStream(bis);
                ois = new ApplicationObjectInputStream(gis);
            } else {
                ois = new ApplicationObjectInputStream(bis);
            }
            structure = ois.readObject();
            state = ois.readObject();
            Map requestMap = context.getExternalContext().getRequestMap();
            // store the state object temporarily in request scope until it is
            // processed by getComponentStateToRestore which resets it.
            requestMap.put(FACES_VIEW_STATE, state);
            bis.close();
            if (compress) {
                gis.close();
            }
            ois.close();
        } catch (java.io.OptionalDataException ode) {
            log.error(ode.getMessage(), ode);
            throw new FacesException(ode);
        } catch (java.lang.ClassNotFoundException cnfe) {
            log.error(cnfe.getMessage(), cnfe);
            throw new FacesException(cnfe);
        } catch (java.io.IOException iox) {
            log.error(iox.getMessage(), iox);
            throw new FacesException(iox);
        }
    } else {
        structure = viewString;
    }
    return structure;
}

From source file:org.intermine.web.search.ClassAttributes.java

private static void loadIndexFromDatabase(ObjectStore os, String path) {
    long time = System.currentTimeMillis();
    LOG.info("Attempting to restore search index from database...");
    if (os instanceof ObjectStoreInterMineImpl) {
        Database db = ((ObjectStoreInterMineImpl) os).getDatabase();
        try {//from   w w  w  .  jav  a 2 s .c  o  m
            InputStream is = MetadataManager.readLargeBinary(db, MetadataManager.SEARCH_INDEX);

            if (is != null) {
                GZIPInputStream gzipInput = new GZIPInputStream(is);
                ObjectInputStream objectInput = new ObjectInputStream(gzipInput);

                try {
                    Object object = objectInput.readObject();

                    if (object instanceof LuceneIndexContainer) {
                        index = (LuceneIndexContainer) object;

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

            if (index != null) {
                time = System.currentTimeMillis();
                LOG.info("Attempting to restore search directory from database...");
                is = MetadataManager.readLargeBinary(db, MetadataManager.SEARCH_INDEX_DIRECTORY);

                if (is != null) {
                    if ("FSDirectory".equals(index.getDirectoryType())) {
                        final int bufferSize = 2048;
                        File directoryPath = new File(path + File.separator + LUCENE_INDEX_DIR);
                        LOG.info("Directory path: " + directoryPath);

                        // make sure we start with a new index
                        if (directoryPath.exists()) {
                            String[] files = directoryPath.list();
                            for (int i = 0; i < files.length; i++) {
                                LOG.info("Deleting old file: " + files[i]);
                                new File(directoryPath.getAbsolutePath() + File.separator + files[i]).delete();
                            }
                        } else {
                            directoryPath.mkdir();
                        }

                        ZipInputStream zis = new ZipInputStream(is);
                        ZipEntry entry;
                        while ((entry = zis.getNextEntry()) != null) {
                            LOG.info("Extracting: " + entry.getName() + " (" + entry.getSize() + " MB)");

                            FileOutputStream fos = new FileOutputStream(
                                    directoryPath.getAbsolutePath() + File.separator + entry.getName());
                            BufferedOutputStream bos = new BufferedOutputStream(fos, bufferSize);

                            int count;
                            byte[] data = new byte[bufferSize];

                            while ((count = zis.read(data, 0, bufferSize)) != -1) {
                                bos.write(data, 0, count);
                            }

                            bos.flush();
                            bos.close();
                        }

                        FSDirectory directory = FSDirectory.open(directoryPath);
                        index.setDirectory(directory);

                        LOG.info("Successfully restored FS directory from database in "
                                + (System.currentTimeMillis() - time) + " ms");
                        time = System.currentTimeMillis();
                    } else if ("RAMDirectory".equals(index.getDirectoryType())) {
                        GZIPInputStream gzipInput = new GZIPInputStream(is);
                        ObjectInputStream objectInput = new ObjectInputStream(gzipInput);

                        try {
                            Object object = objectInput.readObject();

                            if (object instanceof FSDirectory) {
                                RAMDirectory directory = (RAMDirectory) object;
                                index.setDirectory(directory);

                                time = System.currentTimeMillis() - time;
                                LOG.info("Successfully restored RAM directory" + " from database in " + time
                                        + " ms");
                            }
                        } finally {
                            objectInput.close();
                            gzipInput.close();
                        }
                    } else {
                        LOG.warn("Unknown directory type specified: " + index.getDirectoryType());
                    }

                    LOG.info("Directory: " + index.getDirectory());
                } else {
                    LOG.warn("index is null!");
                }
            }
        } catch (ClassNotFoundException e) {
            LOG.error("Could not load search index", e);
        } catch (SQLException e) {
            LOG.error("Could not load search index", e);
        } catch (IOException e) {
            LOG.error("Could not load search index", e);
        }
    } else {
        LOG.error("ObjectStore is of wrong type!");
    }
}