Example usage for java.util.zip GZIPInputStream read

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

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:edu.umn.cs.spatialHadoop.nasa.HDFRasterLayer.java

@Override
public void readFields(DataInput in) throws IOException {
    super.readFields(in);
    this.timestamp = in.readLong();
    int length = in.readInt();
    byte[] serializedData = new byte[length];
    in.readFully(serializedData);/*  ww w . jav a  2 s .  co m*/
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
    GZIPInputStream gzis = new GZIPInputStream(bais);

    byte[] buffer = new byte[8];
    gzis.read(buffer);
    ByteBuffer bbuffer = ByteBuffer.wrap(buffer);
    int width = bbuffer.getInt();
    int height = bbuffer.getInt();
    // Reallocate memory only if needed
    if (width != this.getWidth() || height != this.getHeight()) {
        sum = new long[width][height];
        count = new long[width][height];
    }
    buffer = new byte[getHeight() * 2 * 8];
    for (int x = 0; x < getWidth(); x++) {
        int size = 0;
        while (size < buffer.length) {
            size += gzis.read(buffer, size, buffer.length - size);
        }
        bbuffer = ByteBuffer.wrap(buffer);
        for (int y = 0; y < getHeight(); y++) {
            sum[x][y] = bbuffer.getLong();
            count[x][y] = bbuffer.getLong();
        }
    }
}

From source file:com.sinelead.car.club.NewsFragment.java

public String getZipString(org.apache.http.HttpResponse response) {

    String resultString = "";
    ByteArrayBuffer bt = new ByteArrayBuffer(4096);
    try {/*w w w.  j  av a  2 s.  c om*/
        // Get
        HttpEntity he = response.getEntity();
        // 
        GZIPInputStream gis = new GZIPInputStream(he.getContent());
        int l;
        byte[] tmp = new byte[4096];
        while ((l = gis.read(tmp)) != -1) {
            bt.append(tmp, 0, l);
        }

        resultString = new String(bt.toByteArray(), "utf-8");
        // UTF-8

    } catch (Exception e) {
        Log.i("ERR", e.toString()); // 
    }

    return resultString;
}

From source file:com.kkbox.toolkit.internal.api.APIRequest.java

private InputStream getInputStreamFromHttpResponse() throws IOException {
    InputStream inputStream;/* w w  w .  j  a  va2 s. co m*/
    Header contentEncoding = response.getFirstHeader("Content-Encoding");
    if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        byte[] inputStreamBuffer = new byte[8192];
        int length;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        GZIPInputStream gZIPInputStream = new GZIPInputStream(
                new ByteArrayInputStream(EntityUtils.toByteArray(response.getEntity())));
        while ((length = gZIPInputStream.read(inputStreamBuffer)) >= 0) {
            byteArrayOutputStream.write(inputStreamBuffer, 0, length);
        }
        inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        gZIPInputStream.close();
        byteArrayOutputStream.close();
    } else {
        inputStream = response.getEntity().getContent();
    }
    return inputStream;
}

From source file:it.gnappuraz.netflixsubs.opensubtitles.api.OpenSubtitlesAPIClient.java

private byte[] gunzip(byte[] compressed) throws IOException {
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream out = new BufferedOutputStream(baos);
    byte[] buffer = new byte[1024];
    while (true) {
        synchronized (buffer) {
            int amountRead = gis.read(buffer);
            if (amountRead == -1) {
                break;
            }/*from www .  ja va  2 s . c  o m*/
            out.write(buffer, 0, amountRead);
        }
    }
    out.flush();
    out.close();
    return baos.toByteArray();
}

From source file:org.owasp.dependencycheck.data.update.nvd.DownloadTask.java

/**
 * Extracts the file contained in a gzip archive. The extracted file is placed in the exact same path as the file specified.
 *
 * @param file the archive file/*www.j av a 2  s . c  o m*/
 * @throws FileNotFoundException thrown if the file does not exist
 * @throws IOException thrown if there is an error extracting the file.
 */
