It is useful to note that while an interface is included in this example, an interface is not always required. Now that the interface for sharing information between the input and output streams has been defined, we will complete step 1, which was to write a class that extends java.io.FilterOutputStream to create an output stream for the socket, overriding FilterOutputStream methods as necessary. Below is the source code for the class CompressionOutputStream.java . An explanation of the compression algorithm can be found in the comments of the source code. An explanation of the implementation follows the code.
Constructor calls constructor of superclass.
Five 6-bit codes fit into 4 words. This method writes one byte to the socket stream. Look up pos in codeTable to get its encoding. This method writes up to len bytes to the socket stream. First, CompressionOutputStream subclasses FilterOutputStream .