diff --git a/.DS_Store b/.DS_Store index 331c402..31d0ec0 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/src/LZW.class b/src/LZW.class new file mode 100644 index 0000000..8a6778d Binary files /dev/null and b/src/LZW.class differ diff --git a/src/LZWDecoder.class b/src/LZWDecoder.class new file mode 100644 index 0000000..7211596 Binary files /dev/null and b/src/LZWDecoder.class differ diff --git a/src/LZWDecoder.java b/src/LZWDecoder.java index e8e25ad..fe92a25 100644 --- a/src/LZWDecoder.java +++ b/src/LZWDecoder.java @@ -5,6 +5,7 @@ import java.io.InputStream; import java.io.PrintWriter; import java.util.HashMap; +import java.util.*; public class LZWDecoder { @@ -14,9 +15,10 @@ public class LZWDecoder { private byte [] byteArray; private String finalOutput; - public LZWDecoder(String binString, int bitNum, String outputFileName) throws IOException + public LZWDecoder(String binString, int bitNum, String outputFileName) throws IOException // this is the constructor { - this.dict = new HashMap (); + try { + this.dict = new HashMap (); // intializes the hash map this.bitNum = bitNum; this.binString = binString; this.finalOutput = ""; @@ -24,12 +26,18 @@ public LZWDecoder(String binString, int bitNum, String outputFileName) throws IO // File binFile = new File (binFileName); // readBinFile(binFile); - decode(); + //decode(); writeToTxt(outputFileName); + } + catch(Exception e) { + System.out.println("File inputed not found"); + } + } //reads the binary file into a string. not working, so I've bypassed this method for now and just input a binary string into the constructor. + /* public void readBinFile(File binFile) throws IOException { FileInputStream is = new FileInputStream(binFile); @@ -42,67 +50,63 @@ public void readBinFile(File binFile) throws IOException is.close(); } - - - public void decode () - { - for (int x = 0; x<256; x++) - { - char ch = (char)x; - dict.put(x, String.valueOf(ch)); +*/ + public ArrayList getList () throws IOException{ + String binary = binString; // copies the binary string + String temp=""; // creates temp varibale + ArrayList nums= new ArrayList (); // creates an arraylist to put all the numbers when converting from binary to integers + while (binary.length()>=bitNum){ + int number=0; + temp=binary.substring(0,bitNum); // takes substring and sets to temp + number=Integer.parseInt(temp,2); // turns binary to integer number + nums.add(number); // adds the number into an arrayList of nums + binary=binary.substring(bitNum);// chops the string off } - String binStringCopy = binString; - String currBinString = binString.substring (0,bitNum); - binString= binString.substring(bitNum); - int currDecimal = Integer.parseInt(currBinString, 2); - String currString = dict.get(currDecimal); -// finalOutput = currString; - - String nextBinString = ""; - int nextDecimal= 0; - String nextString= ""; - -// String lastSymbolInDict = ""; - - nextBinString = binString.substring(0, bitNum); - binString= binString.substring(bitNum); - nextDecimal = Integer.parseInt(nextBinString, 2); - nextString = dict.get(nextDecimal); - - int counter = 256; - while (binString.length()>= bitNum) - { - - if (nextString!= null) - { - dict.put(counter, currString+ nextString.substring(0,1)); - currString = nextString; - + return (nums); + } - } - else - { - dict.put(counter, currString + currString.substring(0,1)); - currString = currString + currString.substring(0,1); - } - - nextBinString = binString.substring(0, bitNum); - binString= binString.substring(bitNum); - nextDecimal = Integer.parseInt(nextBinString, 2); - nextString = dict.get(nextDecimal); - - counter++; - } - - for (int x = 0; x numbers = getList(); // calls other method to get arraylist of nums + int counter=256; // keeps track of how big the hash map is + HashMap map = new HashMap (); // intialized and delcres hash map + int current=0; + int next=0; + String word="";// this will eventually be the whole word that will get returned + String combined ="";// string that contains current and next as one string + String wordC=""; // the string version of the number of the current value + String wordN=""; // the string version of the number of the next value + for (int i=0; i<256; i++){ + map.put(i, ""+(char)i); // created dictionary assigning ascii values to the first 255 characters + } + int size=numbers.size(); + for (int i=0; i