-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLZW.java
More file actions
175 lines (153 loc) · 4.26 KB
/
LZW.java
File metadata and controls
175 lines (153 loc) · 4.26 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
public class LZW
{
private ArrayList<Integer> compressedAscii;
private HashMap<String, Integer> dictionary;
private HashMap<Integer, String> Invertedictionary;
private String fileInAString;
private ArrayList<String> inputFileInAnArrayList;
private ArrayList<String> preDictionary;
private ArrayList<String> update;
private BufferedWriter decodedFile;
private ArrayList<Integer> decodedFileUnicodeValues;
private ObjectInputStream is;
private BufferedReader input;
private ObjectOutputStream os;
public LZW() throws Exception
{
decodedFileUnicodeValues = new ArrayList<Integer>();
dictionary = new HashMap<String, Integer>();
Invertedictionary = new HashMap<Integer, String>();
String fileName = "encoded.bin";
FileOutputStream fileOs = new FileOutputStream(fileName);
os = new ObjectOutputStream(fileOs);
compressedAscii = new ArrayList<Integer>();
inputFileInAnArrayList = new ArrayList<String>();
preDictionary = new ArrayList<String>();
update = new ArrayList<String>();
FileInputStream fileIS = new FileInputStream(fileName);
is = new ObjectInputStream(fileIS);
input = new BufferedReader(new FileReader("InputFile.txt"));
decodedFile = new BufferedWriter(new FileWriter("decoded.txt"));
fileInAString = "";
}
public void readFile()throws Exception
{
String line = input.readLine();
while(line != null)
{
fileInAString += line;
line = input.readLine();
}
for(int i = 0; i < fileInAString.length(); i++)
{
inputFileInAnArrayList.add(fileInAString.substring(i, i+1));
}
}
public void convertFileStringToArrayDictionary()throws Exception
{
readFile();
String temp = inputFileInAnArrayList.get(0);
String temp2 = inputFileInAnArrayList.get(1);
for (int i = 0; i < inputFileInAnArrayList.size(); i++)
{
if(preDictionary.contains(temp+temp2) == false)
{
preDictionary.add(temp+temp2);
update.add(temp);
if(i+2 < inputFileInAnArrayList.size())
{
temp = inputFileInAnArrayList.get(i+1);
temp2 = inputFileInAnArrayList.get(i+2);
}
}
else if(preDictionary.contains(temp+temp2) == true)
{
temp = temp + temp2;
if(i+2<inputFileInAnArrayList.size())
{
temp2 = inputFileInAnArrayList.get(i+2);
}
}
}
}
public void dictionaryPopulators()
{
int counter = 257;
for(int i = 0; i < 257; i++)
{
char ascii = (char) i;
dictionary.put(Character.toString(ascii), i);
}
for(int k = 0; k < preDictionary.size(); k++)
{
dictionary.put(preDictionary.get(k), counter+k);
}
//inverted dictionary is dictionary with values and keys swapped places
counter = 257;
for(int i = 0; i < 257; i++)
{
char ascii = (char) i;
Invertedictionary.put(i, Character.toString(ascii));
}
for(int k = 0; k < preDictionary.size(); k++)
{
Invertedictionary.put(counter+k, preDictionary.get(k));
}
}
public void fileCompressor() throws Exception
{
for(int i = 0; i < update.size(); i++)
{
if(update.get(i).length() == 1)
{
compressedAscii.add((int) (update.get(i).charAt(0)));
}
else
{
compressedAscii.add(dictionary.get(update.get(i)));
}
}
convertIntValuesToBinary();
}
public void convertIntValuesToBinary()throws Exception
{
for(int i = 0; i < compressedAscii.size(); i++)
{
os.writeInt(compressedAscii.get(i));
}
for(int i = 0; i < compressedAscii.size(); i++)
{
try {
int p = is.readInt();
decodedFileUnicodeValues.add(p);
}catch(IOException e){ }
}
for(int i = 0; i < decodedFileUnicodeValues.size(); i++)
{
int temp = decodedFileUnicodeValues.get(i);
decodedFile.write(Invertedictionary.get(temp));
}
decodedFile.close();
}
public void fileEncoder()throws Exception
{
convertFileStringToArrayDictionary();
dictionaryPopulators();
fileCompressor();
}
public static void main(String[] args)throws Exception
{
LZW test = new LZW();
test.fileEncoder();
}
}