use of java.util.zip.GZIPInputStream in project qi4j-sdk by Qi4j.
the class Inputs method text.
// START SNIPPET: method
/**
* Read lines from a textfile at a given URL.
*
* If the content support gzip encoding, then the data is automatically unzipped when read.
*
* The charset in the content-type of the URL will be used for parsing. Default is UTF-8.
*
* @param source textfile with lines separated by \n character
*
* @return Input that provides lines from the textfiles as strings
*/
public static Input<String, IOException> text(final URL source) // END SNIPPET: method
{
return new Input<String, IOException>() {
@Override
public <ReceiverThrowableType extends Throwable> void transferTo(Output<? super String, ReceiverThrowableType> output) throws IOException, ReceiverThrowableType {
URLConnection urlConnection = source.openConnection();
urlConnection.setRequestProperty("Accept-Encoding", "gzip");
InputStream stream = urlConnection.getInputStream();
// If file is gzipped, unzip it automatically
if ("gzip".equals(urlConnection.getContentEncoding())) {
stream = new GZIPInputStream(stream);
}
// Figure out charset given content-type
String contentType = urlConnection.getContentType();
String charSet = "UTF-8";
if (contentType.indexOf("charset=") != -1) {
charSet = contentType.substring(contentType.indexOf("charset=") + "charset=".length());
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charSet))) {
output.receiveFrom(new Sender<String, IOException>() {
@Override
public <ReceiverThrowableType extends Throwable> void sendTo(Receiver<? super String, ReceiverThrowableType> receiver) throws ReceiverThrowableType, IOException {
String line;
while ((line = reader.readLine()) != null) {
receiver.receive(line);
}
}
});
}
}
};
}
use of java.util.zip.GZIPInputStream in project LogisticsPipes by RS485.
the class ServerPacketBufferHandlerThread method decompress.
private static byte[] decompress(byte[] contentBytes) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(contentBytes));
int buffer = 0;
while ((buffer = gzip.read()) != -1) {
out.write(buffer);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return out.toByteArray();
}
use of java.util.zip.GZIPInputStream in project LogisticsPipes by RS485.
the class ClientPacketBufferHandlerThread method decompress.
private static byte[] decompress(byte[] contentBytes) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(contentBytes));
int buffer = 0;
while ((buffer = gzip.read()) != -1) {
out.write(buffer);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return out.toByteArray();
}
use of java.util.zip.GZIPInputStream in project Notes by MiCode.
the class GTaskClient method getResponseContent.
private String getResponseContent(HttpEntity entity) throws IOException {
String contentEncoding = null;
if (entity.getContentEncoding() != null) {
contentEncoding = entity.getContentEncoding().getValue();
Log.d(TAG, "encoding: " + contentEncoding);
}
InputStream input = entity.getContent();
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
input = new GZIPInputStream(entity.getContent());
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
Inflater inflater = new Inflater(true);
input = new InflaterInputStream(entity.getContent(), inflater);
}
try {
InputStreamReader isr = new InputStreamReader(input);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while (true) {
String buff = br.readLine();
if (buff == null) {
return sb.toString();
}
sb = sb.append(buff);
}
} finally {
input.close();
}
}
use of java.util.zip.GZIPInputStream in project phonegap-facebook-plugin by Wizcorp.
the class HttpEngine method initContentStream.
private void initContentStream(InputStream transferStream) throws IOException {
responseTransferIn = transferStream;
if (transparentGzip && responseHeaders.isContentEncodingGzip()) {
// If the response was transparently gzipped, remove the gzip header field
// so clients don't double decompress. http://b/3009828
//
// Also remove the Content-Length in this case because it contains the
// length 528 of the gzipped response. This isn't terribly useful and is
// dangerous because 529 clients can query the content length, but not
// the content encoding.
responseHeaders.stripContentEncoding();
responseHeaders.stripContentLength();
responseBodyIn = new GZIPInputStream(transferStream);
} else {
responseBodyIn = transferStream;
}
}
Aggregations