forked from ktkaufman/LZW-Compression
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLZWDecoder.java
More file actions
73 lines (65 loc) · 2.46 KB
/
Copy pathLZWDecoder.java
File metadata and controls
73 lines (65 loc) · 2.46 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
import java.util.*;
import java.io.*;
public class LZWDecoder {
//codeMap is a hashmap that stores the LZW table
private HashMap<Integer, String> codeMap;
//lastIndex stores the index of last number that we added to the table (# things in table - 1)
private int lastIndex;
//the initial size of the table
private final int INIT_TABLE_SIZE = 128;
public LZWDecoder() {
//adds first 128 ascii characters to table
codeMap = new HashMap<Integer, String>(INIT_TABLE_SIZE);
for (int i = 0; i<INIT_TABLE_SIZE; i++) {
codeMap.put(i, Character.toString((char)(i))); //value of ith ascii as a key is i
}
lastIndex = INIT_TABLE_SIZE - 1;
}
public void decode(String inputFile, String outputFile) throws IOException{
//input string we're about to encode
String buffer = "";
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter outputWriter = new BufferedWriter(new FileWriter(new File(outputFile)));
//read the first char beforehand
int inputCharNum = reader.read();
//stores the previous code
String prev = "";
Integer bufferNum = new Integer(0);
while(inputCharNum != -1) {
//add the new character to our buffer
buffer += Character.toString((char)inputCharNum);
//buffer will now become all the chars up until the next space
//the one exception is if we have SPACE SPACE SPACE, in which case buffer=space
inputCharNum = reader.read();
while(inputCharNum != ' ' && inputCharNum != -1) {
buffer += Character.toString((char)inputCharNum);
inputCharNum = reader.read();
}
//if a written out number (like 2539) we parse the int, otherwise it's a single char/int
if (buffer.length() > 1) {
bufferNum = new Integer(Integer.parseInt(buffer));
} else {
bufferNum = new Integer((int)(buffer.charAt(0)));
}
//if the code has not been added yet, that means the last char is the first char of prev
if (!codeMap.containsKey(bufferNum)) {
outputWriter.write(prev+prev.substring(0,1));
codeMap.put(bufferNum, prev+prev.substring(0,1));
lastIndex++;
} else { //write the corresponding string and add to codeMap
outputWriter.write(codeMap.get(bufferNum));
if (!prev.equals("")) {
codeMap.put(lastIndex+1, prev+codeMap.get(bufferNum).substring(0,1));
lastIndex++;
}
}
//update variables
prev = codeMap.get(bufferNum);
buffer = "";
inputCharNum = reader.read();
}
//closing the reader and writer
reader.close();
outputWriter.close();
}
}