Search in sources :

Example 96 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project YCSB by brianfrankcooper.

the class RestClient method httpExecute.

private int httpExecute(HttpEntityEnclosingRequestBase request, String data) throws IOException {
    requestTimedout.setIsSatisfied(false);
    Thread timer = new Thread(new Timer(execTimeout, requestTimedout));
    timer.start();
    int responseCode = 200;
    for (int i = 0; i < headers.length; i = i + 2) {
        request.setHeader(headers[i], headers[i + 1]);
    }
    InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(data.getBytes()), ContentType.APPLICATION_FORM_URLENCODED);
    reqEntity.setChunked(true);
    request.setEntity(reqEntity);
    CloseableHttpResponse response = client.execute(request);
    responseCode = response.getStatusLine().getStatusCode();
    HttpEntity responseEntity = response.getEntity();
    // If null entity don't bother about connection release.
    if (responseEntity != null) {
        InputStream stream = responseEntity.getContent();
        if (compressedResponse) {
            stream = new GZIPInputStream(stream);
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        StringBuffer responseContent = new StringBuffer();
        String line = "";
        while ((line = reader.readLine()) != null) {
            if (requestTimedout.isSatisfied()) {
                // Must avoid memory leak.
                reader.close();
                stream.close();
                EntityUtils.consumeQuietly(responseEntity);
                response.close();
                client.close();
                throw new TimeoutException();
            }
            responseContent.append(line);
        }
        timer.interrupt();
        // Closing the input stream will trigger connection release.
        stream.close();
    }
    EntityUtils.consumeQuietly(responseEntity);
    response.close();
    client.close();
    return responseCode;
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InputStreamEntity(org.apache.http.entity.InputStreamEntity) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader)

Example 97 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project baker-android by bakerframework.

the class AndroidHttpClient method getUngzippedContent.

/**
     * Gets the input stream from a response entity.  If the entity is gzipped
     * then this will get a stream over the uncompressed data.
     *
     * @param entity the entity whose content should be read
     * @return the input stream to read from
     * @throws IOException
     */
public static InputStream getUngzippedContent(HttpEntity entity) throws IOException {
    InputStream responseStream = entity.getContent();
    if (responseStream == null)
        return responseStream;
    Header header = entity.getContentEncoding();
    if (header == null)
        return responseStream;
    String contentEncoding = header.getValue();
    if (contentEncoding == null)
        return responseStream;
    if (contentEncoding.contains("gzip"))
        responseStream = new GZIPInputStream(responseStream);
    return responseStream;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.apache.http.Header) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream)

Example 98 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project bazel by bazelbuild.

the class CompressedTarFunctionTest method testDecompressWithPrefix.

/**
   * Test decompressing a tar.gz file with hard link file and symbolic link file inside and
   * stripping a prefix
   *
   * @throws Exception
   */
@Test
public void testDecompressWithPrefix() throws Exception {
    descriptorBuilder.setPrefix(ROOT_FOLDER_NAME);
    Path outputDir = new CompressedTarFunction() {

        @Override
        protected InputStream getDecompressorStream(DecompressorDescriptor descriptor) throws IOException {
            return new GZIPInputStream(new FileInputStream(descriptor.archivePath().getPathFile()));
        }
    }.decompress(descriptorBuilder.build());
    assertOutputFiles(outputDir.getRelative(INNER_FOLDER_NAME));
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) GZIPInputStream(java.util.zip.GZIPInputStream) CompressedTarFunction(com.google.devtools.build.lib.bazel.repository.CompressedTarFunction) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DecompressorDescriptor(com.google.devtools.build.lib.bazel.repository.DecompressorDescriptor) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 99 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project bazel by bazelbuild.

the class CompressedTarFunctionTest method testDecompressWithoutPrefix.

/**
   * Test decompressing a tar.gz file with hard link file and symbolic link file inside without
   * stripping a prefix
   *
   * @throws Exception
   */
