-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecodeTextFile.java
104 lines (69 loc) · 2.19 KB
/
DecodeTextFile.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
package app;
import java.awt.List;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import javax.swing.text.html.HTMLDocument.Iterator;
import ac.Node;
import ac.TreeBuilder;
import ac.compressionTest;
import io.InputStreamBitSource;
import io.InsufficientBitsLeftException;
import io.OutputStreamBitSink;
public class DecodeTextFile {
public static void main(String [] args) throws InsufficientBitsLeftException, IOException{
String input_file_name = "compressed (1).dat";
String output_file_name = "uncompressed.txt";
FileInputStream fis = new FileInputStream(input_file_name);
InputStreamBitSource bit_source = new InputStreamBitSource(fis);
int[] sym_lengths = new int[256];
Integer[] symbols = new Integer[256];
ArrayList<Node> sorted_Nodes = new ArrayList<Node>();
for (int i=0; i<256; i++) {
Node new_Node = new Node(i, bit_source.next(8));
sorted_Nodes.add(new_Node);
}
Collections.sort(sorted_Nodes, Node.sorted);
int num_syms = bit_source.next(32);
System.out.println(num_syms);
TreeBuilder tree = new TreeBuilder(sorted_Nodes, num_syms);
tree.buildTree();
Node root = tree.getRoot();
Node current = root;
FileOutputStream fos = new FileOutputStream(output_file_name);
int i = 0;
while(i < num_syms){
int bit = bit_source.next(1);
if(bit == 0){
current = current.left();
if(current.getSymbol() !=-1){
fos.write((char) current.getSymbol());
i++;
current = root;
}
}else{
current = current.right();
if(current.getSymbol() !=-1){
fos.write((char) current.getSymbol());
i++;
current = root;
}
}
}
System.out.println("Decoding Done");
fis.close();
}
public static void printList(ArrayList sorted_Nodes){
java.util.Iterator<Node> iter = sorted_Nodes.iterator();
while(iter.hasNext()){
Node node = iter.next();
System.out.println("Symbol: "+ node.getSymbol() + " "+ "Length: "+ node.getLength());
}
}
}