-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.java
161 lines (147 loc) · 4.07 KB
/
decoder.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Created by Shaan on 004, 4 Apr.
*/
public class decoder {
Node dec_root;
PrintWriter DW = null;
String encoded_path, code_table_path;
int max_len;
public decoder()
{
this.encoded_path = "encoded.bin";
this.code_table_path = "code_table.txt";
}
public decoder(String inp1, String inp2)
{
this.encoded_path = inp1;
this.code_table_path = inp2;
}
public void make_dec_tree(Node n, int val, String code)
{
if (code.length() == 0)
{
n.f = 0;
n.v = val;
}
else if (code.charAt(0) == '0')
{
if (n.l1 == null)
{
Node temp = new Node();
n.l1 = temp;
}
make_dec_tree(n.l1, val, code.substring(1));
}
else
{
if (n.r1 == null)
{
Node temp = new Node();
n.r1 = temp;
}
make_dec_tree(n.r1, val, code.substring(1));
}
}
public String dec_parse(Node n, String code)
{
if (n.f == 0)
{
//System.out.println(n.v);
DW.print(n.v + "\n");
return code;
}
else if (code.charAt(0) == '0')
{
code = dec_parse(n.l1, code.substring(1));
}
else
code = dec_parse(n.r1, code.substring(1));
return code;
}
public void dec_start_decoding() throws IOException
{
FileInputStream fis = null;
try
{
byte[] data = new byte[1];
String bin = "";
String temp;
fis = new FileInputStream(encoded_path);
DW = new PrintWriter("decoded.txt");
int k = 0;
//fis.read(data);
//for(byte b:data) {
while(fis.read(data) != -1)
{
int c = (int)data[0];
if (c < 0)
c = 256 + c;
temp = Integer.toBinaryString(c);
while (temp.length() != 8)
temp = "0" + temp;
bin += temp;
if (bin.length() >= max_len * 2) {
bin = dec_parse(dec_root, bin);
}
}
while (bin.length() != 0)
bin = dec_parse(dec_root, bin);
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
fis.close();
DW.close();
}
}
public void dec_code_table()
{
Scanner scan = null;
try
{
dec_root = new Node();
scan = new Scanner(new BufferedInputStream(new FileInputStream(code_table_path)));
int firstI = scan.nextInt();
String firstS = scan.next();
max_len = firstS.length();
make_dec_tree(dec_root, firstI, firstS);
while(scan.hasNext()) {
int temp1 = scan.nextInt();
String temp2 = scan.next();
if (temp2.length() > max_len)
max_len = temp2.length();
make_dec_tree(dec_root, temp1, temp2);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
scan.close();
}
}
public static void main(String[] args) {
try {
decoder dec;
if (args.length == 2) {
dec = new decoder(args[0], args[1]);
dec.dec_code_table();
dec.dec_start_decoding();
}
else
System.out.println("Please enter the filenames!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}