forked from yvettejade/LZWEncoding
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlzwDecoder.java
More file actions
57 lines (49 loc) · 2.01 KB
/
lzwDecoder.java
File metadata and controls
57 lines (49 loc) · 2.01 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
import java.io.*;
import java.util.*;
public class lzwDecoder {
private HashMap<Integer, String> encodingTable;
final static int INITIAL_TABLE_SIZE = 128;
public lzwDecoder() {}
public void decode(String inputFileName, String outputFileName) throws IOException {
BufferedReader br = new BufferedReader (new FileReader(inputFileName));
encodingTable = new HashMap<Integer, String>();
PrintWriter pw = new PrintWriter(new File(outputFileName));
//Fills table with the initial characters
for(int a = 0; a < INITIAL_TABLE_SIZE; a++) {
encodingTable.put(a, new String((char)a + ""));
}
//First iteration, priming variables to loop
int nextValue = INITIAL_TABLE_SIZE;
int currentCode = br.read(); //the char being decoded, represents an encoded string
String previousStr = ""; //represents the most recently printed set of chars
String currentStr = ""; //represents the set of chars to be printed & added to the hashmap
//Puts first combination (the first two letters) into the hashmap
currentStr = (char) currentCode +"";
currentCode = br.read();
currentStr += (char) currentCode +"";
encodingTable.put(nextValue, currentStr);
pw.print(currentStr);
//Resets strings to accept the next encoding
previousStr = currentStr.substring(1);
currentStr = "";
nextValue++;
while(br.ready()) {
currentCode = br.read();
if (encodingTable.containsKey(currentCode)) { //if current code is already in the hashmap, add previous characters + 1st letter of current to hashmap
currentStr = encodingTable.get(currentCode);
encodingTable.put(nextValue, previousStr + currentStr.substring(0,1));
}
else { //otherwise, add the previous string + its first letter
currentStr = previousStr + previousStr.substring(0,1);
encodingTable.put(nextValue, currentStr);
}
pw.print(currentStr);//print the current string
//Resets strings to accept the next encoding
previousStr = encodingTable.get(currentCode);
currentStr = "";
nextValue++;
}
pw.close();
br.close();
}
}