private void extractGzip(File file) throws FileNotFoundException, IOException {
    final String originalPath = file.getPath();
    final File gzip = new File(originalPath + ".gz");
    if (gzip.isFile() && !gzip.delete()) {
        gzip.deleteOnExit();
    }
    if (!file.renameTo(gzip)) {
        throw new IOException("Unable to rename '" + file.getPath() + "'");
    }
    final File newfile = new File(originalPath);

    final byte[] buffer = new byte[4096];

    GZIPInputStream cin = null;
    FileOutputStream out = null;
    try {
        cin = new GZIPInputStream(new FileInputStream(gzip));
        out = new FileOutputStream(newfile);

        int len;
        while ((len = cin.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
    } finally {
        if (cin != null) {
            try {
                cin.close();
            } catch (IOException ex) {
                LOGGER.trace("ignore", ex);
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                LOGGER.trace("ignore", ex);
            }
        }
        if (gzip.isFile()) {
            FileUtils.deleteQuietly(gzip);
        }
    }
}

From source file:org.semanticscience.narf.structures.factories.tertiary.ExtractedTertiaryStructureFactory.java

/**
 * Get all nucleic acid tertiary structures produced using information
 * generated by the tertiary structure predictor.
 * /* w w  w  . j a v  a 2 s.  c o  m*/
 * @param aPdbFile
 *            a PDB structure file
 * @param commands
 *            set of commands to modify the execution of the annoator
 * @return a set of annotated nucleic acid tertiary structures
 * @throws FileNotFoundException
 *             if the PDB structure file does not exist
 * @throws IOException
 *             if any IO error occur reading the output of the tertiary
 *             structure annotator or writing the output of the tertiary
 *             structure annotator
 * @throws InvalidResidueException
 *             if any of the residues are invalid
 */
public Set<ExtractedTertiaryStructure> getStructures(File aPdbFile, String[] commands)
        throws FileNotFoundException, IOException, InvalidResidueException {

    if (!aPdbFile.exists() || aPdbFile.isDirectory()) {
        throw new FileNotFoundException("There is no PDB file with the name specified.");
    }

    String extension = PdbHelper.getFileExtension(aPdbFile);
    File directory = new File(FileUtils.getTempDirectoryPath() + "/pdb/");
    File pdbFile = new File(directory.getAbsolutePath() + "/" + aPdbFile.getName());
    FileUtils.copyFile(aPdbFile, pdbFile);
    // check if the file is compressed
    if (extension.equals("gz")) {
        GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(pdbFile));
        File gunzippedFile = new File(directory.getAbsolutePath() + pdbFile.getName().replace(".gz", ""));
        OutputStream out = new FileOutputStream(gunzippedFile);
        byte[] buf = new byte[1024];
        int len;
        while ((len = gzipInputStream.read(buf)) > 0)
            out.write(buf, 0, len);
        gzipInputStream.close();
        out.close();
        pdbFile.delete();
        pdbFile = gunzippedFile;
    }
    String pdbId = PdbHelper.findPdbId(pdbFile);
    int numberOfModels = PdbHelper.findNumberOfModels(pdbFile);
    Set<ExtractedTertiaryStructure> tertiaryAnnotatedStructures = new HashSet<ExtractedTertiaryStructure>();
    if (numberOfModels == 1) {
        File annotatedStructure = this.execute(pdbFile, commands);
        Map<String, Sequence> sequenceMap = this.parseSequences(pdbFile, annotatedStructure);
        Set<NucleotideInteraction> interactions = this.parseInteractions(sequenceMap, annotatedStructure);
        tertiaryAnnotatedStructures
                .add(new ExtractedTertiaryStructure(this, pdbFile, pdbId, 1, sequenceMap, interactions));
    } else {
        for (int modelNumber = 1; modelNumber <= numberOfModels; modelNumber++) {
            File modelFile = PdbHelper.extractModelFromPDB(pdbFile,
                    new File(FileUtils.getTempDirectoryPath() + "/pdb/"), pdbId, modelNumber);
            File outputFile = this.execute(modelFile, commands);
            Map<String, Sequence> sequenceMap = this.parseSequences(modelFile, outputFile);
            Set<NucleotideInteraction> interactions = this.parseInteractions(sequenceMap, outputFile);
            tertiaryAnnotatedStructures.add(new ExtractedTertiaryStructure(this, pdbFile, pdbId, modelNumber,
                    sequenceMap, interactions));
        }
    }
    return tertiaryAnnotatedStructures;
}

From source file:org.mitre.xtext.converters.ArchiveNavigator.java

private File gunzip(File theFile, String fname) throws IOException {

    GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(theFile));
    String outFilename = tempDir.toString() + File.separator + fname + ".tar";
    File outFile = new File(outFilename);
    OutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));

    byte[] buf = new byte[1024];
    int len;//from   w ww .  j av  a2s  .c  o  m
    while ((len = gzipInputStream.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    gzipInputStream.close();
    out.close();
    return outFile;
}

From source file:eu.stratuslab.storage.disk.resources.DisksResource.java

private long inflateFile(InputStream gzippedContents, File inflatedFile) {

    GZIPInputStream in = null;
    OutputStream out = null;/*  w  ww.  ja va  2s . c  o m*/

    try {

        in = new GZIPInputStream(gzippedContents);
        out = new FileOutputStream(inflatedFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

    } catch (IOException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage());
    } finally {
        FileUtils.closeIgnoringError(in);
        FileUtils.closeIgnoringError(out);
    }
    return DiskUtils.convertBytesToGibiBytes(inflatedFile.length());
}

From source file:autoupdater.FileDAO.java

/**
 * Untars and ungzips a .tar.gz file./*from   w  w  w .j a va 2s .co m*/
 *
 * @param in a {@code GZIPInputStream} of the file that needs to be
 * ungzipped and untarred
 * @param fileLocationOnDiskToDownloadTo the file to ungzip and untar to
 * @return true if successful
 * @throws IOException
 */
public boolean unGzipAndUntarFile(GZIPInputStream in, File fileLocationOnDiskToDownloadTo) throws IOException {

    try {
        FileOutputStream fos = new FileOutputStream(fileLocationOnDiskToDownloadTo);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        //close resources
        fos.close();
        in.close();

        untar(fileLocationOnDiskToDownloadTo);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.l2jfree.security.Base64.java

/**
 * Decodes data from Base64 notation, automatically detecting
 * gzip-compressed data and decompressing it.
 * /*from   www.ja  va2 s.  co  m*/
 * @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.length >= 2) {

        int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
        if (bytes.length >= 4 && // Don't want to get ArrayIndexOutOfBounds exception
                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 {
                IOUtils.closeQuietly(baos);
                IOUtils.closeQuietly(gzis);
                IOUtils.closeQuietly(bais);
            } // end finally

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

    return bytes;
}