-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.java
More file actions
72 lines (61 loc) · 2.32 KB
/
decoder.java
File metadata and controls
72 lines (61 loc) · 2.32 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
package ADSHuffman;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author Kanika Gupta
*
*/
public class decoder {
public static void main(String args[]) {
long startTime = System.currentTimeMillis();
String codeTable_file = args[1];
String encoded_file = args[0];
parse_code_table_and_build_decode_tree(codeTable_file);
parse_encoded_file_and_decode(encoded_file);
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Time using decoder: " + totalTime);
}
/* Parses Code Table and Builds Decode Tree */
public static void parse_code_table_and_build_decode_tree(String codeTable_file) {
try {
BufferedReader br = new BufferedReader(new FileReader(codeTable_file));
String line = "";
while ((line = br.readLine()) != null && !line.equals("")) {
String vars[] = line.split(" ");
/* Adds each individual value and huffman code to decode tree */
DecoderTree.add_code_to_decode_tree(vars[0], vars[1]);
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found Exception in parseCodeTableBuildDecodeTree : " + e.getMessage());
e.printStackTrace();
} catch (IOException ex) {
System.out.println("IO Exception in parseCodeTableBuildDecodeTree: " + ex.getMessage());
ex.printStackTrace();
}
}
/* Parses Encoded File */
public static void parse_encoded_file_and_decode(String encoded_file) {
try {
Path path = Paths.get(encoded_file);
byte[] bytearray = Files.readAllBytes(path);
StringBuilder code = new StringBuilder();
for (byte nextbyte : bytearray) {
code.append(String.format("%8s", Integer.toBinaryString(nextbyte & 0xff)).replace(' ', '0')); // Padding of 0s in the front of each binary code
}
DecoderTree.decode_code(code.toString());
} catch (FileNotFoundException e) {
System.out.println("File Not Found Exception in parse_encoded_file_and_decode : " + e.getMessage());
e.printStackTrace();
} catch (IOException ex) {
System.out.println("IO Exception in parse_encoded_file_and_decode : " + ex.getMessage());
ex.printStackTrace();
}
}
}