-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.java
101 lines (70 loc) · 2.8 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
package com.huffman;
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;
public class decoder {
public static void main(String args[]) throws IOException{
//Scanner sc=new Scanner(System.in);
String encoded_file=args[0];
String codeTable_file=args[1];
long startTime = System.currentTimeMillis();
//System.out.print("here inside1");
parseCodeTableBuildDecodeTree(codeTable_file);
parseEncodedFileDecode(encoded_file);
//sc.close();
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Time using binary heap (millisecond): " +totalTime);
}
/* Parses Encoded File */
public static void parseEncodedFileDecode(String encoded_file) {
try {
Path path = Paths.get(encoded_file);
byte[] bytearray = Files.readAllBytes(path);
StringBuilder s = new StringBuilder();
for (byte nextbyte : bytearray) {
s.append(String.format("%8s", Integer.toBinaryString(nextbyte & 0xff)).replace(' ', '0'));
}
DecodeTree.decodeMessage(s.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();
}
}
/*public static void parseEncodedFileDecode(String encoded_file) throws IOException{
try (
InputStream inputStream = new BufferedInputStream(new FileInputStream(encoded_file));
) {
StringBuilder sb=new StringBuilder();
Integer c=0;
while (( c = inputStream.read()) != -1) {
//System.out.println("printing c"+c);
sb.append(String.format("%8s", Integer.toBinaryString(c)).replace(" ", "0"));
}
//System.out.println(sb);
DecodeTree.decodeMessage(sb.toString());
}
catch (IOException ex) {
ex.printStackTrace();
}
}*/
public static void parseCodeTableBuildDecodeTree(String codeTable_file) throws IOException{
//System.out.println("inside building decode tree");
BufferedReader br = new BufferedReader(new FileReader(codeTable_file));
String line = "";
///////////////////////////////////////////////////////////
while((line = br.readLine()) != null) {
String vars[] = line.split(" ");
//System.out.println("1st value is "+vars[0]+" 2nd value is "+ vars[1]);
DecodeTree.addToDecodeTree(vars[0],vars[1]);
}
br.close();
}
}