Decompress.java |
| package zzhao_ex9; import java.util.*; import java.io.*; public class Decompress{ public static void main(String[] args){ Decompress tester=new Decompress(); String inputfilename = ""; String outputfilename = ""; if (args.length == 1){//ARGS="test" inputfilename = args[0]; }//else{System.out.println(args.length); //BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); //inputfilename ="test"; //PrintWriter output = new PrintWriter(new FileWriter(new File(inputfilename))); //String str=""; //while((str=input.readLine())!=null){ //output.print(str); //} // } outputfilename = inputfilename + ".DEC"; try{ tester.decompress(inputfilename, outputfilename); }catch(IOException e){ System.out.println("Error: " + e); System.exit(1); } }//main public void decompress(String inputfilename, String outputfilename)throws IOException{ Vector string_table = new Vector(); int MAX_SIZE=4000; for(int i=0; i<256; i++) string_table.add(String.valueOf((char)i)); int oldCode=0, newCode=0; try{ BufferedInputStream inBuf = new BufferedInputStream(new FileInputStream(inputfilename)); BitStreamInputStream breader = new BitStreamInputStream(inBuf, 12); BufferedOutputStream bwriter = new BufferedOutputStream(new FileOutputStream(outputfilename)); String aString = "", aChar = ""; oldCode = (int)breader.readBitField(); aChar=String.valueOf((char)oldCode); bwriter.write(oldCode); while((newCode = (int)breader.readBitField())!=-1){ if(newCode >= string_table.size()){ aString =(String)string_table.get(oldCode); aString = aString + aChar; }else{ aString = (String)string_table.get(newCode); } bwriter.write(aString.getBytes()); aChar = aString.substring(0, 1); string_table.add((String)string_table.get(oldCode) + aChar); oldCode = newCode; }// end while if(string_table.size()>MAX_SIZE){ string_table.removeAllElements(); for(int j=0; j<256; j++) string_table.add(String.valueOf((char)j)); } bwriter.close(); breader.close(); }catch(IOException e){System.out.println("Error: " + e);} //if(string_table.size()>MAX_SIZE){ //string_table.removeAllElements(); //for(int j=0; j<256; j++) //string_table.add(String.valueOf((char)j)); //} //for testing only-table ok //for(int i=0; i<string_table.size(); i++) //if(i>255) //System.out.println(" in hashtable: " + // + i + " " + (String)string_table.elementAt(i)); }// decompress } |
James Little |