Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a class named BitOutputStream, as shown below, for writing bits to an

ID: 3751597 • Letter: I

Question

Implement a class named BitOutputStream, as shown below, for writing bits to an output stream. The writeBit(char bit) method stores the bit in a byte variable.

When you create a BitOutputStrea, the byte is empty.

After invoking writeBit(‘1’), the byte becomes 0000 0001.

After invoking writeBit(“0101”), the byte becomes 0001 0101.

The firest three bits are not filled yet.

When a byte is full, it is sent to the output stream.

Now the byte is reset to empty.

You must close the stream by invoking the close() method.

If the byte is neither empty nor full, the close() method first fills the zeros to make a full 8 bits in the byte, and then outputs the byte and closed the stream.

Write a test program that sends the bits 0 1000 0100 1000 0100 1101 to the file named Assignment04.dat (submit this file too)

BitOutputStream

+BitOutputStream(file: File)

+writeBit(char bit): void

+writeBit(String bit): void

+close(): void

BitOutputStream

+BitOutputStream(file: File)

+writeBit(char bit): void

+writeBit(String bit): void

+close(): void

Explanation / Answer

package assignment;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class BitOutputStream {

private File file;

private byte buffer;

private FileOutputStream fos;

private int numBitsOccupied = 0;

public BitOutputStream(File file) {

super();

this.file = file;

try {

fos = new FileOutputStream(this.file);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

public void writeBit(char bit) {

System.out.println("Buffer : " + buffer);

buffer = (byte) (buffer << 1);

if (bit == '1') {

buffer = (byte) (buffer | 1);

}

numBitsOccupied++;

if (numBitsOccupied == 8) {

writeBufferToFile();

numBitsOccupied = 0;

buffer = 0;

}

}

public void writeBit(String bits) {

if (null != bits) {

for (char bit : bits.toCharArray()) {

writeBit(bit);

}

}

}

public void close() {

if (numBitsOccupied < 8) {

buffer = (byte) (buffer << (8 - numBitsOccupied));

}

writeBufferToFile();

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

numBitsOccupied=0;

}

private void writeBufferToFile() {

try {

for (int i = 1; i <= 8; i++) {

if ((buffer & 0x80) != 0) {

fos.write('1');

} else {

fos.write('0');

}

buffer = (byte) (buffer << 1);

}

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws IOException {

BitOutputStream bos = new BitOutputStream(new File("Assignment04.dat"));

bos.writeBit('0');

bos.writeBit("1000");

bos.writeBit("0100");

bos.writeBit("1000");

bos.writeBit("0100");

bos.writeBit("1101");

bos.close();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote