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;
}
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;
}
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));
}
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));
}
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();
}
}
Aggregations