Skip to content

Challenge Submittal #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Decoder.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.