forked from y-a-l-l-s/LZW
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncoder.java
More file actions
82 lines (77 loc) · 2.34 KB
/
Copy pathEncoder.java
File metadata and controls
82 lines (77 loc) · 2.34 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.*;
import java.util.HashMap;
public class Encoder {
private final int ORIG_LENGTH = 256;
private HashMap <String,Integer> dict;
private int counter, maxCodes;
public Encoder(int maxCodes) {
//dictionary
dict = new HashMap<String, Integer>();
counter = 0;
this.maxCodes = maxCodes;
setup(ORIG_LENGTH);
}
public void encode (String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fileName.substring(0, fileName.length()-4)+".lzw")));
char current;
String temp = "";//temp holds the string of characters to be compressed
while (br.ready()) {
current = (char) br.read();
temp += current;
while (isKey(temp) && br.ready()) {
current = (char) br.read();
temp += current;
}
//break case where we arent at the end of the file, and thus temp is no longer a key
if (br.ready()) {
try {
write(addKey(temp), bw);
} catch (Exception ex) {
System.out.println(temp);
System.out.println(dict.get(temp));
System.out.println(addKey(temp));
ex.printStackTrace();
}
temp = "" + current;
} else {
//break case where we are at the end of a file, but temp is a key
if (isKey(temp)) {
write(dict.get(temp), bw);
//break case where we are at the end of a file, but temp is not a key
} else {
write(addKey(temp), bw);
write(dict.get(""+current), bw);
}
}
}
br.close();
bw.flush();
bw.close();
}
//writes the Integer onto the output file as one character
private void write (Integer num, BufferedWriter writer) throws IOException {
writer.write((char) (num.intValue()));
System.out.println(num.intValue());
}
//sets up the first 256 ascii chars in the dictionary
private void setup (int chars) {
for (int i = 0; i <= chars; i++) {
dict.put("" + (char) i, i);
}
counter = chars;
}
//checks if a string is a key in our dictionary
private boolean isKey (String s) {
return dict.containsKey(s);
}
//adds new string to HashMap with the value of counter. Increments counter. Returns the associated value of str - 1 letter.
private Integer addKey (String str) {
if (counter < maxCodes) {
dict.put(str, counter);
counter++;
}
return dict.get(str.substring(0,str.length()-1));
}
}