Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Create BitInputStream and BitOutputStream Classes : Page 4

Although the JDK 1.4 has many Stream classes, it doesn't contain classes for reading and writing single bits. Extend your tool set by creating these BitInputStream and BitOutputStream classes.


advertisement
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 Java programmer and technology writer, living in New York City. After spending three 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.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date