-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder.java
251 lines (217 loc) · 7.05 KB
/
encoder.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class encoder {
public static HashMap<String, Integer> hashMap = new HashMap<>();
public static HashMap<String, String> codeMap = new HashMap<>();
private static String FILENAME = null;
private static final String CODEFILE = "code_table.txt";
private static final String BINFILE = "encoded.bin";
public String[] code;
public static void createFrequencyTable() {
hashMap.clear();
FileReader in;
try {
in = new FileReader(FILENAME);
BufferedReader br = new BufferedReader(in);
String newLine;
while ((newLine = br.readLine()) != null) {
newLine = newLine.trim();
if (!newLine.equals("")) {
if (hashMap.get(newLine) == null) {
hashMap.put(newLine, 1);
} else {
hashMap.put(newLine, hashMap.get(newLine) + 1);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("No file to write to.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO Exception.");
e.printStackTrace();
}
}
public static BinaryHeap buildBinaryHeap(HashMap freqtable) {
BinaryHeap bh = new BinaryHeap(freqtable.size());
for (Entry<String, Integer> entry : hashMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
bh.insert(new Node(key, value));
}
return bh;
}
public static PairingHeap buildPairingHeap(HashMap freqtable) {
PairingHeap ph = new PairingHeap(freqtable.size());
for (Entry<String, Integer> entry : hashMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
ph.insert(new Node(key, value));
}
return ph;
}
public static FourWayHeap buildFourWayHeap(HashMap freqtable) {
FourWayHeap fwh = new FourWayHeap(freqtable.size());
for (Entry<String, Integer> entry : hashMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
fwh.insert(new Node(key, value));
}
return fwh;
}
public static Node buildHuffman(HashMap<String, Integer> freqtable, int type) {
createFrequencyTable();
if(freqtable.size()==0){
return null;
}
BinaryHeap binaryHeap = null;
switch (type) {
case 0: {
binaryHeap = buildBinaryHeap(freqtable);
while (binaryHeap.getSize() != 1) {
Node n1 = binaryHeap.extract_min();
Node n2 = binaryHeap.extract_min();
int freqSum = n1.getFrequency() + n2.getFrequency();
Node newNode = new Node("" + System.currentTimeMillis(), freqSum);
newNode.setLeft(n1);
newNode.setRight(n2);
newNode.setParent(null);
binaryHeap.insert(newNode);
}
return binaryHeap.extract_min();
}
case 1: {
FourWayHeap fourWayHeap = buildFourWayHeap(freqtable);
while (fourWayHeap.getSize() != 1) {
Node n1 = fourWayHeap.extract_min();
Node n2 = fourWayHeap.extract_min();
int freqSum = n1.getFrequency() + n2.getFrequency();
Node newNode = new Node("" + System.currentTimeMillis(), freqSum);
newNode.setLeft(n1);
newNode.setRight(n2);
newNode.setParent(null);
fourWayHeap.insert(newNode);
}
return fourWayHeap.extract_min();
}
case 2: {
PairingHeap pairingHeap = buildPairingHeap(freqtable);
while (pairingHeap.getSize() != 1) {
Node n1 = pairingHeap.extract_min().getNode();
Node n2 = pairingHeap.extract_min().getNode();
int freqSum = n1.getFrequency() + n2.getFrequency();
Node newNode = new Node("" + System.currentTimeMillis(), freqSum);
newNode.setLeft(n1);
newNode.setRight(n2);
newNode.setParent(null);
pairingHeap.insert(newNode);
}
return pairingHeap.extract_min().getNode();
}
default: {
System.out.println("Not a correct value");
return null;
}
}
// return binaryHeap;
}
public static void generateCode(Node node, String code) {
// codeMap.clear();
if (node != null) {
if (node.getLeft() == null && node.getRight() == null) {
codeMap.put(node.getValue(), code);
} else {
generateCode(node.getLeft(), code + "0");
generateCode(node.getRight(), code + "1");
}
}
}
public static void writeOntoCodeTable(HashMap codemap) {
try {
PrintWriter writer = new PrintWriter(CODEFILE, "UTF-8");
for (Entry<String, String> entry : codeMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
writer.println(key + " " + value);
}
writer.close();
} catch (IOException e) {
System.out.println("Not able to write file");
}
}
public static void writeOntoEncodedFile() {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
FileReader in;
try {
in = new FileReader(FILENAME);
BufferedReader br = new BufferedReader(in);
String newLine;
StringBuilder stringBuilder = new StringBuilder();
while ((newLine = br.readLine()) != null) {
newLine = newLine.trim();
// System.out.println(newLine);
if (!newLine.equals("")) {
String value = codeMap.get(newLine);
stringBuilder.append(value);
while (stringBuilder.length() >= 8) {
String str = stringBuilder.substring(0, 8);
Integer intValueOfStr = null;
try {
intValueOfStr = Integer.parseInt(str, 2);
} catch (Exception e) {
System.out.println("sdasdfsf");
}
byte byteValueOfStr = intValueOfStr.byteValue();
byteArrayOutputStream.write(byteValueOfStr);
stringBuilder.delete(0, 8);
}
}
}
byte[] byteArray = byteArrayOutputStream.toByteArray();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(BINFILE));
bufferedOutputStream.write(byteArray);
bufferedOutputStream.close();
byteArrayOutputStream.close();
br.close();
in.close();
} catch (FileNotFoundException e) {
System.out.println("No file to write to.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO Exception.");
e.printStackTrace();
}
}
public static void main(String args[]) {
if (args.length == 1) {
FILENAME = args[0];
} else {
System.out.println("Wrong number of arguments provided.");
System.out.println("Correct usage: java encoder <input_file_name>");
System.exit(0);
}
double startTime = 0;
startTime = System.currentTimeMillis();
System.out.println("Starting to encode");
Node binarynode = buildHuffman(hashMap, 1);
System.out.println("Huffman Tree Generation Done. Proceeding to encode..");
System.out.println("Time for tree generation: " + (System.currentTimeMillis() - startTime) / 1000 + " seconds");
generateCode(binarynode, "");
System.out.println("Encoding done. Printing values to code file..");
writeOntoCodeTable(codeMap);
System.out.println("Code table written. Writing encoded binary file..");
writeOntoEncodedFile();
System.out.println("Done");
System.out.println(
"Time to encode with Binary Heap " + (System.currentTimeMillis() - startTime) / 1000 + " seconds");
}
}