-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decode.java
94 lines (85 loc) · 3.12 KB
/
Decode.java
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
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Decode {
static HashMap<Character, String> createCanonicalCodeBook(Object[] chars , int[] len) {
HashMap<Character, String> hashMap = new HashMap<>();
String code = "";
for(int i=0; i<len[0]; i++) code += "0";
for (int i=0; i<chars.length; i++) {
hashMap.put((char) chars[i], code);
if (i+1 < chars.length) {
int length = code.length();
code = Integer.toBinaryString( Integer.parseInt(code, 2) + 1 );
while (code.length() < length) code = "0" + code;
while (code.length() < len[i+1]) code = code + "0"; //shift
}
}
return hashMap;
}
public static Character getKeyByValue(HashMap<Character,String> hashMap,String value) {
for (Map.Entry<Character, String> entry : hashMap.entrySet()) {
if (entry.getValue().equals(value)) {
return entry.getKey();
}
}
return null;
}
public static void main(String[] arg){
HashMap<Character, String> canonicalCodeBook;
PrintWriter output = null;
try{
output = new PrintWriter(
new BufferedWriter(
new FileWriter(
new File("decodedFile.txt"))));
}catch (IOException e){
e.printStackTrace();
}
DataInputStream input = null;
try{
input = new DataInputStream(
new BufferedInputStream(
new FileInputStream(
new File("EncodedFile.bin"))));
int count , maxLen;
count = input.readInt();
String line = "";
Object[] chars = new Object[count];
int[] len = new int[count];
for(int i =0 ; i<count ; i++){
chars[i] = input.readChar();
len[i] = input.readInt();
}
maxLen = len[len.length-1];
canonicalCodeBook = createCanonicalCodeBook(chars,len);
String codeLine = input.readLine();
for(int i=1 ; i<codeLine.length() ; i+=2){
line = line.concat(String.valueOf(codeLine.charAt(i)));
}
int k = 0;
for(int i = 0 ; i<line.length();i+=k){
k=0;
String code = "";
for( int j = i + 1; j<= i + maxLen ; j++){
k++;
code = line.substring(i,j);
if(canonicalCodeBook.containsValue(code)) {
output.print(Decode.getKeyByValue(canonicalCodeBook, code));
break;
}
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
try{
input.close();
output.close();
}catch (IOException e){
e.printStackTrace();
}
}
System.out.println("Decoding finished successfully");
}
}