-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.java
More file actions
103 lines (95 loc) · 2.75 KB
/
Decoder.java
File metadata and controls
103 lines (95 loc) · 2.75 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
98
99
100
101
102
103
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class Decoder {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//Code Table
BufferedReader br = new BufferedReader(new FileReader(args[1]));
String line;
HashMap<String, String> huff = new HashMap<String,String>();
while ((line = br.readLine()) != null) {
String tmp[] = line.split(" ");
huff.put(tmp[0], tmp[1]);
}
br.close();
//Construct Huffman Tree from Code Table
BinHeapNode root = new BinHeapNode(null, null);
BinHeapNode curr;
for (Map.Entry<String, String> entry : huff.entrySet()){
char[] charVal = entry.getValue().toCharArray();
curr = root;
for(int i = 0; i < charVal.length; i++){
if(charVal[i] == '0'){
if(curr.left != null)
curr = curr.left;
else{
if(i == charVal.length - 1)
curr.left = new BinHeapNode(entry.getKey(), null);
else
curr.left = new BinHeapNode(null, null);
curr = curr.left;
}
}
else{
if(curr.right != null)
curr = curr.right;
else{
if(i == charVal.length - 1)
curr.right = new BinHeapNode(entry.getKey(), null);
else
curr.right = new BinHeapNode(null, null);
curr = curr.right;
}
}
}
}
//Decode Binary File
byte[] bFile = Files.readAllBytes(Paths.get(args[0]));
StringBuilder sb = new StringBuilder();
for (int i=0; i < bFile.length;i++)
{
sb.append(String.format("%8s", Integer.toBinaryString(bFile[i] & 0xFF)).replace(' ', '0'));
}
String decodedString = sb.toString();
//Traverse through Huffman Tree to decode bits from Encoded Binary File
BufferedWriter buff = new BufferedWriter(new FileWriter("decoded.txt"));
int j=0;
while(j<decodedString.length())
{
BinHeapNode temp = root;
while(!(temp.left==null && temp.right==null))
{
if(decodedString.charAt(j)=='0')
{
temp = temp.left;
}
else
{
temp = temp.right;
}
j++;
}
buff.write(temp.data);
buff.newLine();
}
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}