Example usage for java.util.zip GZIPInputStream GZIPInputStream

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

Introduction

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

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:org.onebusaway.nyc.stif.StifTripLoader.java

/**
 * For each STIF file, call run()./* w w  w  . j  a  va 2 s  . c  o m*/
 */
public void run(File path) {
    try {
        _log.info("loading stif from " + path.getAbsolutePath());
        InputStream in = new FileInputStream(path);
        if (path.getName().endsWith(".gz"))
            in = new GZIPInputStream(in);
        run(in);
    } catch (IOException e) {
        throw new RuntimeException("Error loading " + path, e);
    }
}

From source file:eu.peppol.smp.SmpContentRetrieverImpl.java

/**
 * Gets the XML content of a given url, wrapped in an InputSource object.
 *///from www. j av a2s.  c  o  m
@Override
public InputSource getUrlContent(URL url) {

    HttpURLConnection httpURLConnection = null;
    try {
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.connect();
    } catch (IOException e) {
        throw new IllegalStateException("Unable to connect to " + url + " ; " + e.getMessage(), e);
    }

    try {
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE)
            throw new TryAgainLaterException(url, httpURLConnection.getHeaderField("Retry-After"));
        if (httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK)
            throw new ConnectionException(url, httpURLConnection.getResponseCode());
    } catch (IOException e) {
        throw new RuntimeException("Problem reading URL data at " + url.toExternalForm(), e);
    }

    try {

        String encoding = httpURLConnection.getContentEncoding();
        InputStream in = new BOMInputStream(httpURLConnection.getInputStream());
        InputStream result;

        if (encoding != null && encoding.equalsIgnoreCase(ENCODING_GZIP)) {
            result = new GZIPInputStream(in);
        } else if (encoding != null && encoding.equalsIgnoreCase(ENCODING_DEFLATE)) {
            result = new InflaterInputStream(in);
        } else {
            result = in;
        }

        String xml = readInputStreamIntoString(result);

        return new InputSource(new StringReader(xml));

    } catch (Exception e) {
        throw new RuntimeException("Problem reading URL data at " + url.toExternalForm(), e);
    }

}

From source file:com.cloudant.sync.datastore.SavedHttpAttachment.java

@Override
public InputStream getInputStream() throws IOException {
    if (data == null) {
        HttpRequests requests = new HttpRequests(new BasicHttpParams(), null, null, null);
        if (encoding == Encoding.Gzip) {
            return new GZIPInputStream(requests.getCompressed(attachmentURI));
        } else {//  w w  w. jav  a 2  s.  c  om
            return requests.get(attachmentURI);
        }
    } else {
        return new ByteArrayInputStream(data);
    }

}

From source file:com.github.commonclasses.network.DownloadUtils.java

public static long download(String urlStr, File dest, boolean append, DownloadListener downloadListener)
        throws Exception {
    int downloadProgress = 0;
    long remoteSize = 0;
    int currentSize = 0;
    long totalSize = -1;

    if (!append && dest.exists() && dest.isFile()) {
        dest.delete();//from  w w w .j  a  va2 s.c  om
    }

    if (append && dest.exists() && dest.exists()) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(dest);
            currentSize = fis.available();
        } catch (IOException e) {
            throw e;
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

    HttpGet request = new HttpGet(urlStr);
    request.setHeader("Content-Type", "application/x-www-form-urlencoded");

    if (currentSize > 0) {
        request.addHeader("RANGE", "bytes=" + currentSize + "-");
    }

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, DATA_TIMEOUT);
    HttpClient httpClient = new DefaultHttpClient(params);

    InputStream is = null;
    FileOutputStream os = null;
    try {
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            is = response.getEntity().getContent();
            remoteSize = response.getEntity().getContentLength();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                is = new GZIPInputStream(is);
            }
            os = new FileOutputStream(dest, append);
            byte buffer[] = new byte[DATA_BUFFER];
            int readSize = 0;
            while ((readSize = is.read(buffer)) > 0) {
                os.write(buffer, 0, readSize);
                os.flush();
                totalSize += readSize;
                if (downloadListener != null) {
                    downloadProgress = (int) (totalSize * 100 / remoteSize);
                    downloadListener.downloading(downloadProgress);
                }
            }
            if (totalSize < 0) {
                totalSize = 0;
            }
        }
    } catch (Exception e) {
        if (downloadListener != null) {
            downloadListener.exception(e);
        }
        e.printStackTrace();
    } finally {
        if (os != null) {
            os.close();
        }
        if (is != null) {
            is.close();
        }
    }

    if (totalSize < 0) {
        throw new Exception("Download file fail: " + urlStr);
    }

    if (downloadListener != null) {
        downloadListener.downloaded(dest);
    }

    return totalSize;
}

From source file:com.simiacryptus.util.Util.java

/**
 * Binary stream stream./* w  ww .  j  a v  a2s  . co m*/
 *
 * @param path       the path
 * @param name       the name
 * @param skip       the skip
 * @param recordSize the record size
 * @return the stream
 * @throws IOException the io exception
 */
