Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Convert InputStream to binary, hexadecimal, and UTF-8 Strings. package edu.ics211.h01; import java.io.IOException; import java.io.InputStream; /** * Translates InputStream to binary string, hexadecimal string, and a

Convert InputStream to binary, hexadecimal, and UTF-8 Strings.

package edu.ics211.h01;

import java.io.IOException;

import java.io.InputStream;

/**

* Translates InputStream to binary string, hexadecimal string, and a UTF-8 string.

*

* @author .

*

*/

public interface Translate {

/**

* Returns the contents of the InputStream as a binary String.

* @param in the InputStream.

* @return The contents of the InputStream as a binary String.

*/

String asBinaryString(InputStream in) throws IOException;

/**

* Returns the contents of the InputStream as a hexadecimal String.

* @param in the InputStream.

* @return The contents of the InputStream as a hexadecimal String.

* @throws IOException

*/

String asHexadecimalString(InputStream in) throws IOException;

/**

* Returns the contents of the InputStream as a UTF-8 String.

* @param in the InputStream.

* @return The contents of the InputStream as a UTF-8 String.

*/

String asUtf8String(InputStream in) throws IOException;

}

package edu.ics211.h01;

import java.io.InputStream;

/**

* This represents a Translator.

*

* @author .

*/

public class Translator implements Translate {

public Translator() {

// TODO Auto-generated constructor stub

}

@Override

public String asBinaryString(InputStream in) {

// TODO Auto-generated method stub

return null;

}

@Override

public String asHexadecimalString(InputStream in) {

// TODO Auto-generated method stub

return null;

}

@Override

public String asUtf8String(InputStream in) {

// TODO Auto-generated method stub

return null;

}

}

image text in transcribed

image text in transcribed

image text in transcribed

package edu.ics211.h01; import java.io.IOException; import java.io.InputStream; /** * Represents a BitReader. * * @author . * */ public class BitReader implements AutoCloseable { private boolean done; private byte buffer = 0; private int bits = 0; private InputStream in; /** * Creates a BitReader for the given input stream. * * @param in The input stream to read from. * @throws IOException if there is a problem with the InputStream. */ public BitReader(InputStream in) throws IOException { this.done = false; this.bits = 0; this.in = in; fillBuffer(); } /** * Reads a Byte from the input stream. * * @return a Byte from the input stream. */ public Byte readByte() { byte buf = 0; for (int i = 0; i  

Please implement methods and build the program. Thanks.

package edu.ics211.01; import static org.junit. Assert.assertEquals; import java.io.ByteArrayInputStream; import org.junit. jupiter.api.BeforeEach; import org.junit.jupiter.api. Test; class TranslatorTest { private final String testData = "Hello World from ICS 211. This is the second line."; private final String binaryData = "0100100001100101011011000110110001101111001000000101011101101" + "11101110010011011000110010000100000011001100111001001101111011011010010000001001001010000" 01100100 + "11010100110010000000110010001100010011000100101110000010100101010001101000011010010111001" + "10010000001101001011100110010000001110100011010000110010100100000011100110110010101100011" + "011011110110111001100100001000000110110001101001011011100110010100101110"; private final String hexData = "48656c6c6f20576F726c642066726f6d20494353203231312e0a" + "5468697320697320746865207365636f6e642060696e652e"; private byte[] byteArray; private Translator translator; /** * Sets up the member variables. * @throws java.lang. Exception if there is a problem. */ @BeforeEach void setup() throws Exception { this.byteArray = testData.getBytes("UTF-8"); this.translator = new Translator(); } /** * Test method for {@link edu.ics211.901. Translator#asBinaryString(java.io.InputStream)}. */ @Test void testAsBinaryString() { String binary = translator.asBinaryString(new ByteArrayInputStream(byteArray)); assertEquals("Got wrong binary String", binaryData, binary); } /** * Test method for {@link edu.ics 211.01. Translator#asHexadecimalString(java.io.InputStream)}. @Test void testAsHexadecimalString() { String hex = translator.asHexadecimalstring(new ByteArrayInputStream(byteArray)); assertEquals("Got wrong hexadecimal String", hexData, hex); } /** * Test method for {@link edu.ics211.01. Translator#asutf8String(java.io.InputStream) } . */ @Test void testAsUtf8String() { String utf8 = translator.asUtf8String(new ByteArrayInputStream(byteArray)); assertEquals("Got wrong UTF-8 String", testData, utf8); } } 5. Implement the asBinaryString method using the BitReader class Think about the steps you need to do to implement this method. Don't worry about the other methods, focus on the asBinaryString method. Using an instance of the BitReader class we need to read the InputStream bit by bit and append those bits to the String we are going to return. So the steps might be: 1. Create an instance of the BitReader class. Create a BitReader class in edu.ics211.h01 and replace the contents with BitReader.java. You can create a BitReader instance from an InputStream. 2. Create an empty String or even better use Java's StringBuilder class. 3. Loop until the BitReader is done appending the bits to the String or StringBuilder. The BitReader instance then will read the InputStream bit by bit. You can use the public boolean read or the public int readAsint() methods to read a single bit from the InputStream. 4. Return the String or StringBuilder.toString(). Put the steps into your code using comments. Your asBinaryString method should look something like: @Override public String asBinaryString(InputStream in) { // create BitReader from InputStream // create StringBuilder // loop until BitReader is done append 0 or 1 from BitReader to StringBuilder // return String or StringBuilder.toString return null; } Then add the Java code to accomplish the steps. Leave your step comments in the code. After implementing the asBinaryString method you may want to run the JUnit tests to see if you got it right. Remember to focus on the problem at hand and not worry about the other methods until you get to them. Once the JUnit test passes you can move on to Task 6. 6. Implement the asHexadecimalString method Now focus on solving the problem of building a Hexadecimal String from the InputStream. We don't need to use the BitReader for this problem. The steps might be: 1. Create an empty String or even better use Java's StringBuilder class. 2. Loop until the InputStream is done, reading in a byte at a time. We then need to convert that byte to a hexadecimal string. You can use the Integer.toHexString method to convert that byte to a hexadecimal string. 3. Return the String or StringBuilder.toString(). Once the JUnit test passes we can move on to Task 7 the astf8String method. Hint: You might find that your asHexadecimalString method doesn't pass test. The reason is likely a missing leading 0. How many leading O's do we add to numbers? Also, how many bits are represented by a single hexadecimal digit? 7. Implement the asUtf8String method To build a UTF-8 String we can use Java's String class has a constructor that takes an array of bytes and an encoding scheme. You can create an array of bytes then fill it from the InputStream then return return new String (bytes, "UTF-8"); So the steps might be: 1. Create an array of bytes. 2. Fill the array from the InputStream. 3. Return the newly constructed String. One issue with this solution is how do we know how big an array to build? Hint: You can ask the InputStream how many bytes it has. Take a look at the InputStream Javadoc and see if you can figure out how many bytes it has. Once all the JUnit tests pass, and you have no checkstyle issues then you can confidently turn in your

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions