ec.util
Class CompressingInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by java.util.zip.InflaterInputStream
              extended by ec.util.CompressingInputStream
All Implemented Interfaces:
java.io.Closeable

public class CompressingInputStream
extends java.util.zip.InflaterInputStream

CompressingInputStream and CompressingOutputStream allow you to perform compression on a stream, and flush it, and continue to use it. This is useful if you're planning on compressing a stream socket. Ordinarily compression does not send partial blocks across the stream for efficiency's sake. That's not good because we want to use a stream socket and the other side will just block waiting for a big chunk of data that may take a long time to show up. This simulates the Z_PARTIAL_FLUSH and Z_SYNC_FLUSH mechanisms in zlib. The code was largely stolen from the comments at

http://developer.java.sun.com/developer/bugParade/bugs/4255743.html
http://developer.java.sun.com/developer/bugParade/bugs/4206909.html

Beware that, like other compression streams, you should NOT construct multiple CompressingInputStreams and attach them one by one to the same underlying InputStream, throwing each away one by one. This won't work. Instread, construct one CompressingInputStream wrapped around your underlying InputStream and just use that.


Field Summary
 
Fields inherited from class java.util.zip.InflaterInputStream
buf, inf, len
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
CompressingInputStream(java.io.InputStream in)
           
CompressingInputStream(java.io.InputStream in, boolean nowrap)
           
 
Method Summary
 int available()
          available() should return the number of bytes that can be read without running into blocking wait.
 
Methods inherited from class java.util.zip.InflaterInputStream
close, fill, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.io.FilterInputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompressingInputStream

public CompressingInputStream(java.io.InputStream in)

CompressingInputStream

public CompressingInputStream(java.io.InputStream in,
                              boolean nowrap)
Method Detail

available

public int available()
              throws java.io.IOException
available() should return the number of bytes that can be read without running into blocking wait. Accomplishing this feast would eventually require to pre-inflate a huge chunk of data, so we rather opt for a more relaxed contract (java.util.zip.InflaterInputStream does not fit the bill). This code has been tested to work with BufferedReader.readLine();

Overrides:
available in class java.util.zip.InflaterInputStream
Throws:
java.io.IOException