 |
|
| |
Create BitInputStream and BitOutputStream Classes (cont.)
|
|
|
BitInputStream: Reading Bits
The process of reading from a BitInputStream is similar. Just as a BitOutputStream has an OutputStream, a BitInputStream has an InputStream. And it also has a byte buffer:
 | |
| The BitInputStream transfers one byte at a time from the InputStream to the buffer. |
Note that the InputStream is shown already filled with data. This is because this InputStream is the source of the bits for the BitInputStream. The amount of data depends on the contents of the InputStream.
The first time that user code calls the readBit() method, there are no bits in the buffer. The BitInputStream must first read a single byte from the InputStream and store it in the buffer.
 | |
| Figure 8. Filling the Buffer Before reading bits, the class must fill the buffer with data from the InputStream. |
Now that the buffer contains some bits, you can return the first one to the calling code. You must also remember to advance the cursor.
 | |
| Figure 9. Returning a Bit Now that the buffer contains some bits, it can return one to the calling code. |
Each time the user calls readBit(), the BitInputStream returns another bit from the buffer.
 | |
| Figure 10. More calls to readBit() Each time the user calls <span class="pf">readBit()</span>, the BitInputStream returns another bit from the buffer. |
After reading the eighth bit, there are no more bits available in the buffer. The cursor has moved "off the end" of the buffer.
 | |
| Figure 11. The Buffer is Drained. There are no more bits available in the buffer. |
The next time the code calls readBit() is called, you must get another byte from the InputStream and place that in the buffer.
 | |
| Figure 12. Refilling the buffer. The BitInputStream gets another byte from the InputStream and places it in the buffer. |
After refilling the buffer, you can return the next bit and advance the cursor.
 | |
| Figure 13. Returning another Bit. Return the first bit from the newly-filled buffer. |
Write a Unit Test
To verify that the classes work correctly, here's a quick test program. The program called FileCopy (available with the downloadable code for this article) copies a file one bit at a time. You can use the FileCopy program from the command line as follows:
java FileCopy fromFile toFile
This command copies a file from fromFile to toFile.
The inner loop of this program is very simple:
while (true) {
bout.writeBit( bin.readBit() );
}
Each cycle through the loop reads a bit from the BitInputStream connected to fromFile, and writes it to the BitOutputStream connected to toFile. Verifying that this program makes a perfect copy of a file proves that the buffering code in the BitInputStream and BitOutputStream classes described in the previous section work properly.
This article describes the implementation of a pair of classes, BitInputStream and BitOutputStream that read and write individual bits. While these classes aren't strictly subclasses of the stream classes from the java.io package, they operate in a very similar way, and the implementation makes use of the java.io package. They can be used in ways to the more familiar stream classes.
From a design point of view, these classes are interesting because they create a filter that converts one kind of class to another. The BitInputStream hides a regular InputStream, and the BitOutputStream hides a regular OutputStream.
Greg Travis is a free-lance Java programmer and technology writer living in New York City. After spending 3 years in the world of high-end PC games, he joined EarthWeb, where he developed new technologies with the then-new Java programming language. Since 1997, he has been a consultant in a variety of Web technologies. He can be reached at
mito@panix.com.
|
| |
 |
TALK
BACK |
|
While the JDK 1.4 contains most of the basic Stream classes you need, it doesn't support reading and writing individual bits. Have you ever needed this functionality? Have you written your own bit-aware Stream classes? How did you like the sample code? Let us know in the java.general discussion group.
|
|
|
|
|
|
|