forked from captivationsoftware/DeveloperChallenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.java
More file actions
97 lines (78 loc) · 2.97 KB
/
Decoder.java
File metadata and controls
97 lines (78 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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();
}
}