com.bigzip.plugin.compression.io
Class CompressionStream

java.lang.Object
  |
  +--com.bigzip.plugin.compression.io.AbstractStream
        |
        +--com.bigzip.plugin.compression.io.CompressionStream

public class CompressionStream
extends AbstractStream

This class implements the archive IO stream for compressing data in the "deflate" compression format and decomressing data using the "inflate" decompression scheme. It is also used as the basis for other types of compression filters, such as GZIPOutputStream.

See Also:
java.util.zip.Deflater, java.util.zip.Inflater, CheckedStream

Field Summary
protected  byte[] buf
          Output buffer for writing compressed data.
protected  Compressor comp
          Compressor for this stream.
protected  Decompressor decomp
          Decompressor for this stream.
protected  int len
          Length of input buffer.
 
Fields inherited from class com.bigzip.plugin.compression.io.AbstractStream
clientStream
 
Constructor Summary
CompressionStream(AbstractStream clientStream)
          Creates a new random access stream with a default buffer size of 512.
CompressionStream(AbstractStream clientStream, Compressor comp)
          Creates a new random access stream with the specified compressor and a default buffer size of 512.
CompressionStream(AbstractStream clientStream, Compressor comp, Decompressor decomp)
          Creates a new random access stream with the specified compressor and a default buffer size of 512.
CompressionStream(AbstractStream clientStream, Compressor comp, Decompressor decomp, int size)
          Creates a new random access stream with the specified compressor and buffer size.
CompressionStream(AbstractStream clientStream, Decompressor decomp)
          Creates a new random access stream with the specified compressor and a default buffer size of 512.
CompressionStream(AbstractStream clientStream, int size)
          Creates a new random access stream with the specified buffer size.
 
Method Summary
 int availableCompress()
          Returns 0 after EOF has reached, otherwise always return 1.
 void deflate()
          Writes next block of compressed data to the random access stream.
protected  void fill()
          Fills input buffer with more data to decompress.
 void finish()
          Finishes writing compressed data to the random access stream without closing the underlying stream.
 Compressor getCompressor()
           
 Decompressor getDecompressor()
           
 int read()
          Reads a byte of uncompressed data.
 int read(byte[] b, int off, int len)
          Reads uncompressed data into an array of bytes.
 void readFully(byte[] b, int off, int len)
          Reads data that is not compressed into an array of bytes.
 void resetEngine()
          Retrieves new instances of the compressor and decompressor.
 void setCompressor(Compressor comp)
          Assigns a new compressing engine.
 void setDecompressor(Decompressor decomp)
          Assigns a new compressing engine.
 int skipBytes(int n)
          Skips specified number of bytes of uncompressed data.
 void write(byte[] b, int off, int len)
          Writes an array of bytes to the compressed random access stream.
 void write(int b)
          Writes a byte to the compressed random access stream.
 
Methods inherited from class com.bigzip.plugin.compression.io.AbstractStream
close, ensureOpen, getFilePointer, isClosed, length, read, readBoolean, readByte, readChar, readDirect, readDirect, readDouble, readFloat, readInt, readInt, readLine, readLong, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF, seek, setClientStream, setClosed, write, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDirect, writeDirect, writeDouble, writeFloat, writeFully, writeInt, writeLong, writeShort, writeUTF
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

comp

protected Compressor comp
Compressor for this stream.

decomp

protected Decompressor decomp
Decompressor for this stream.

buf

protected byte[] buf
Output buffer for writing compressed data.

len

protected int len
Length of input buffer.
Constructor Detail

CompressionStream

public CompressionStream(AbstractStream clientStream,
                         Compressor comp,
                         Decompressor decomp,
                         int size)
Creates a new random access stream with the specified compressor and buffer size.
Parameters:
clientStream - the random access stream
comp - the compressor ("deflater")
decomp - the decompressor ("inflater")
len - the output buffer size
Throws:
java.lang.IllegalArgumentException - if size is <= 0

CompressionStream

public CompressionStream(AbstractStream clientStream,
                         Compressor comp,
                         Decompressor decomp)
Creates a new random access stream with the specified compressor and a default buffer size of 512.
Parameters:
clientStream - the random access stream
comp - the compressor ("deflater")
decomp - the decompressor ("inflater")

