forked from echoi3-hw/LZW-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlzwDecode.java
More file actions
105 lines (93 loc) · 3.12 KB
/
Copy pathlzwDecode.java
File metadata and controls
105 lines (93 loc) · 3.12 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
import java.util.*;
import java.io.*;
public class lzwDecode
{
public static HashMap<Character,String> init(HashMap<Character,String> table)
{
// fill the table with the standard ascii 1-128
for(int a = 0; a < 128; a++)
{
char current = (char)(a);
// (corresponding ascii (char) + pattern (string))
// reversed from prev bc i'm pretty sure containsValue is O(N) while containsKey is O(1)
// also bc get is way easier to use than whatever we would need to use to find the matching key
table.put(current,current+"");
}
return table;
}
public static void decode (String input, String output)
{
// the table containing the pattern and corresponding ascii - "a" -> 'a'
HashMap<Character, String> table = new HashMap<Character, String>();
// holds the decoded message
StringBuilder decoding = new StringBuilder("");
// fill the table with the standard ascii 1-128
init(table);
// the first available character
int num = 128;
try
{
BufferedReader br = new BufferedReader(new FileReader(input));
int current = br.read();
// java doesn't allow for empty chars, so we just treat 'a' as '' bc it gets overwritten first
char prev='a';
String decodeBlock = "";
// the first char of the previous block, used for the special case when LZW doesn't work + adding to the table
String prevChar = "";
// check if the first char of the encoding is in the table
// should always be there, just being safe (we have big problems if it isn't in the table)
if(table.containsKey((char)current))
{
prev = ((char)current);
// add to the decoding
decoding.append(table.get((char)current));
}
while(current != -1)
{
current = br.read();
// until EOF
if(current == -1)
break;
// current character isn't found in the table, weird lzw case
if(!table.containsKey((char)current))
// add the first char of the prev to the end of the decoded prev to find the missing value
decodeBlock = table.get(prev) + prevChar;
// current character is in the table!
if(table.containsKey((char)current))
//simply decode the current char
decodeBlock = table.get((char)current);
// add whatever was decoded to decoding
decoding.append(decodeBlock);
// save first char of the decoded block
prevChar = decodeBlock.charAt(0)+"";
// max 256 bc the extended ascii table ends at 255, so we can't represent anything past 255
// update: changed it to 55296
// max should be 65536 bc in utf - 8 1 char is maxxed at 65536, but bc of some weird utf rule, we're capped at 55296
// add to the table
if(num < 55296)
table.put((char)num, table.get(prev)+prevChar);
// increase the next available ascii/table slot
num++;
// save the previous
prev = (char)current;
}
br.close();
}
catch(IOException e)
{
System.out.println("IOException");
}
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(output));
// write out the decoding
bw.write(decoding.toString());
//System.out.println(decoding.toString());
bw.close();
}
catch(IOException e)
{
System.out.println("IOException");
}
}
}