-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlzwEncoding.java
More file actions
82 lines (72 loc) · 1.92 KB
/
lzwEncoding.java
File metadata and controls
82 lines (72 loc) · 1.92 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
import java.util.*;
import java.io.*;
public class lzwEncoding
{
public static HashMap<String,Character> init(HashMap<String,Character> table)
{
// fill the table with the standard ascii 1-128
for(int a = 0; a < 128; a++)
{
char current = (char)(a);
// (pattern (string) + corresponding ascii (char))
table.put(current+"",current);
}
return table;
}
public static void encode (String input, String output)
{
// the table containing the pattern and corresponding ascii - "a" -> 'a'
HashMap<String, Character> table = new HashMap<String, Character>();
// holds the encoded message
StringBuilder encoding = new StringBuilder("");
// fill the table with the standard ascii 1-128
init(table);
try
{
BufferedReader br = new BufferedReader(new FileReader(input));
int current = br.read();
// last char of previous pattern
String prev = (char)current + "";
// the next available ascii/table slot
int num = 128;
while(current != -1)
{
current = br.read();
// current portion of the text being scanned for new patterns
String temp = prev + (char)current;
// pattern not found
if(!table.containsKey(temp))
{
// encode previous
encoding.append(table.get(prev));
// max 256 bc the extended ascii table ends at 255, so we can't represent anything past 255
// add to the table
if(num < 256)
table.put(temp, (char)num);
// increase the next available ascii/table slot
num++;
// reset bc we've already encoded the previous
prev = "";
}
// add to or set 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 encoding
bw.write(encoding.toString());
bw.close();
}
catch(IOException e)
{
System.out.println("IOException");
}
}
}