CompressionStream

public CompressionStream(AbstractStream clientStream,
                         Compressor comp)
Creates a new random access stream with the specified compressor and a default buffer size of 512.
Parameters:
clientStream - the random access stream
comp - the compressor ("deflater")

CompressionStream

public CompressionStream(AbstractStream clientStream,
                         Decompressor decomp)
Creates a new random access stream with the specified compressor and a default buffer size of 512.
Parameters:
clientStream - the random access stream
decomp - the decompressor ("inflater")

CompressionStream

public CompressionStream(AbstractStream clientStream,
                         int size)
Creates a new random access stream with the specified buffer size.
Parameters:
clientStream - the random access stream
len - the output buffer size
Throws:
java.lang.IllegalArgumentException - if size is <= 0

CompressionStream

public CompressionStream(AbstractStream clientStream)
Creates a new random access stream with a default buffer size of 512.
Parameters:
clientStream - the random access stream
Method Detail

read

public int read()
         throws java.io.IOException
Reads a byte of uncompressed data. This method will block until enough input is available for decompression.
Overrides:
read in class AbstractStream
Returns:
the byte read, or -1 if end of compressed input is reached
Throws:
java.io.IOException - if an I/O error has occurred

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads uncompressed data into an array of bytes. This method will block until some input can be decompressed.
Overrides:
read in class AbstractStream
Parameters:
b - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes read
Returns:
the actual number of bytes read, or -1 if the end of the compressed input is reached or a preset dictionary is needed
Throws:
java.util.zip.DataFormatException - if an archive format error has occurred
java.io.IOException - if an I/O error has occurred

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Reads data that is not compressed into an array of bytes. This method will block until some input can be decompressed.
Overrides:
readFully in class AbstractStream
Parameters:
b - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes read
Returns:
the actual number of bytes read, or -1 if the end of the compressed input is reached or a preset dictionary is needed
Throws:
ZipException - if a ZIP format error has occurred
java.io.IOException - if an I/O error has occurred

write

public void write(int b)
           throws java.io.IOException
Writes a byte to the compressed random access stream. This method will block until the byte can be written.
Overrides:
write in class AbstractStream
Parameters:
b - the byte to be written
Throws:
java.io.IOException - if an I/O error has occurred

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes an array of bytes to the compressed random access stream. This method will block until all the bytes are written.
Overrides:
write in class AbstractStream
Parameters:
outputBuffer - the data to be written
off - the start offset of the data
len - the length of the data
Throws:
java.io.IOException - if an I/O error has occurred

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Skips specified number of bytes of uncompressed data.
Overrides:
skipBytes in class AbstractStream
Parameters:
n - the number of bytes to skip
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if an I/O error has occurred
java.lang.IllegalArgumentException - if n < 0

resetEngine

public void resetEngine()
Retrieves new instances of the compressor and decompressor.

This should be used where the engines are designed to nolonger function upon the call to finalise.


getCompressor

public Compressor getCompressor()
Returns:
current instance of Compressor.

setCompressor

public void setCompressor(Compressor comp)
Assigns a new compressing engine.
Parameters:
Compressor - the new engine to perform the compression.

getDecompressor

public Decompressor getDecompressor()
Returns:
current instance of Decompressor.

setDecompressor

public void setDecompressor(Decompressor decomp)
Assigns a new compressing engine.
Parameters:
Compressor - the new engine to perform the compression.

finish

public void finish()
            throws java.io.IOException
Finishes writing compressed data to the random access stream without closing the underlying stream.
Overrides:
finish in class AbstractStream
Throws:
java.io.IOException - if an I/O error has occurred

availableCompress

public int availableCompress()
                      throws java.io.IOException
Returns 0 after EOF has reached, otherwise always return 1.

Programs should not count on this method to return the actual number of bytes that could be read without blocking.

Returns:
1 before EOF and 0 after EOF.
Throws:
java.io.IOException - if an I/O error occurs.

deflate

public void deflate()
             throws java.io.IOException
Writes next block of compressed data to the random access stream.

fill

protected void fill()
             throws java.io.IOException
Fills input buffer with more data to decompress.
Throws:
java.io.IOException - if an I/O error has occurred