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.apache.sling.discovery.base.connectors.ping.TopologyRequestValidator.java

/**
 * Get the request body./*  ww  w.j av  a2s  .co m*/
 *
 * @param request the request.
 * @return the body as a string.
 * @throws IOException
 */
private String getRequestBody(HttpServletRequest request) throws IOException {
    final String contentEncoding = request.getHeader("Content-Encoding");
    if (contentEncoding != null && contentEncoding.contains("gzip")) {
        // then treat the request body as gzip:
        final GZIPInputStream gzipIn = new GZIPInputStream(request.getInputStream());
        final String gunzippedEncodedJson = IOUtils.toString(gzipIn);
        gzipIn.close();
        return gunzippedEncodedJson;
    } else {
        // otherwise assume plain-text:
        return IOUtils.toString(request.getReader());
    }
}

From source file:org.apache.sling.discovery.base.connectors.ping.TopologyRequestValidator.java

/**
 * @param response the response/*w w  w.jav  a 2s . c  om*/
 * @return the body of the response from the server.
 * @throws IOException
 */
private String getResponseBody(HttpResponse response) throws IOException {
    final Header contentEncoding = response.getFirstHeader("Content-Encoding");
    if (contentEncoding != null && contentEncoding.getValue() != null
            && contentEncoding.getValue().contains("gzip")) {
        // then the server sent gzip - treat it so:
        final GZIPInputStream gzipIn = new GZIPInputStream(response.getEntity().getContent());
        final String gunzippedEncodedJson = IOUtils.toString(gzipIn);
        gzipIn.close();
        return gunzippedEncodedJson;
    } else {
        // otherwise the server sent plaintext:
        return IOUtils.toString(response.getEntity().getContent(), "UTF-8");
    }
}

From source file:de.robbers.dashclock.stackextension.StackExtension.java

private String performHttpRequest(String uri) {
    Log.i(TAG, "URI: " + uri);
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(uri);
    get.addHeader("Accept-Encoding", "gzip");
    try {//  w  ww .j av a2s.  c om
        // get JSON from Stack Exchange API
        HttpResponse response = client.execute(get);
        InputStream inputStream = response.getEntity().getContent();
        GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(inputStream));
        InputStreamReader reader = new InputStreamReader(zis);
        BufferedReader in = new BufferedReader(reader);
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) {
            builder.append(line);
        }
        String json = builder.toString();
        zis.close();
        return json;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.apache.sling.discovery.impl.topology.connector.TopologyRequestValidator.java

/**
 * @param method the response method.//w  ww . j a  v  a 2 s. c  om
 * @return the body of the response from the server.
 * @throws IOException
 */
private String getResponseBody(HttpMethod method) throws IOException {
    final Header contentEncoding = method.getResponseHeader("Content-Encoding");
    if (contentEncoding != null && contentEncoding.getValue() != null
            && contentEncoding.getValue().contains("gzip")) {
        // then the server sent gzip - treat it so:
        final GZIPInputStream gzipIn = new GZIPInputStream(method.getResponseBodyAsStream());
        final String gunzippedEncodedJson = IOUtils.toString(gzipIn);
        gzipIn.close();
        return gunzippedEncodedJson;
    } else {
        // otherwise the server sent plaintext:
        if (method instanceof HttpMethodBase) {
            return ((HttpMethodBase) method).getResponseBodyAsString(16 * 1024 * 1024);
        }
        return method.getResponseBodyAsString();
    }
}

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/*w  w  w .  ja  v 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:me.Aron.Heinecke.fbot.lib.Socket.java

/***
 * Performs a logout from fronter based on the DB credentials
 * @param site content//from w  w w.j a v a  2 s  .com
 */
public synchronized String logout() throws ClientProtocolException, IOException {
    String url = "https://fronter.com/giessen/index.phtml?logout=1";

    //create own context which custom cookie store
    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(fbot.getDB().getCookieStore());

    //create client & request
    HttpGet request = new HttpGet(url);

    // add request header
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    request.addHeader("Accept-Encoding", "gzip, deflate");
    request.addHeader("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
    request.addHeader("Content-Type", "application/x-www-form-urlencoded");
    request.addHeader("Connection", "close");
    request.addHeader("DNT", "1");
    request.addHeader("Host", "fronter.com");
    request.addHeader("Referer", "https://fronter.com/giessen/personalframe.phtml");
    request.addHeader("User-Agent", UA);

    //execute request with local context
    HttpResponse response = client.execute(request, context);

    if (fbot.isDebug()) {
        fbot.getLogger().debug("socket", "Sending GET request to URL: " + url);
        fbot.getLogger().debug("socket", "Response code: " + response.getStatusLine().getStatusCode());
    }

    //get gzip
    InputStream input = response.getEntity().getContent();
    GZIPInputStream gzip = new GZIPInputStream(input);
    InputStreamReader isr = new InputStreamReader(gzip);
    BufferedReader rd = new BufferedReader(isr);

    //write buffer
    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    input.close();
    gzip.close();
    isr.close();
    rd.close();

    return result.toString();
}

From source file:autoupdater.FileDAO.java

/**
 * Untars and ungzips a .tar.gz file./* w w  w.  j a  va  2s  .  c  o 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.kkbox.toolkit.internal.api.APIRequest.java

private InputStream getInputStreamFromHttpResponse() throws IOException {
    InputStream inputStream;/*from   w  ww.j a  va 2  s  .  c  om*/
    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:net.sf.ehcache.constructs.web.PageInfoTest.java

private byte[] ungzip4(final byte[] gzip) throws IOException {
    byte[] buffer = new byte[500000];
    int size = 0;

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(gzip);
    GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);

    int nextByte = 0;
    int counter = 0;
    while (nextByte != -1) {
        nextByte = gzipInputStream.read();
        if (nextByte != -1) {
            buffer[counter] = (byte) nextByte;
            counter++;/*from  w ww.j a v a2  s  .  co m*/
            size = counter;
        }
    }

    gzipInputStream.close();
    byte[] unzipped = new byte[counter];
    System.arraycopy(buffer, 0, unzipped, 0, counter);
    return unzipped;
}

From source file:weka.knowledgeflow.steps.TimeSeriesForecasting.java

/**
 * Decodes a base 64 encoded string to a byte array.
 *
 * @param string the base 64 encoded string
 * @return the decoded bytes/*from  w  w  w.j  a v  a 2  s . co m*/
 * @throws Exception if a problem occurs
 */
protected static byte[] decodeFromBase64(String string) throws Exception {

    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;
}