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:com.streamsets.pipeline.stage.destination.http.TestHttpClientTarget.java

@Before
public void setUp() throws Exception {
    int port = getFreePort();
    server = new Server(port);
    server.setHandler(new AbstractHandler() {
        @Override//from  w  w  w  .  ja  v a  2 s.c o m
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            serverRequested = true;
            if (returnErrorResponse) {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return;
            }

            compressionType = request.getHeader(HttpConstants.CONTENT_ENCODING_HEADER);
            InputStream is = request.getInputStream();
            if (compressionType != null && compressionType.equals(HttpConstants.SNAPPY_COMPRESSION)) {
                is = new SnappyFramedInputStream(is, true);
            } else if (compressionType != null && compressionType.equals(HttpConstants.GZIP_COMPRESSION)) {
                is = new GZIPInputStream(is);
            }
            requestPayload = IOUtils.toString(is);
            requestContentType = request.getContentType();
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
        }
    });
    server.start();
}

From source file:com.github.kutschkem.Qgen.QuestionRankerByParseProbability.java

/**
 * Load the parser from the given location within the classpath.
 * /*from  ww w  .j ava2 s .c om*/
 * @param aUrl
 *            URL of the parser file.
 */
// copied from DKPro's StanfordParser
private LexicalizedParser getParserDataFromSerializedFile(URL aUrl) throws IOException {
    ObjectInputStream in;
    InputStream is = null;
    try {
        is = aUrl.openStream();

        if (aUrl.toString().endsWith(".gz")) {
            // it's faster to do the buffering _outside_ the gzipping as
            // here
            in = new ObjectInputStream(new BufferedInputStream(new GZIPInputStream(is)));
        } else {
            in = new ObjectInputStream(new BufferedInputStream(is));
        }
        LexicalizedParser pd = LexicalizedParser.loadModel(in);
        // Numberer.setNumberers(pd.numbs); // will happen later in
        // makeParsers()
        in.close();
        return pd;
    } finally {
        closeQuietly(is);
    }
}

From source file:com.florianmski.tracktoid.image.Fanart.java

private synchronized void loadData(URL url, Context c) {
    try {/*from  w  w  w. j av  a 2 s  .c  o m*/
        HttpUriRequest request = new HttpGet(url.toString());
        request.addHeader("Accept-Encoding", "gzip");
        res = http.execute(request);
    } catch (IOException e) {
        Log.e("FanartParser", "Error loading data", e);
    }
    try {
        //using special encoding to reduce download time
        InputStream instream = res.getEntity().getContent();
        Header contentEncoding = res.getFirstHeader("Content-Encoding");
        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip"))
            instream = new GZIPInputStream(instream);

        xr.parse(new InputSource(instream));
    } catch (Exception e) {
        Log.e("FanartParser", "Error parsing data", e);
    }
}

From source file:net.oddsoftware.android.html.HttpCache.java

public InputStream getResource(String url, boolean forceDownload) throws IOException {
    CacheItem cacheItem = CacheItem.getByURL(mContentResolver, url);

    if (cacheItem == null) {
        cacheItem = new CacheItem(url);

        download(cacheItem);//from  ww w . j av a 2  s  .  co m
    } else {
        cacheItem.mHitTime = new Date().getTime();
        cacheItem.update(mContentResolver);
    }

    /*// for now we will always use the cache if it's there
    if( (! ALWAYS_PREFER_CACHE) && cacheItem.mExpiresAt < new Date().getTime() )
    {
    download(cacheItem);
    }
    */

    File cacheFile = new File(cacheItem.mFilename);

    if (!forceDownload) {
        if (cacheFile.exists()) {
            return new GZIPInputStream(new FileInputStream(cacheFile));
        }
    }

    download(cacheItem);

    if (cacheFile.exists()) {
        return new GZIPInputStream(new FileInputStream(cacheFile));
    }

    return null;
}

From source file:com.jaspersoft.jasperserver.core.util.StreamUtil.java

public static String uncompressToString(byte[] compressed) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
    GZIPInputStream gzis = new GZIPInputStream(bais);
    BufferedReader br = new BufferedReader(new InputStreamReader(gzis));
    String line;/*from  w  ww .j a v  a 2  s.  c o  m*/
    StringBuffer sb = new StringBuffer();
    while ((line = br.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
    }
    return sb.toString();
}

From source file:com.streamsets.datacollector.util.UntarUtility.java

/**
 * Ungzip an input file into an output file.
 * <p>//from   w  ww  .  j  a v a  2s .c  o m
 * The output file is created in the output folder, having the same name as the input file, minus the '.gz' extension.
 * @param inputFile the input .gz file
 * @param outputDir the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 * @return The {@File} with the ungzipped content.
 * @throws ArchiveException
 */
