-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressor.java
More file actions
211 lines (191 loc) · 7.03 KB
/
Compressor.java
File metadata and controls
211 lines (191 loc) · 7.03 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
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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.PriorityQueue;
public class Compressor {
HashMap<Character, Integer> table;
PriorityQueue<BinaryTree<CharFreq>> queue;
BinaryTree<CharFreq> huffmanTree;
HashMap<Character, String> key;
public Compressor() {
this.table = new HashMap<Character, Integer>();
this.key = new HashMap<Character, String>();
}
public BinaryTree<CharFreq> getHuffmanTree() {
return huffmanTree;
}
public void compress(String pathName) throws FileNotFoundException {
if (pathName.indexOf(".txt") == -1) {
throw new FileNotFoundException("Invalid file type!");
}
BufferedReader inputFile = new BufferedReader(new FileReader(pathName));
try {
checkFrequencies(pathName);
// Checks if the txt file is empty.
if (inputFile.readLine() == null) {
inputFile.close();
return;
}
} catch (IOException e) {
e.printStackTrace();
}
queue();
huffman();
createKey();
try {
writeCompressed(pathName);
inputFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Check frequencies of chars in the txt file
*/
public void checkFrequencies(String pathName) throws IOException {
BufferedReader inputFile = new BufferedReader(new FileReader(pathName));
try {
int charInt = inputFile.read();
while (charInt != -1) {
char c = (char) charInt;
Integer value = table.get(c);
if (value != null) {
table.put(c, value + 1);
} else {
table.put(c, 1);
}
charInt = inputFile.read();
}
} finally {
inputFile.close();
}
}
public void writeCompressed(String pathName) throws IOException {
BufferedReader inputFile = new BufferedReader(new FileReader(pathName));
String compressedPath = "";
if (pathName.indexOf("_decompressed") == -1) {
compressedPath += pathName.substring(0, pathName.indexOf(".txt")) + "_compressed" + ".txt";
} else {
compressedPath += pathName.substring(0, pathName.indexOf("_decompressed")) + "_compressed" + ".txt";
}
BufferedBitWriter bitWriter = new BufferedBitWriter(compressedPath);
try {
String leafBinary = Integer.toBinaryString(huffmanTree.countLeaves());
System.out.println("Tree size in binary: " + leafBinary);
if (leafBinary.length() < 8) {
for (int i = 0; i < 8 - leafBinary.length(); i++) {
bitWriter.writeBit(0);
}
}
for (int i = 0; i < leafBinary.length(); i++) {
bitWriter.writeBit(Integer.valueOf(leafBinary.substring(i, i + 1)));
}
writeTree(huffmanTree, bitWriter);
int charInt = inputFile.read();
while (charInt != -1) {
char c = (char) charInt;
String binaryKey = key.get(c);
for (int i = 0; i < key.get(c).length(); i++) {
bitWriter.writeBit(Integer.valueOf(binaryKey.substring(i, i + 1)));
}
charInt = inputFile.read();
}
} finally {
inputFile.close();
bitWriter.close();
}
}
public void writeTree(BinaryTree<CharFreq> node, BufferedBitWriter bitWriter) throws IOException {
if (node == null) {
return;
}
if (node.isLeaf()) {
// System.out.println("writing 1");
bitWriter.writeBit(1);
// System.out.println("writing char: " + node.getData().getChar());
String charByte = Integer.toBinaryString(node.getData().getChar());
String zeroes = "";
if (charByte.length() != 8) {
for (int i = 0; i < 8 - charByte.length(); i++) {
zeroes += "0";
}
charByte = zeroes + charByte;
}
for (int i = 0; i < charByte.length(); i++) {
bitWriter.writeBit(Integer.valueOf(charByte.substring(i, i + 1)));
}
} else {
bitWriter.writeBit(0);
writeTree(node.getLeft(), bitWriter);
writeTree(node.getRight(), bitWriter);
}
}
/*
* Takes Hashmap and puts the BinaryTree objects into a min first priority
* queue.
*/
public void queue() {
TreeComparator comparator = new TreeComparator();
queue = new PriorityQueue<BinaryTree<CharFreq>>(11, comparator);
Iterator it = table.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
BinaryTree<CharFreq> binaryTree = new BinaryTree<CharFreq>(
new CharFreq((char) pair.getKey(), (int) pair.getValue()));
queue.add(binaryTree);
it.remove();
}
queue.comparator();
}
/*
* Use Huffman Encoding to create a binary tree based on the queue.
*/
public void huffman() {
while (queue.size() > 1) {
// set two trees
BinaryTree<CharFreq> tree1 = queue.peek();
queue.remove();
BinaryTree<CharFreq> tree2 = queue.peek();
queue.remove();
// sum their frequencies
int sum = tree1.getData().getFreq() + tree2.getData().getFreq();
// create a new tree with children
BinaryTree<CharFreq> mergedTree = new BinaryTree<CharFreq>(new CharFreq('?', sum));
mergedTree.setLeft(tree1);
mergedTree.setRight(tree2);
// add the tree back into the priority queue
queue.add(mergedTree);
// reorder the queue
queue.comparator();
}
// finally, set the huffman tree instance variable.
huffmanTree = queue.peek();
}
/*
* uses pre-order traversal and creates the key for chars
*/
public void createKey() {
inorderTraversalKey(huffmanTree, "");
}
public void inorderTraversalKey(BinaryTree<CharFreq> node, String code) {
if (node == null) {
return;
}
if (huffmanTree.isLeaf() && huffmanTree.count() == 1) {
key.put(huffmanTree.getData().getChar(), "0");
return;
}
String left = code + "0";
String right = code + "1";
inorderTraversalKey(node.getLeft(), left);
if (node.isLeaf()) {
key.put(node.getData().getChar(), code);
System.out.println("Char '" + node.getData().getChar() + "': " + code);
}
inorderTraversalKey(node.getRight(), right);
}
}