public static Stream<byte[]> binaryStream(final String path, @javax.annotation.Nonnull final String name,
        final int skip, final int recordSize) throws IOException {
    @javax.annotation.Nonnull
    final File file = new File(path, name);
    final byte[] fileData = IOUtils.toByteArray(
            new BufferedInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))));
    @javax.annotation.Nonnull
    final DataInputStream in = new DataInputStream(new ByteArrayInputStream(fileData));
    in.skip(skip);
    return com.simiacryptus.util.Util.toIterator(new BinaryChunkIterator(in, recordSize));
}

From source file:io.scigraph.services.jersey.dynamic.SwaggerFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    // Capture the output of the filter chain
    ByteArrayResponseWrapper wrappedResp = new ByteArrayResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request, wrappedResp);
    if (isGzip(request)) {
        try (InputStream is = new ByteArrayInputStream(wrappedResp.getBytes());
                GZIPInputStream gis = new GZIPInputStream(is);
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                GZIPOutputStream gzos = new GZIPOutputStream(bs)) {
            byte[] newApi = writeDynamicResource(gis);
            gzos.write(newApi);/*from  ww  w. ja va  2  s  . c o m*/
            gzos.close();
            byte[] output = bs.toByteArray();
            response.setContentLength(output.length);
            response.getOutputStream().write(output);
        }
    } else {
        try (InputStream is = new ByteArrayInputStream(wrappedResp.getBytes());
                ByteArrayOutputStream bs = new ByteArrayOutputStream()) {
            byte[] newApi = writeDynamicResource(is);
            response.setContentLength(newApi.length);
            response.getOutputStream().write(newApi);
        }

    }
}

From source file:edu.scripps.fl.pubchem.PubChemFactory.java

public InputStream getPubChemXmlDesc(long aid) throws IOException {
    String archive = getAIDArchive(aid);
    String sUrl = String.format(pubchemBioAssayUrlFormat, ftpUser, ftpPass, "Description", archive);
    String szip = String.format("zip:%s!/%s/%s.descr.xml.gz", sUrl, archive, aid);
    log.debug(sUrl);/*from ww w. j  a  v  a 2  s. c om*/
    FileObject fo = VFS.getManager().resolveFile(szip);
    log.debug("Resolved file: " + szip);
    try {
        InputStream is = fo.getContent().getInputStream();
        return new GZIPInputStream(is);
    } catch (Exception e) {
        log.info("AID: " + aid + " is not in the ftp site.", e.getMessage());
        return null;
    }
}

From source file:com.audiveris.installer.Expander.java

/**
 * Ungzip an input file into an output file.
 *
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.gz' extension.
 *
 * @param inFile the input .gz file//from  w  w w .ja  v  a  2  s .  co  m
 * @param outDir the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 *
 * @return The file with the ungzipped content.
 */
public static File unGzip(final File inFile, final File outDir) throws FileNotFoundException, IOException {
    logger.debug("Ungzipping {} to dir {}", inFile, outDir);

    final String inName = inFile.getName();
    assert inName.endsWith(GZ_EXT);

    final InputStream in = new GZIPInputStream(new FileInputStream(inFile));
    final File outFile = streamToFile(in,
            new File(outDir, inName.substring(0, inName.length() - GZ_EXT.length())));
    in.close();

    return outFile;
}

From source file:io.druid.data.input.impl.prefetch.RetryingInputStreamTest.java

@Before
public void setup() throws IOException {
    testFile = temporaryFolder.newFile();

    try (FileOutputStream fis = new FileOutputStream(testFile);
            GZIPOutputStream gis = new GZIPOutputStream(fis);
            DataOutputStream dis = new DataOutputStream(gis)) {
        for (int i = 0; i < 10000; i++) {
            dis.writeInt(i);/*from  w w  w .jav a  2 s  . c  o m*/
        }
    }

    throwError = false;

    final InputStream retryingInputStream = new RetryingInputStream<>(testFile, new ObjectOpenFunction<File>() {
        @Override
        public InputStream open(File object) throws IOException {
            return new TestInputStream(new FileInputStream(object));
        }

        @Override
        public InputStream open(File object, long start) throws IOException {
            final FileInputStream fis = new FileInputStream(object);
            Preconditions.checkState(fis.skip(start) == start);
            return new TestInputStream(fis);
        }
    }, e -> e instanceof IOException, MAX_RETRY);

    inputStream = new DataInputStream(new GZIPInputStream(retryingInputStream));

    throwError = true;
}

From source file:com.lyndir.lhunath.opal.network.GZIPPostMethod.java

/**
 * If the response body was GZIP-compressed, responseStream will be set to a GZIPInputStream wrapping the original InputStream used by
 * the super class./*from  ww w.  j a va 2  s . c om*/
 *
 * {@inheritDoc}
 */
@Override
protected void readResponse(final HttpState state, final HttpConnection conn) throws IOException {

    super.readResponse(state, conn);

    Header contentEncodingHeader = getResponseHeader("Content-Encoding");
    if (contentEncodingHeader != null && "gzip".equalsIgnoreCase(contentEncodingHeader.getValue())) {
        try (InputStream zippedStream = new GZIPInputStream(getResponseStream())) {
            setResponseStream(zippedStream);
        }
    }
}