org.tentackle.util
Class CompressedOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by org.tentackle.util.CompressedOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class CompressedOutputStream
extends FilterOutputStream

Stream to write compressed data to the underlying output stream.

Replacement for GZIPOutputStream and ZipOutputStream of java.util.zip capable for communication link streams like tcp-sockets. The standard classes are only file-capable (relying on EOF-detection). In comlinks, however, there are several packets instead of a single file. Hence, the stream must be blocked, i.e. the (varying) blocksize must become part of the protocol to allow the corresponding input stream to detect the end of the packets. Furthermore, we optimize to compress only packets larger than a certain size. All smaller packets will be transferred uncompressed. For the packet size we use the first two bytes in the packet (short), while MSBit signals whether the packet is compressed or not. Because of this, the maximum buffer size is limited to 32K - 1.


Field Summary
static int MAX_BUFFER_SIZE
          The maximum compression buffer size.
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
CompressedOutputStream(OutputStream out)
          Creates a compressed output stream with maximum allowed buffersize (32K-1) and a default minCompressSize of 64.
CompressedOutputStream(OutputStream out, int bufSize, int minCompressSize)
          Creates a compressed output stream.
 
Method Summary
 void close()
          Overridden to print stats only
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out to the stream.
 boolean isClosed()
           
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void write(int b)
          Writes the specified byte to this output stream.
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_BUFFER_SIZE

public static final int MAX_BUFFER_SIZE
The maximum compression buffer size. The larger the buffer, the better the compression.

See Also:
Constant Field Values
Constructor Detail

CompressedOutputStream

public CompressedOutputStream(OutputStream out,
                              int bufSize,
                              int minCompressSize)
Creates a compressed output stream.

Parameters:
out - the underlying output stream (e.g. from a socket)
bufSize - the buffer size for compression. Packets larger than bufSize are split.
minCompressSize - the minimum compressed packet size. Smaller packets pass the stream uncompressed.

CompressedOutputStream

public CompressedOutputStream(OutputStream out)
Creates a compressed output stream with maximum allowed buffersize (32K-1) and a default minCompressSize of 64.

Parameters:
out - the underlying output stream
Method Detail

write

public void write(int b)
           throws IOException
Writes the specified byte to this output stream.

Overrides:
write in class FilterOutputStream
Parameters:
b - the byte.
Throws:
IOException - if an I/O error occurs.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.

Packets larger than the buffer size will be split and written to the underlying output stream as separate packets.

Overrides:
write in class FilterOutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
IOException - if an I/O error occurs.
See Also:
FilterOutputStream.write(int)

flush

public void flush()
           throws IOException
Flushes this output stream and forces any buffered output bytes to be written out to the stream.

Specified by:
flush in interface Flushable
Overrides:
flush in class FilterOutputStream
Throws:
IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out

close

public void close()
           throws IOException
Overridden to print stats only

Specified by:
close in interface Closeable
Overrides:
close in class FilterOutputStream
Throws:
IOException - if an I/O error occurs.
See Also:
FilterOutputStream.flush(), FilterOutputStream.out

isClosed

public boolean isClosed()
Returns:
true if closed


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de