public static List<File> unGzip(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {
    LOG.info(String.format("Ungzipping %s to dir %s.", inputFile.getAbsolutePath(),
            outputDir.getAbsolutePath()));

    final File outputFile = new File(outputDir,
            inputFile.getName().substring(0, inputFile.getName().length() - 3));

    final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
    final FileOutputStream out = new FileOutputStream(outputFile);

    IOUtils.copy(in, out);

    in.close();
    out.close();

    return unTar(outputFile, outputDir);
}

From source file:com.imellon.android.grocerymate.util.HttpManager.java

/**
 * Executes an HTTP request on a REST web service. If the response is ok, the content
 * is sent to the specified response handler.
 *
 * @param host//w  ww .j  a va  2  s  . c o  m
 * @param get The GET request to executed.
 * @param handler The handler which will parse the response.
 *
 * @throws java.io.IOException
 */
private static InputStream executeRequest(HttpGet get) throws IOException {

    HttpEntity entity = null;
    //        try {
    final HttpResponse response = HttpManager.execute(get);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        entity = response.getEntity();
        InputStream in = entity.getContent();
        if (response.containsHeader("Content-Encoding")
                && response.getFirstHeader("Content-Encoding").getValue().equalsIgnoreCase("gzip")) {
            in = new GZIPInputStream(in);
        }
        return in;
    }
    return null;
    //        } finally {
    //            if (entity != null) {
    //                entity.consumeContent();
    //            }
    //        }
}

From source file:com.foglyn.fogbugz.Request.java

private <T> T request(String url, HttpMethod method, IProgressMonitor monitor, ResponseProcessor<T> processor)
        throws FogBugzException {
    Utils.checkCancellation(monitor);/*from  ww  w . j  a va2  s  .co  m*/

    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, repositoryLocation,
            monitor);

    if (allowGzip) {
        method.addRequestHeader("Accept-Encoding", "gzip");
    }

    InputStream responseStream = null;
    CancellableInputStream cancellableStream = null;
    try {
        log.debug("Sending request to server");
        int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);

        log.debug("Got " + code + " response");

        if (!processor.checkHttpStatus(code)) {
            Map<String, String> headers = Utils.convertHeadersToMap(method);

            method.abort();

            throw unexpectedStatus(code, url, headers);
        }

        log.debug("Downloading data");

        responseStream = method.getResponseBodyAsStream();

        InputStream processed = responseStream;

        // may be null, for example for HEAD request
        if (processed != null) {
            Header contentEncoding = method.getResponseHeader("Content-Encoding");
            if (allowGzip && contentEncoding != null && "gzip".equals(contentEncoding.getValue())) {
                processed = new GZIPInputStream(processed);
            }

            cancellableStream = new CancellableInputStream(processed, monitor, threadFactory);
            processed = cancellableStream;
        }

        log.debug("Processing response");

        return processor.processResponse(url, method, processed);
    } catch (RuntimeException e) {
        // also catches OperationCanceledException

        // we don't know what happened to method, so we better abort processing
        method.abort();

        log.error("Error while executing request", e);

        throw e;
    } catch (IOException e) {
        // we don't know what happened... better abort connection
        method.abort();

        log.error("IO Error while executing request", e);

        throw ioError(e, url);
    } finally {
        if (cancellableStream != null) {
            cancellableStream.shutdownBackgroundThread();
        }

        // don't use cancellable stream to close responseStream -- because in case of cancelled monitor, it would ignore close request 
        Utils.close(responseStream);

        method.releaseConnection();
    }
}

From source file:edu.cornell.med.icb.goby.modes.BuildSequenceCacheMode.java

/**
 * Convert the Fasta input file to a random access cache.
 *
 * @throws java.io.IOException error reading / writing
 *//* w w w.  j a va2 s.c om*/
@Override
public void execute() throws IOException {
    final RandomAccessSequenceCache cacheBuilder = new RandomAccessSequenceCache();

    InputStream input = null;
    try {
        System.out.println("Loading and compressing genome..");
        if (inputFile.endsWith(".compact-reads")) {
            input = new FileInputStream(inputFile);
            cacheBuilder.loadCompact(input);
        } else {
            if (inputFile.endsWith(".gz")) {
                input = new GZIPInputStream(new FileInputStream(inputFile));

            } else {
                input = new FileInputStream(inputFile);
            }
            cacheBuilder.loadFasta(new InputStreamReader(input));
        }

        System.out.println("Done loading input. Starting to write random access cacheBuilder.");
        cacheBuilder.save(basename);
        System.out.println("Compressed genome was written to basename " + basename);
    } finally {
        IOUtils.closeQuietly(input);
    }
    System.out.println("Sequence cacheBuilder written to disk with basename " + basename);
}

From source file:com.gargoylesoftware.htmlunit.WebResponseData.java

private InputStream getStream(final DownloadedContent downloadedContent, final List<NameValuePair> headers)
        throws IOException {

    InputStream stream = downloadedContent_.getInputStream();
    if (stream == null) {
        return null;
    }/*from   ww w .  j  av  a2 s. c  o m*/

    if (downloadedContent.isEmpty()) {
        return stream;
    }

    final String encoding = getHeader(headers, "content-encoding");
    if (encoding != null) {
        if (StringUtils.contains(encoding, "gzip")) {
            stream = new GZIPInputStream(stream);
        } else if (StringUtils.contains(encoding, "deflate")) {
            boolean zlibHeader = false;
            if (stream.markSupported()) { // should be always the case as the content is in a byte[] or in a file
                stream.mark(2);
                final byte[] buffer = new byte[2];
                stream.read(buffer, 0, 2);
                zlibHeader = (((buffer[0] & 0xff) << 8) | (buffer[1] & 0xff)) == 0x789c;
                stream.reset();
            }
            if (zlibHeader) {
                stream = new InflaterInputStream(stream);
            } else {
                stream = new InflaterInputStream(stream, new Inflater(true));
            }
        }
    }
    return stream;
}