Create BitInputStream and BitOutputStream Classes

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.

by Greg Travis

he classes in the java.io and java.nio packages make it easy to read and write just about any kind of data—such as bytes, or arrays of bytes. Other classes make it easy to read and write other data types; however, all these classes read and write data in pieces that are at least a byte long. For example, the DataInputStream and DataOutputStream classes can write Boolean values, which require—in theory—only a single bit to store. These classes, however, use an entire byte for each Boolean value.

In contrast, this article describes a pair of stream classes that let you read and write single bits easily. Not a bit stored as a byte, but a real bit. They treat the data stream as a stream of bits, rather than a stream of bytes. You don't have to think in terms of bytes at all.

This kind of facility is particularly useful when you are trying to save space. Some file formats store certain values using bit-lengths other than 8, 16, or 32. Others—such as data compression formats—benefit from being able to forget about byte boundaries entirely, and to treat data as a homogenous stream of bits.

How BitInputStream and BitOutputStream Work
Before getting into the nitty-gritty, here's a quick glance at how you might use these classes. In many ways, they're like regular InputStream and OutputStream classes, except that they act on bits rather than bytes. Here's a program fragment that writes three bits—1, 1, and 0—to a file.

   FileOutputStream fout =
     new FileOutputStream( "bits.dat" );
   BitOutputStream bout = new BitOutputStream( fout );
   
   bout.writeBit( 1 );
   bout.writeBit( 1 );
   bout.writeBit( 0 );
   
   bout.close();

Likewise, here's some code to read them back in:

   FileInputStream fin = new FileInputStream( "bits.dat" );
   BitInputStream bin = new BitInputStream( fin );
   
   int b0 = bin.readBit();
   int b1 = bin.readBit();
   int b2 = bin.readBit();
   
   bin.close();

Before learning how they work, you should think a little bit about how bits are stored in files.


 
Creating a Bit Data Format

How BitInputStream and BitOutputStream Work Creating a Bit Data Format
BitOutputStream: Writing Bits BitInputStream: Reading Bits


 




 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.
Click here to Join
Get the Code for this Article

J2SE 1.4

The java.io Package

Sun's Tutorial on the java.io Package





 
Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers

Copyright Information/Privacy Statement