Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry excluding="src/" kind="src" path=""/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LZW_Compression</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
3 changes: 0 additions & 3 deletions Writer.java

This file was deleted.

1 change: 1 addition & 0 deletions decoded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcdefgabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcdefgabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
1 change: 1 addition & 0 deletions intcodesOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
97 98 99 256 258 257 259 262 261 264 260 266 263 267 265 268 271 270 273 269 275 272 276 274 277 280 279 282 278 284 281 285 283 286 289 288 283 100 101 102 103 290 287 298 297 300 291 99 293 295 301 299 302 306 309 308 311 307 313 310 314 312 315 318 317 320 316 322 319 323 321 324 327 326 329 325 331 328 99
1 change: 1 addition & 0 deletions lzw-file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abcabcabcabcabcabcabcabcabcabcabcabc
2 changes: 2 additions & 0 deletions lzw-file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcdefgabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcdefgabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc

83 changes: 83 additions & 0 deletions lzw-file3.txt

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions src/Decoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;

public class Decoder {

//private static double maxTableSize;
private static String decodedOutputFileName = "decoded.txt";

public static String decode (String inputFile, int maxDictSize) throws IOException{
//maxTableSize = Math.pow(2, bit_Length);

String[] compressedValuesStr = null;
List<Integer> compressedValues = new ArrayList<Integer>();
int dictSize = 255;

String inputIntCodes = "";
BufferedReader br = null;
br = new BufferedReader(new FileReader(inputFile));
inputIntCodes = br.readLine();
br.close();
compressedValuesStr = inputIntCodes.split(" ");
for (String str: compressedValuesStr) {
int num = Integer.parseInt(str);
compressedValues.add(num);
}

Map<Integer, String> dict = new HashMap<Integer, String>();
for (int i = 0; i < 255; i++)
dict.put(i, "" + (char) i);

String oldStr = "" + compressedValues.get(0);
StringBuffer decodedValues = new StringBuffer();

String newStr = null;
for (int key : compressedValues) {
if (dict.containsKey(key))
newStr = dict.get(key);
else
newStr = oldStr + oldStr.charAt(0);
decodedValues.append(newStr);

if(dictSize<maxDictSize)
dict.put(dictSize++, oldStr + newStr.charAt(0));

oldStr = newStr;
}
createFile(decodedValues.toString());
return(decodedValues.toString());
}

private static void createFile(String decodedValues) throws IOException{
FileWriter writer = new FileWriter(decodedOutputFileName);
BufferedWriter bw = new BufferedWriter(writer);

try {
bw.write(decodedValues);
}catch (IOException e) {
e.printStackTrace();
}
bw.flush();
bw.close();
}

public static void main(String[] args) throws IOException {
System.out.println(decode("intcodesOutput.txt", 512));
}

}
71 changes: 34 additions & 37 deletions src/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,49 @@
public class Encoder {
private HashMap<String, Integer> dictionary;
BufferedReader in;
String output;
int tracker;
int binary;
int dictTracker;

public Encoder (String filename) throws IOException {
in = new BufferedReader (new FileReader(new File (filename)));
dictionary = new HashMap<String, Integer>();
output = "";
tracker = 256;
binary = 0;
dictTracker = 256;
}

public String encode () throws IOException {
while (true) {
StringBuilder cur = new StringBuilder (in.read());
if (in.read() >= 0) {
String next = in.read();
if (dictionary.containsKey(cur + next)) {
cur.append(next);
}
else {
output += cur;
dictionary.put(cur + next, tracker++);
cur = next;
}
}
else
break;
for (int i = 0; i < 256; i++)//load ascii table
dictionary.put("" + (char)i, i);
StringBuilder sb = new StringBuilder ();
String cur = "";
while (in.ready()) {
String next = ""+(char)in.read();
String combined = cur + next;
if (dictionary.containsKey(combined)) {
cur = combined;
}else {
sb.append(dictionary.get(cur) + " ");
dictionary.put(combined, dictTracker++);
cur = next;
}
}
}

/**
* variable naming is very poor here but they are simple placeholders to go from char to a big binary int.
*/
public void toBinary () {
for (int i = 0; i<output.length(); i++) {
char cur = output.charAt(i);

//The following three lines is not my code
Integer a = Character.getNumericValue(cur);
String b = Integer.toBinaryString(a);
Integer binaryInteger = Integer.valueOf(b);

//back to my code :p
binary = (binary * 100000000) + binaryInteger;

if (!cur.equals("")) {
sb.append(dictionary.get(cur));
}
return sb.toString();
}

public static void main (String [] args) throws IOException {
FileWriter outputFile;
BufferedWriter writer = null;
Encoder coder = new Encoder("lzw-file2.txt");
try {
outputFile = new FileWriter("intcodesOutput.txt");
writer = new BufferedWriter(outputFile);
writer.write(coder.encode());
}finally {
writer.close();
}

}

}