@Test
public void testDecompressWithoutPrefix() throws Exception {
    Path outputDir = new CompressedTarFunction() {

        @Override
        protected InputStream getDecompressorStream(DecompressorDescriptor descriptor) throws IOException {
            return new GZIPInputStream(new FileInputStream(descriptor.archivePath().getPathFile()));
        }
    }.decompress(descriptorBuilder.build());
    assertOutputFiles(outputDir.getRelative(ROOT_FOLDER_NAME).getRelative(INNER_FOLDER_NAME));
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) GZIPInputStream(java.util.zip.GZIPInputStream) CompressedTarFunction(com.google.devtools.build.lib.bazel.repository.CompressedTarFunction) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DecompressorDescriptor(com.google.devtools.build.lib.bazel.repository.DecompressorDescriptor) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 100 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project h2o-3 by h2oai.

the class GlmMojoBenchHelper method readData.

static void readData(File f, int[] mapping, String firstColName, double[][] out, MojoModel mojo) throws IOException {
    InputStream is = new FileInputStream(f);
    try {
        InputStream source;
        if (f.getName().endsWith(".zip")) {
            ZipInputStream zis = new ZipInputStream(is);
            ZipEntry entry = zis.getNextEntry();
            if (!entry.getName().endsWith(".csv"))
                throw new IllegalStateException("CSV file expected, name " + entry.getName());
            source = zis;
        } else {
            source = new GZIPInputStream(is);
        }
        CSVReader r = new CSVReader(new InputStreamReader(source));
        if (firstColName != null) {
            String[] header = r.readNext();
            if (header == null)
                throw new IllegalStateException("File empty");
            if (!firstColName.equals(header[0]))
                throw new IllegalStateException("Header expected");
        }
        int rowIdx = 0;
        String[] row;
        while ((rowIdx < out.length) && ((row = r.readNext()) != null)) {
            double[] outRow = out[rowIdx++];
            if (row.length < mapping.length)
                throw new IllegalStateException("Row too short: " + Arrays.toString(row));
            for (int i = 0; i < mapping.length; i++) {
                int target = mapping[i];
                if (target < 0)
                    continue;
                if ("NA".equals(row[i])) {
                    outRow[target] = Double.NaN;
                    continue;
                }
                String[] domain = mojo.getDomainValues(target);
                if (domain == null)
                    outRow[target] = Double.parseDouble(row[i]);
                else {
                    outRow[target] = -1;
                    for (int d = 0; d < domain.length; d++) if (domain[d].equals(row[i])) {
                        outRow[target] = d;
                        break;
                    }
                    if (outRow[target] < 0)
                        throw new IllegalStateException("Value " + row[i] + " not found in domain " + Arrays.toString(domain));
                }
            }
        }
    } finally {
        is.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) CSVReader(au.com.bytecode.opencsv.CSVReader) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Aggregations

GZIPInputStream (java.util.zip.GZIPInputStream)376 InputStream (java.io.InputStream)144 IOException (java.io.IOException)125 ByteArrayInputStream (java.io.ByteArrayInputStream)120 FileInputStream (java.io.FileInputStream)98 ByteArrayOutputStream (java.io.ByteArrayOutputStream)77 InputStreamReader (java.io.InputStreamReader)57 File (java.io.File)56 BufferedReader (java.io.BufferedReader)45 BufferedInputStream (java.io.BufferedInputStream)41 Test (org.junit.Test)41 FileOutputStream (java.io.FileOutputStream)30 URL (java.net.URL)25 InflaterInputStream (java.util.zip.InflaterInputStream)25 OutputStream (java.io.OutputStream)24 GZIPOutputStream (java.util.zip.GZIPOutputStream)21 ObjectInputStream (java.io.ObjectInputStream)19 HttpURLConnection (java.net.HttpURLConnection)19 URLConnection (java.net.URLConnection)17 HashMap (java.util.HashMap)15