Lab 16: Reading bits and bytes from a file
You are to write a reader class that reads a file in bits.
Reading in bits
Create the following class BitReader. The class should have one constructor:
- BitReader(InputStream inStream)
Sets the data stream that you will read from inStream. For example, to open a file
for reading, you can type the following code in the interactions pane or add it to a program that will
use the BitReader:
File file = new File(filename)
if (file.exists() && file.canRead()) {
BitReader reader = new BitReader(new FileInputStream(file));
You can not directly read a bit at a time from a file. The smallest unit you can read is a byte
using the read() method of InputStream. What you will need to do is maintain a
1 byte buffer and an index into the buffer. The first time a next method is called, read in 1 byte into the byte buffer
and set the index to the first bit of the buffer. The index keeps track of the next bit to read from the buffer. Each
time nextBit is called, return the bit pointed to by the index and increment the index.
When the buffer is empty, you refill the buffer by reading the next byte from the InputStream.
Now add the following methods:
- boolean nextBit() throws IOException
Returns true if the next bit from the file is a 1 and false if the next bit is 0.
The code should work as follows:
- If the next index to read less than 0, read the next byte from the input stream and store it in the buffer. Set the
index to 7.
- Extract the bit and the index from the buffer.
- Decrement the index.
- Return true if the bit read was 1 and false if it was 0.
- byte nextByte() throws IOException
Returns the next 8 bits of the file packed into a byte.
The code should work as follows. Set a byte to 0, and then for i goes from 7 to 0, get the next bit using
the above method and set the ith bit of the byte to the appropriate value. Then return the byte.
- int nextInt() throws IOException
Returns the next 32 bits of the file packed into an int.
The code should work as follows. Set an int to 0, and then for i goes from 3 to 0, get the next byte
using the above method and set the ith byte of the int to the appropriate value. Then return the int.
- boolean moreData() throws IOException
Returns true if there is more data to read. Use the available method for InputStream to tell if there
is more data in the stream, and also check to see if there are still more bits to read in your buffer.
Optional improvement
It is actually very inefficient to read one byte at a time from a file. It is much faster to read in a
large chunk at a time. You can do this by creating a second buffer that is an array of bytes.
I suggest using a large power of 2, such as 1024, 2048, 4096 (1K, 2K, 4K), bytes for the buffer. That will
better match the page size of the computers memory. Then you use the read(byte[] b) method
of InputStream to
read in the entire array at a time. The read returns the number of bytes read. You will need to
know this! Then the 1 byte buffer will refill by getting the next byte from the byte array. When the
array is depleted, you refill the array from the file.
At the end of the lab, email what you have to your instructor, hsc@albion.edu.