public class CompressEngine
extends java.lang.Object
compress
method reads an InputStream
and writes a compressed version to an OutputStream.
The compression algorithm is that of the Unix program
compress
. The output is suitable for HTTP
transmission with Content-Encoding: compress.
This code was transposed by Fred Hansen, January, 2008, from the original C:
compress.c - File compression ala IEEE Computer, June 1984.
Authors: Spencer W. Thomas (decvax!utah-cs!thomas)
Jim McKie (decvax!mcvax!jim)
Steve Davies (decvax!vax135!petsd!peora!srd)
Ken Turkowski (decvax!decwrl!turtlevax!ken)
James A. Woods (decvax!ihnp4!ames!jaw)
Joe Orost (decvax!vax135!petsd!joe)
Algorithm from "A Technique for High Performance Data Compression",
Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.Constructor and Description |
---|
CompressEngine(InputStream instr,
OutputStream outstr)
creates a CompressEngine object that will compress the
data in an InputStream and write the result to an OutputStream.
|
Modifier and Type | Method and Description |
---|---|
boolean |
compress()
compresses
instr to outstr . |
void |
input_size(int fsize)
sets the expected size of the input stream.
|
public CompressEngine(InputStream instr, OutputStream outstr)
instr
- the data to compress.outstr
- the compressed data,
preceded by a three byte magic number.public void input_size(int fsize)
compress
method.fsize
- The expected length of the file to be compressed.public boolean compress() throws IOException
instr
to outstr
.
Algorithm: use open addressing double hashing (no chaining) on the
prefix code / next character combination. We do a variant of Knuth's
algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
secondary probe. Here, the modular division first probe gives way
to a faster exclusive-or manipulation. Also do block compression with
an adaptive reset, whereby the code table is cleared when the compression
ratio decreases, but after the table fills. The variable-length output
codes are re-sized at this point, and a special CLEAR code is generated
for the decompressor. Late addition: construct the table according to
file size for noticeable speed improvement on small files. Please direct
questions about this implementation to ames!jaw.IOException
- if reading or writing fails.