From d4f214e8172355f9160a3c1a210635ac68ee0ce6 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 30 Apr 2014 19:45:08 +0200 Subject: StreamImp: support gzip compression May yield huge bandwidth savings and improve throughput for slow links. --- src/io/StreamImpl.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/io/StreamImpl.java b/src/io/StreamImpl.java index 259caa6..d9f2875 100644 --- a/src/io/StreamImpl.java +++ b/src/io/StreamImpl.java @@ -9,6 +9,7 @@ import java.util.HashSet; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Logger; +import java.util.zip.GZIPInputStream; import mining.Stream; import org.apache.commons.io.Charsets; import org.apache.commons.lang.StringUtils; @@ -160,6 +161,7 @@ public class StreamImpl implements Stream { // set request headers conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + conn.addRequestProperty("Accept-Encoding", "gzip"); // connect and send request conn.setDoOutput(true); conn.getOutputStream().write(postData.getBytes(Charsets.UTF_8)); @@ -172,12 +174,26 @@ public class StreamImpl implements Stream { return conn; } + /** + * Wraps an inputstream as gzip if possible. + * + * @param is The raw input stream. + * @return An inputstream that outputs decoded data. + * @throws IOException + */ + private InputStream wrapGzip(InputStream is) throws IOException { + if ("gzip".equals(connection.getContentEncoding())) { + return new GZIPInputStream(is); + } + return is; + } + @Override public void run() { InputStream is = null; try { is = connection.getInputStream(); - parseMainLoop(is); + parseMainLoop(wrapGzip(is)); } catch (IOException ex) { if (is != null) { try { -- cgit v1.2.1