forked from ktkaufman/LZW-Compression
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLZW.java
More file actions
64 lines (58 loc) · 2.2 KB
/
Copy pathLZW.java
File metadata and controls
64 lines (58 loc) · 2.2 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
import java.io.*;
import java.util.*;
public class LZW {
private HashMap<String, Integer> table;
private FileReader fr;
private BufferedReader br;
private String newFileName;
private final int INIT_TABLE_SIZE = 127;
private final int EXTENDED_ASCII_SIZE = 256;
public LZW(String fileName) throws FileNotFoundException {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
table=new HashMap<String, Integer>();
newFileName = "out.txt"; //OUTPUT FILE
}
public void fillTable() //puts in all the values for one letter chars into the hash map
{
table=new HashMap<String, Integer>();
for(int n=0;n<INIT_TABLE_SIZE;n++){
table.put(""+(char)n, n);
}
}
public void createFile () throws IOException
{
fillTable();
encode(br);
}
public void encode(BufferedReader text) throws IOException //goes through text, adds new patterns to hmap, and updates output with more integers
{
String temp = "";//the temp variable keeps track of the series of letters you are reading so you can check if the variable is already present in you table or not
int counter=INIT_TABLE_SIZE;
FileWriter out;
try {
out = new FileWriter(newFileName);
BufferedWriter put = new BufferedWriter(out);
while (text.ready())
{
temp+=(char)(text.read());
if(!table.containsKey(temp)){ //if the table doesn't have the series of letters already it adds the new pattern to the table
String temp2=temp.substring(0,temp.length()-1);//temp 2 is a temporary placeholder that holds all the characters of temp besides the last one so that we can output the pattern which should be in the table already
int tableIndex =table.get(temp2);
if (tableIndex >= EXTENDED_ASCII_SIZE) {
put.write(""+tableIndex+" "); //numbers not corresponding to ASCII table chars stay the same
} else {
put.write(""+(char)(tableIndex)+" "); //numbers corresponding to ASCII table chars get converted to chars (less space)
}
counter++;
table.put(temp, counter); //adds code to table
temp = temp.substring(temp.length()-1); //resets temp string to last char
}
}
put.write(""+table.get(temp)); //writes the last code
put.close();
} catch (IOException e) {
System.out.println("BIG OOPS");
}
}
}