-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanWriter.java
More file actions
155 lines (120 loc) · 3.47 KB
/
Copy pathHuffmanWriter.java
File metadata and controls
155 lines (120 loc) · 3.47 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
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
/* Author: Edric Orense
* File: HuffmanWriter.java
* Purpose: Handles the writing to file/binary file, and encoding of the huffman tree.
*/
import java.io.File;
public class HuffmanWriter {
private BinaryFile output, input;
private TextFile tf;
private Node node;
//Compresses the file, writes out to specified file in binary
public void compressFile(String inputFile, String outputFile, String[] codeArray, Node node){
File file = new File(outputFile);
file.delete();
output = new BinaryFile(outputFile, 'w');
tf = new TextFile(inputFile, 'r');
output.writeChar('H');
output.writeChar('F');
serializeTree(node);
Character c;
while(!tf.EndOfFile()){
c = tf.readChar();
String s = codeArray[c.hashCode()];
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '1'){
output.writeBit(true);
}else{
output.writeBit(false);
}
}
}
output.close();
}
//Writes the huffman tree out in binary to the specified file
public void serializeTree(Node tree){
if (tree == null){
return;
}
if ((tree.getLeftChild() == null) && (tree.getRightChild() == null)){
output.writeBit(true);
output.writeChar(tree.getValue());
return;
}
output.writeBit(false);
serializeTree(tree.getLeftChild());
serializeTree(tree.getRightChild());
}
//Reads in the inputfile(Huffman encoded file) and writes out the decoded file to outputFile
public void decompressFile(String inputFile, String outputFile){
File file = new File(outputFile);
file.delete();
tf = new TextFile(outputFile, 'w');
input = new BinaryFile(inputFile, 'r');
int i = 0;
String s = "";
while(i < 2){
Character c = input.readChar();
s = s.concat(c.toString());
i++;
}
if(s.equals("HF")){
System.out.println("Decompressing...");
if(input.readBit() == false){
node = new Node('.', 0);
node.setLeftChild(rebuildTree());
node.setRightChild(rebuildTree());
}else{
node = new Node(input.readChar(), 0);
}
while(!input.EndOfFile()){
if(node.getLeftChild() == null && node.getRightChild() == null){
tf.writeChar(node.getValue());
input.readBit();
}else{
traverseTree(node);
}
}
tf.close();
System.out.println("Done!");
}else{
System.out.println("Cannot decompress file");
}
}
public void traverseTree(Node node){
if(node.getLeftChild() == null && node.getRightChild() == null){
tf.writeChar(node.getValue());
return;
}
if(input.readBit() == false){
traverseTree(node.getLeftChild());
}else{
traverseTree(node.getRightChild());
}
}
public Node rebuildTree(){
if(input.readBit() == true){
Node n = new Node(input.readChar(), 0);
return n;
}
Node node = new Node('.', 0);
node.setLeftChild(rebuildTree());
node.setRightChild(rebuildTree());
return node;
}
public void printVerbose(){
System.out.println("=====================");
System.out.println("Huffman Tree");
System.out.println("=====================");
print(node, 1);
}
private void print(Node tree, int indent) {
if (tree != null) {
for(int i=0; i<indent; i++) {
System.out.print("\t");
}
System.out.println(tree.getValue() + " : " + tree.getFrequency());
print(tree.getLeftChild(), indent + 1);
print(tree.getRightChild(), indent + 1);
}
}
}