Question
Need the coding in java please We are going to write a class to output bits to an output stream Start with the template file
Need the coding in java please
We are going to write a class to output bits to an output stream
Start with the template file for BitOutputStream.java
You will submit a completed version of this file.
You must fill in the WriteBit(char bit), WriteBit(String bit) and close() functions.
Fill in the blank functions so that the class buffers up bits and outputs them to a file once 8 bits have been provided.
Bits are input as either the character 0 or 1.
May also be input as a string, such as 0010010.
Details
Take the character being input and determine if it is a 1 or 0 then save that to some intermediate buffer
The first character entered is the most significant bit of the byte
For example:
bitOutputStream.write(1) Right now the byte is 1
bitOutputStream.write(0) Now the byte is 10 (2)
bitOutputStream.write(01) Now the byte is 1001 (9)
bitOutputStream.write(0110) 8 bits have been entered so the number 150 is output to the file (10010110).
When the close() method is called, output whatever bits are in the buffer (even if less than 8).
Main File
I have provided a class template Homework5.java which you may use as your main file. This will call the BitOutputStream class with the following lines:
BitOutputStream bitOut = new BitOutputStream(buffered);
bitOut.WriteBit('0'); //byte is 0
bitOut.WriteBit('1'); //byte is 01
bitOut.WriteBit("101110"); //byte is 01101110 = 110 in decimal is written to the file
bitOut.WriteBit("110"); //byte is 110
bitOut.close(); //byte is 110=6 in decimal is written to the file
This outputs the decimal values 110 and 6 to the binary file. example.bin was output by this program and only contains 2 bytes. This screenshot from a hex editor (Homework 4.png) shows the values 0x6E and 0x06 are in the file.
Advice
You could use a stack of Booleans to represent the 0 and 1
Then when the stack size is at 8, translate the values from binary to a decimal byte variable and output it
What would that algorithm look like?
int exp = 1
byte b = 0
From 0 to the size of the stack
Pop out the next Boolean
If it is true, b += exp
exp *= 2
///BitOutputStream.java
import java.util.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.io.*;
public class BitOutputStream
{
//I found a Stack of Boolean values to be a useful data structure
//But this is optional, feel free to use something else
Stack
OutputStream os;
BitOutputStream(OutputStream file)
{
buffer = new Stack
os = file;
}
public void WriteBit(char bit)
{
}
public void WriteBit(String bit)
{
}
public void close()
{
}
//This is an optional function to convert the buffer to a Byte and output it
to os
//I found it useful to have
private void OutputByte()
{
}
}
//// Homework5.java
import java.util.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.io.*;
public class Homework5 {
public static void main(String[] args) {
Path path = Paths.get("example.bin");
try (OutputStream outputStream = Files.newOutputStream(path,
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
BufferedOutputStream buffered =
new BufferedOutputStream(outputStream)) {
BitOutputStream bitOut = new BitOutputStream(buffered);
bitOut.WriteBit('0');
bitOut.WriteBit('1');
bitOut.WriteBit("101110");
bitOut.WriteBit("110");
bitOut.close();
} catch (IOException e) {
// do something with e
}
}
}
Show transcribed image text
Transcribed image textStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started