summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-30 19:45:08 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-30 19:45:08 +0200
commitd4f214e8172355f9160a3c1a210635ac68ee0ce6 (patch)
tree36450759e78c6a2750381c8175ec7a0ed4215904
parente6e30ea585131df43ca1d47ac5cd4b1730b02200 (diff)
downloadTwitterDataAnalytics-d4f214e8172355f9160a3c1a210635ac68ee0ce6.tar.gz
StreamImp: support gzip compression
May yield huge bandwidth savings and improve throughput for slow links.
-rw-r--r--src/io/StreamImpl.java18
1 files changed, 17 insertions, 1 deletions
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 {