java - LZW compression algorithm implementation -


i have been reading dictionary based compression algorithms including lzw , lzss. then, wanted implement lzw using java , started working on it. not developer , suspect implementation may not efficient. can @ code , tell me wrong or inefficient in implementation? here full code.

public class lzw {      public hashmap compdic, decompdic;     string filename = "walaloo.txt";     short lastcode = 0, dlastcode = 0;      lzw() {         compdic = new hashmap<string, integer>();         decompdic = new hashmap<integer, string>();         createdictionary();     }      public void createdictionary() {         try {             short code;             char ch;             fileinputstream fis = new fileinputstream(filename);             inputstreamreader rdr = new inputstreamreader(fis, "utf-8");             while ((code = (short) rdr.read()) != -1) {                 ch = (char) code;                  if (!compdic.containskey(ch)) {                     compdic.put("" + ch, code);                     decompdic.put(code, "" + ch);                     if (code > lastcode) {                         lastcode = code;                         dlastcode = code;                     }                 }             }             fis.close();         } catch (exception ex) {             logger.getlogger(lzw.class.getname()).log(level.severe, null, ex);         }     }      public void compressfile() {         try {             short code, codeword;             char c;             string s;              system.out.print("compressing...");             fileinputstream fis = new fileinputstream(filename);             inputstreamreader rdr = new inputstreamreader(fis, "utf-8");             fileoutputstream fos = new fileoutputstream(filename + "1.lzw");             objectoutputstream fout = new objectoutputstream(fos);              s = (char) rdr.read() + "";             while ((code = (short) rdr.read()) != -1) {                 c = (char) code;                  if (!compdic.containskey(s + c)) {                     codeword = short.parseshort(compdic.get(s).tostring());                      fout.writeshort(codeword);                     compdic.put(s + c, ++lastcode);                     s = "" + c;                 } else {                     s = s + c;                 }             }              codeword = short.parseshort(compdic.get(s).tostring());             fout.writeshort(codeword);             fout.writeshort(00);              fout.close();             fis.close();              system.out.print("done");          } catch (exception ex) {             logger.getlogger(lzw.class.getname()).log(level.severe, null, ex);         }     }      public void decompressfile() {         short priorcode = -1, codeword = -1;         char c;          string priorstr, str;         fileinputstream fis;          filewriter fos;          objectinputstream fin;          try {             fis = new fileinputstream(filename + "1.lzw");             fos = new filewriter(filename + "2.txt");             fin = new objectinputstream(fis);              system.out.print("\ndecompressing...");             priorcode = fin.readshort();             fos.write(decompdic.get(priorcode).tostring());             while ((codeword = fin.readshort()) != -1) {                 if(codeword == 00)                     break;                  priorstr = decompdic.get(priorcode).tostring();                  if (decompdic.containskey(codeword)) {                     str = decompdic.get(codeword).tostring();                     fos.write(str);                     decompdic.put(++dlastcode, priorstr + str.charat(0));                 } else {                     decompdic.put(++dlastcode, priorstr + priorstr.charat(0));                     fos.write(priorstr + priorstr.charat(0));                 }                  priorcode = codeword;             }              fos.close();             fis.close();             system.out.print("done\n");          } catch (exception ex) {             //logger.getlogger(lzw.class.getname()).log(level.severe, null, ex);             system.out.println("\n\nerror: " + ex.getmessage());             system.out.print(codeword + " " + priorcode + " " + decompdic.get(133) + " " + dlastcode);         }     }      public static void main(string args[]) {         lzw lzw = new lzw();         lzw.compressfile();         lzw.decompressfile();     } } 


Comments