Create BitInputStream and BitOutputStream Classes (cont.)

BitOutputStream: Writing Bits
Consider the BitOutputStream. Each BitOutputStream instance has an OutputStream to which it writes bytes. That is, the user code writes bits to the BitOutputStream, and the BitOutputStream writes bytes to its OutputStream.

Each BitOutputStream also has a buffer, which is a single byte:

 
Figure 1. The BitOutputStream. Each BitOutputStream has a stream to which it writes bytes, and a buffer, which is a single byte. The buffer accumulates bits until there are eight of them, at which point they can be written to the OutputStream.
Assume three bits have already been written, using the writeBit() method. These three bits exist in the buffer. Note that the class keeps track of the position where the next bit should be stored; and advances this "cursor" after writing each bit:

 
Figure 2. Some Bytes are Written Bits are stored in the buffer. The class keeps track of where to store the next bit with a "cursor" advanced after each write.
After eight bits have been written, the buffer is full—no more bits can be stored in it:

 
Figure 3. The Buffer is Full After eight bits have been written, the buffer is full.
At this point, the BitOutputStream writes the full byte to the OutputStream.

 
Figure 4. The Buffer is Written to the OutputStream The full buffer is written out to the OutputStream.
The buffer is cleared, and the cursor reset:

 
Figure 5. The Buffer Cleared and Cursor Reset The full buffer is written out to the OutputStream. The buffer is cleared, and the cursor reset.
The user code continues to write bits to the BitOutputStream. After another eight bits have been written, the class writes the filled buffer byte to the OutputStream:

 
Figure 6. Another Byte is Written After another eight bits have been written, a second byte is written to the OutputStream.
This process continues until the stream is closed. If the buffer is partially full when the stream is closed, it is written out as if it had contained eight bits. The remaining empty slots take on the default value of zero.



Creating a Bit Data Format

BitInputStream: Reading Bits

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