aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-04-03 23:50:48 +0200
committerDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-04-03 23:50:48 +0200
commitbd9dba1a69e2a5523b4d0d7e445d10b6197c3329 (patch)
tree90bdae99830b539e57c1301d4f5be843a07cd7cf /src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java
parent91aeffae1b94766dd89d061b55a65741ba42be66 (diff)
parenta0fc1c6c77aea1d99078706cf2bee1c5efd246a4 (diff)
Merge branch 'compression' of https://github.com/rtreffer/Conversations into rtreffer-compression
Diffstat (limited to 'src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java')
-rw-r--r--src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java b/src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java
new file mode 100644
index 00000000..2eebf6f4
--- /dev/null
+++ b/src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java
@@ -0,0 +1,52 @@
+package eu.siacs.conversations.utils.zlib;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.Inflater;
+import java.util.zip.InflaterInputStream;
+
+/**
+ * ZLibInputStream is a zlib and input stream compatible version of an
+ * InflaterInputStream. This class solves the incompatibility between
+ * {@link InputStream#available()} and {@link InflaterInputStream#available()}.
+ */
+public class ZLibInputStream extends InflaterInputStream {
+
+ /**
+ * Construct a ZLibInputStream, reading data from the underlying stream.
+ *
+ * @param is The {@code InputStream} to read data from.
+ * @throws IOException If an {@code IOException} occurs.
+ */
+ public ZLibInputStream(InputStream is) throws IOException {
+ super(is, new Inflater(), 512);
+ }
+
+ /**
+ * Provide a more InputStream compatible version of available.
+ * A return value of 1 means that it is likly to read one byte without
+ * blocking, 0 means that the system is known to block for more input.
+ *
+ * @return 0 if no data is available, 1 otherwise
+ * @throws IOException
+ */
+ @Override
+ public int available() throws IOException {
+ /* This is one of the funny code blocks.
+ * InflaterInputStream.available violates the contract of
+ * InputStream.available, which breaks kXML2.
+ *
+ * I'm not sure who's to blame, oracle/sun for a broken api or the
+ * google guys for mixing a sun bug with a xml reader that can't handle
+ * it....
+ *
+ * Anyway, this simple if breaks suns distorted reality, but helps
+ * to use the api as intended.
+ */
+ if (inf.needsInput()) {
+ return 0;
+ }
+ return super.available();
+ }
+
+}