Repository navigation
Expand file tree
/
Copy pathSimpleHuffProcessor.java
More file actions
435 lines (413 loc) · 15.5 KB
/
Copy pathSimpleHuffProcessor.java
File metadata and controls
435 lines (413 loc) · 15.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* Student information for assignment:
*
* On OUR honor, Derek and Shanti, this programming assignment is <MY|OUR> own work
* and WE have not provided this code to any other student.
*
* Number of slip days used: 1
*
* Student 1
* UTEID: dyc377
* email address: derexh2so4@utexas.edu
* Grader name: Lilly Tian
*
* Student 2
* UTEID: sc66349
* email address: shantikiranc@utexas.edu
*
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SimpleHuffProcessor implements IHuffProcessor {
private IHuffViewer myViewer;
private Map<Integer, Integer> mapofwords;
private PriorityQueue314<TreeNode> queue;
private Map<Integer, String> values;
private TreeNode root;
private int header;
private int sizeOfNewFile;
/**
* Preprocess data so that compression is possible ---
* count characters/create tree/store state so that
* a subsequent call to compress will work. The InputStream
* is <em>not</em> a BitInputStream, so wrap it int one as needed.
*
* @param in is the stream which could be subsequently compressed
* @param headerFormat a constant from IHuffProcessor that determines what kind
* of
* header to use, standard count format, standard tree
* format, or
* possibly some format added in the future.
* @return number of bits saved by compression or some other measure
* Note, to determine the number of
* bits saved, the number of bits written includes
* ALL bits that will be written including the
* magic number, the header format number, the header to
* reproduce the tree, AND the actual data.
* @throws IOException if an error occurs while reading from the input file.
*/
public int preprocessCompress(InputStream in, int headerFormat) throws IOException {
BitInputStream bits = new BitInputStream(in);
showString("Opening file...");
int size = 0;
while (bits.readBits(1) != -1) {
size++;
}
in.reset();
bits = new BitInputStream(in);
// Process for getting word frequencies, enqueueing them, and converting to Huffman Tree
mapofwords = getMapOfFreq(bits);
showString("Map of frequencies created! " + mapofwords);
queue = getQueue(mapofwords);
root = createTree(queue);
header = headerFormat;
Map<Integer, String> curr = new TreeMap<>();
getHuffCodesHelper(curr, "", root);
values = curr;
in.reset();
bits = new BitInputStream(in);
if (header == IHuffConstants.STORE_COUNTS) {
sizeOfNewFile = getSizeOfFileCounts(bits);
} else {
sizeOfNewFile = getSizeOfFileTree(bits);
}
int saved = size - sizeOfNewFile;
showString("Saved: " + saved + " bits");
return saved;
}
/**
* Helper method to get the size of file in SCF format
*
* @return The size of file in SCF format
* @throws IOException
*/
private int getSizeOfFileCounts(BitInputStream bits) throws IOException {
int total = 0;
total += BITS_PER_INT * 2;
total += BITS_PER_INT * ALPH_SIZE;
int inbits = bits.readBits(IHuffConstants.BITS_PER_WORD);
while (inbits != -1) {
String value = values.get(inbits);
total += value.length();
inbits = bits.readBits(IHuffConstants.BITS_PER_WORD);
}
String value = values.get(PSEUDO_EOF);
total += value.length();
return total;
}
/**
* Helper method to get the size of file in STF format
*
* @return The size of file in STF format
* @throws IOException
*/
private int getSizeOfFileTree(BitInputStream bits) throws IOException {
int total = 0;
total += BITS_PER_INT * 3;
TreeNode n = root;
int allNodes = getSize(n);
int size = mapofwords.size() * 9 + allNodes;
total += size;
int inbits = bits.readBits(IHuffConstants.BITS_PER_WORD);
while (inbits != -1) {
String value = values.get(inbits);
total += value.length();
inbits = bits.readBits(IHuffConstants.BITS_PER_WORD);
}
String value = values.get(PSEUDO_EOF);
total += value.length();
return total;
}
/**
* Creates the Huffman Tree given a PriorityQueue314 with frequencies
*
* @return The last value in the queue, the root of the Huffman Tree
*/
private TreeNode createTree(PriorityQueue314<TreeNode> queue) {
while (queue.size() > 1) {
TreeNode left = queue.deque();
TreeNode right = queue.deque();
TreeNode temp = new TreeNode(left, -1, right);
queue.enque(temp);
}
showString("Huffman Tree Created!");
return queue.deque();
}
/**
* Creates a map of the binary values representing the characters in the Huffman Tree by
* traversing down the tree
*/
private void getHuffCodesHelper(Map<Integer, String> map, String current, TreeNode node) {
if (node.getLeft() != null && node.getRight() != null) {
String leftCurr = current + "0";
String rightCurr = current + "1";
getHuffCodesHelper(map, leftCurr, node.getLeft());
getHuffCodesHelper(map, rightCurr, node.getRight());
} else if (node.getLeft() != null) {
String curr = current + "0";
getHuffCodesHelper(map, current, node.getLeft());
} else if (node.getRight() != null) {
String curr = current + "1";
getHuffCodesHelper(map, current, node.getRight());
} else {
map.put(node.getValue(), current);
}
}
/**
* Generates the map of frequencies for the characters based on the input stream
*
* @throws IOException
*/
private Map<Integer, Integer> getMapOfFreq(BitInputStream bits) throws IOException {
Map<Integer, Integer> mapofwords = new TreeMap<>();
int inbits = bits.readBits(IHuffConstants.BITS_PER_WORD);
while (inbits != -1) {
if (mapofwords.containsKey(inbits)) {
int x = mapofwords.get(inbits);
mapofwords.put(inbits, x + 1);
} else {
mapofwords.put(inbits, 1);
}
inbits = bits.readBits(IHuffConstants.BITS_PER_WORD);
}
mapofwords.put(IHuffConstants.PSEUDO_EOF, 1);
return mapofwords;
}
/**
* Generates the PriorityQueue314 based on the frequency map provided
*/
private PriorityQueue314<TreeNode> getQueue(Map<Integer, Integer> mapofwords) {
PriorityQueue314<TreeNode> queue = new PriorityQueue314<>();
for (Map.Entry<Integer, Integer> entry : mapofwords.entrySet()) {
TreeNode node = new TreeNode(entry.getKey(), entry.getValue());
queue.enque(node);
}
return queue;
}
/**
* Compresses input to output, where the same InputStream has
* previously been pre-processed via <code>preprocessCompress</code>
* storing state used by this call.
* <br>
* pre: <code>preprocessCompress</code> must be called before this method
*
* @param in is the stream being compressed (NOT a BitInputStream)
* @param out is bound to a file/stream to which bits are written
* for the compressed file (not a BitOutputStream)
* @param force if this is true create the output file even if it is larger than
* the input file.
* If this is false do not create the output file if it is larger
* than the input file.
* @return the number of bits written.
* @throws IOException if an error occurs while reading from the input file or
* writing to the output file.
*/
public int compress(InputStream in, OutputStream out, boolean force) throws IOException {
BitInputStream bits = new BitInputStream(in);
BitOutputStream outs = new BitOutputStream(out);
outs.writeBits(BITS_PER_INT, MAGIC_NUMBER);
if (header == IHuffConstants.STORE_COUNTS) {
outs.writeBits(BITS_PER_INT, STORE_COUNTS);
standardCountsHeader(outs);
} else {
outs.writeBits(BITS_PER_INT, STORE_TREE);
standardTreeHeader(outs);
}
int inbits = bits.readBits(IHuffConstants.BITS_PER_WORD);
while (inbits != -1) {
String value = values.get(inbits);
for (int x = 0; x < value.length(); x++) {
outs.writeBits(1, value.charAt(x));
}
inbits = bits.readBits(IHuffConstants.BITS_PER_WORD);
}
String value = values.get(PSEUDO_EOF);
for (int x = 0; x < value.length(); x++) {
outs.writeBits(1, value.charAt(x));
}
outs.close();
showString("Size of new file: " + sizeOfNewFile);
return sizeOfNewFile;
}
/**
* Helper method writing the standard counts header
*/
private void standardCountsHeader(BitOutputStream outs) {
for (int k = 0; k < IHuffConstants.ALPH_SIZE; k++) {
if (mapofwords.containsKey(k)) {
outs.writeBits(BITS_PER_INT, mapofwords.get(k));
} else {
outs.writeBits(BITS_PER_INT, 0);
}
}
}
/**
* @return The size of the Huffman Tree
*/
private int getSize(TreeNode n) {
if (n == null) {
return 0;
} else {
return getSize(n.getLeft()) + 1 + getSize(n.getRight());
}
}
/**
* Helper method that writes the standard tree header
* \
*/
private void standardTreeHeader(BitOutputStream outs) {
TreeNode n = root;
int allNodes = getSize(n);
int size = mapofwords.size() * 9 + allNodes;
outs.writeBits(BITS_PER_INT, size);
treeToBinary(n, outs);
}
/**
* Helper method converting the huffman tree to binary
*/
private void treeToBinary(TreeNode n, BitOutputStream outs) {
if (n != null) {
if (n.getValue() != -1) {
outs.writeBits(1, 1);
outs.writeBits(IHuffConstants.BITS_PER_WORD + 1, n.getValue());
} else {
outs.writeBits(1, 0);
treeToBinary(n.getLeft(), outs);
treeToBinary(n.getRight(), outs);
}
}
}
/**
* Uncompress a previously compressed stream in, writing the
* uncompressed bits/data to out.
*
* @param in is the previously compressed data (not a BitInputStream)
* @param out is the uncompressed file/stream
* @return the number of bits written to the uncompressed file/stream
* @throws IOException if an error occurs while reading from the input file or
* writing to the output file.
*/
public int uncompress(InputStream in, OutputStream out) throws IOException {
BitInputStream bis = new BitInputStream(in);
BitOutputStream bos = new BitOutputStream(out);
int magicNumba = bis.readBits(BITS_PER_INT);
if (magicNumba != MAGIC_NUMBER) {
myViewer.showError("Error reading compressed file. \n" +
"File did not start with the huff magic number.");
}
int countType = bis.readBits(BITS_PER_INT);
int bitCount = 0;
TreeNode decompRoot;
// Check the type of encoding (STF) (SCF)
if (countType == STORE_COUNTS) {
showString("Uncompressing in SCF");
decompRoot = createTreeSCF(bis);
bitCount = decoder(bis, bos, decompRoot);
} else if (countType == STORE_TREE) {
showString("Uncompressing in STF");
int numBits = bis.readBits(BITS_PER_INT);
decompRoot = createTreeFromBits(bis);
showString("Huffman Tree Created!");
bitCount = decoder(bis, bos, decompRoot);
}
showString(bitCount + " bits written.");
bos.close();
return bitCount;
}
/**
* Reads in bit by bit and traverses the huffman tree depending on the bit value
* A bit value of 0 corresponds to a left traversal while 1 represents a right
* traversal
*
* @throws IOException
*/
private int decoder(BitInputStream bis, BitOutputStream bos, TreeNode root) throws IOException {
boolean finished = false;
TreeNode current = root;
int bitCount = 0;
int dirCheck = 0;
while (!finished && !(dirCheck == -1)) {
dirCheck = bis.readBits(1);
if (dirCheck == -1) {
throw new IOException("Error reading compressed file. \n" +
"unexpected end of input. No PSEUDO_EOF value.");
} else {
if (dirCheck == 0) {
current = current.getLeft();
} else if (dirCheck == 1) {
current = current.getRight();
}
if (current.getValue() == PSEUDO_EOF) {
finished = true;
} else if (current.isLeaf()) {
bos.write(current.getValue());
current = root;
bitCount++;
}
}
}
return bitCount;
}
/**
* Creates a Huffman tree from a SCF header format where the frequencies and
* values of characters in ASCII are given
*
* @return A TreeNode to be set as the child of a previous TreeNode
* @throws IOException
*/
private TreeNode createTreeSCF(BitInputStream bis) throws IOException {
Map<Integer, Integer> tempFreq = new TreeMap<>();
for (int k = 0; k < IHuffConstants.ALPH_SIZE; k++) {
int frequencyInOriginalFile = bis.readBits(BITS_PER_INT);
if (frequencyInOriginalFile > 0) {
tempFreq.put(k, frequencyInOriginalFile);
}
}
tempFreq.put(IHuffConstants.PSEUDO_EOF, 1);
PriorityQueue314<TreeNode> decompQueue = getQueue(tempFreq);
return createTree(decompQueue);
}
/**
* Creates a Huffman tree from a STF header format where 0 corresponds to an
* internal node while 1 corresponds to a leaf node
*
* @return A TreeNode to be set as the child of a previous TreeNode
* @throws IOException
*/
private TreeNode createTreeFromBits(BitInputStream bis) throws IOException {
int tempBit = bis.readBits(1);
if (tempBit == 0) {
// Internal node
TreeNode tempNode = new TreeNode(-1, -1);
tempNode.setLeft(createTreeFromBits(bis));
tempNode.setRight(createTreeFromBits(bis));
return tempNode;
} else if (tempBit == 1) {
// Leaf node
int nodeValue = bis.readBits(BITS_PER_WORD + 1);
TreeNode tempNode = new TreeNode(nodeValue, -1);
return tempNode;
} else {
myViewer.showError("catastrophic failure");
}
return null;
}
/**
* @param viewer is the view for communicating.
*/
public void setViewer(IHuffViewer viewer) {
myViewer = viewer;
}
/**
* Shows a string to the GUI
* @param s
*/
private void showString(String s) {
if (myViewer != null) {
myViewer.update(s);
}
}
}