From f195b6c61bb9657ba31015d4251c9605b70384de Mon Sep 17 00:00:00 2001 From: bsherman101 Date: Sat, 10 Sep 2022 22:58:18 -0400 Subject: [PATCH] Challenge Submittal Build and Run instructions are in the README --- Decoder.java | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 5 +++ 2 files changed, 102 insertions(+) create mode 100644 Decoder.java diff --git a/Decoder.java b/Decoder.java new file mode 100644 index 0000000..1732cbb --- /dev/null +++ b/Decoder.java @@ -0,0 +1,97 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Decoder { + + StringBuilder preamble = new StringBuilder(); + StringBuilder response = new StringBuilder(); + + public Decoder() { + run(); + } + + public void run() { + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + boolean preambleFound = false; + + int bit = 0; + + + while (bit > -1) { + try { + //Read one character at a time + bit = reader.read(); + char bitChar = (char) bit; + + // Check if bit is a 0 or 1 + if (bit == 49 || bit == 48) { + + //If the preamble has not been found we need to keep looking for it + if (!preambleFound) { + + //if the preamble stringbuilder queue is at 88 then check if it equals CAPTIVATION + if (preamble.length() >= 88) { + + // Does preamble = CAPTIVATION? + String decodedPreamble = decodeBinaryString(preamble.toString()); + if (decodedPreamble.compareTo("CAPTIVATION") == 0) { + //the preamble was found. set a flag and reset the preamble stringbuilder queue + preambleFound = true; + preamble = new StringBuilder(); + + //Add current bit we just received to response string + response.append(bitChar); + } else { + //preamble not found, dequeue character from stringbuilder + preamble.deleteCharAt(0); + //append new character to end of string builder. + preamble.append(bitChar); + } + + } else { + //preamble stringbuilder isnt long enough to be the preamble yet, so simply append to it. + preamble.append(bitChar); + } + + } else { + // Preamble was found now accumulate next 100 characters and then print to screen. + if (response.length() < 800) { + // keep adding to response string + response.append(bitChar); + } else { + // We have 100 characters, print to screen + System.out.println(decodeBinaryString(response.toString())); + + // reset response string holder + response = new StringBuilder(); + // reset preambleFound Flag + preambleFound = false; + + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + //Converts a string of 1's and 0's to a character string + public String decodeBinaryString(String binaryString) { + int intChar; + String stringChar; + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < binaryString.length(); i += 8) { + intChar = Integer.parseInt(binaryString.substring(i, i + 8), 2); + stringChar = Character.toString((char) intChar); + stringBuilder.append(stringChar); + } + return stringBuilder.toString(); + } + + public static void main(String arg[]) { + new Decoder(); + } +} \ No newline at end of file diff --git a/README.md b/README.md index 1ba1922..bddbae4 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,8 @@ Develop an application that: - Your solution will be judged for correctness, performance, and style - You may use any language you'd like, but you can only use standard libraries - Your solution must be your own original work + +## Build & Run Instructions +- Ensure the openJDK 8 is installed +- In the directory where the Decoder.java file is, run "javac Decoder.java" in a terminal to compile the class +- In the same directory run "java Decoder" to execute the app.