2013-03-25 96 views
-2

我已經在java中編寫了lzw算法的代碼。在解壓縮時它進入無限循環。以不同的方式讀取255以上的值。即時給予ma代碼的例子。在java中的lzw解壓縮

import java.io.*; 
import java.util.*; 
public class test_decom { 
    static final int BUFFERSIZE = 32; 
static final int CHARSIZE  = 12; 
static long buffer;  //32 bits of space, up to 16bit compression, only 24 bits used 
static int bufferBits;  //How many bits are stored in the buffer 
static int bits = 12;   //How many bits per code 
static int lzwSize;  // have writen dis value at the end of compression file but this 
          //also not writing..... **plz help for this also** 
static int getCode(InputStream fp) 
{ 
long temp; 

while(bufferBits <=(BUFFERSIZE - CHARSIZE *2))//Never go over than size - or we loose data 
{            //Also never use full 64 bits, to avoid issues 
    //No point reading anymore, files done :p 
    try 
    { 
      System.out.print("bufferBits : "); 
    if(fp.available() <= 0) 
      break; 

     //test = fp.read(); 
     //buffer |= test << bufferBits; 
     buffer |= fp.read() <<(bufferBits); 
     bufferBits+= CHARSIZE; //Buffer now stores one more char. 
      System.out.println(buffer); 

    } 

    catch(IOException e) 
    { 
    System.out.println("Error in getCode" + e.getMessage()); 
    } 
} 
    temp = (buffer << (64-bits)) >>> (64-bits); //We remove the excess bits 
    buffer >>>= bits; //Remove the bits from buffer 
    bufferBits -= bits; 
     System.out.println("temp : "+temp); 
    return (int)temp; //we return the correct code 
} 

public static void main(String arg[]) throws IOException 
{ 
    // int prefix[] = new int[20]; 
    //int nextCode = 0; 
    // String dict[] = new String [20]; 
    lzwSize = (short)(1<<(bits)-1); 
    BufferedInputStream fileIn = null; 
    BufferedOutputStream fileOut = null; 
try 
{ 
    fileIn = new BufferedInputStream (new FileInputStream ("C:\\Documents and Settings\\project\\LzwTut\\test1_test.txt")); 
    fileOut = new BufferedOutputStream(new FileOutputStream("C:\\Documents and Settings\\gauri\\project\\practice\\LzwTut\\test1_Test.txt")); 
} 
catch(IOException e) 
{ 
    System.out.println("Unable to load file " + e.getMessage()); 
} 
    ArrayList<Integer> input = new ArrayList<Integer>(); 
    Dictionary dic = new Hashtable(); 
    String val = null; 

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    int ab; 
    while((ab=getCode(fileIn))!=lzwSize){ //i tried putting -1 but that is also not working 
             //problem in this.... 
     //initialization 
     input.add(ab); 

    } 
     // input.get(i) = Integer.parseInt(br.readLine()); 
    for(int i=0;i<input.size()-1;i++) 
    //{ 
    try{ 

     if(input.get(i)<=255) 
     { 
      fileOut.write(input.get(i)); 
      //System.out.println("output : "+input.get(i)); 
      if(input.get(i+1)>255) 
      { 
       val = (String)dic.get(input.get(i+1)-256); 
       dic.put(i, input.get(i)+" "+val); 
      // System.out.println("dic.put("+i+", "+input[i]+" "+val+")"); 
      } 
      else 
      { 
       dic.put(i, input.get(i)+" "+input.get(i+1)); 
       //System.out.println("dic.put("+i+", "+input[i]+" "+input[i+1]+")"); 
      } 

      // addDict(i,input[i],input[i+1]);//abhi tak out of bounds not solved 

     } 
     else 
     { 
      val = (String)dic.get(input.get(i)-256); 
      //System.out.print("output : "); 
      for(int k=0;k<val.length();k++) 
      { 
      if(val.charAt(k) == ' ') 
       continue; 
      else 
      { 
       fileOut.write(val.charAt(k)); 
      // System.out.print(val.charAt(k)); 
      } 
      } 
     // System.out.println(); 
      if(input.get(i+1)>255) 
      { 
       String val1 = (String)dic.get(input.get(i+1)-256); 
       dic.put(i, val+" "+val1); 
       // System.out.println("dic.put("+i+", "+val+" "+val1+")"); 

      } 
      else 
      { 
       dic.put(i, val+" "+input.get(i+1)); 
       // System.out.println("dic.put("+i+", "+val+" "+input[i+1]+")"); 
      } 

     } 

    } 
catch(Exception e) 
{} 
    if(input.get(input.size() - 1)>255) 
    { 
     //System.out.print("output : "); 
      for(int k=0;k<val.length();k++) 
      { 
       if(val.charAt(k) == ' ') 
        continue; 
       else 
       { 
        // System.out.print(val.charAt(k)); 
        fileOut.write(val.charAt(k)); 
       } 
      } 

    } 
    else{ 
    //System.out.println("output : "+input.get(input.size() - 1)); 
    fileOut.write(input.get(input.size() - 1)); 
    } 
    try{ 
     fileIn.close(); 
     fileOut.close(); 
    } 
    catch(Exception e) 
    { 

    } 
     // System.out.println("output : "+input[9]); 
} 
} 

例如: txt文件test_test.txt包含: 堆棧溢出堆棧溢出.......... 壓縮後: 輸入:115 輸入:116輸出:115 輸入: 97輸出:116 輸入:99輸出:97 輸入:107輸出:99 輸入:32輸出:107 輸入:111輸出:32 輸入:118輸出:111 輸入:101輸出:118 輸入:114輸出:101 inpu T:102輸出:114 輸入:108輸出:102 輸入:111輸出:108 輸入:119輸出:111 輸入:32輸出:119 輸入:105輸出:32 輸入:115輸出:105 輸入:32輸出:115 輸入:98輸出:32 輸入:101安輸出:98 輸入:115輸出:101 輸入:116 輸入:32輸出:256 輸入:102輸出:32 輸入:111輸出: 102 輸入:114輸出:111 輸入:117輸出:114 輸入:109輸出:117 輸入:46輸出:109 輸入:32輸出:46 輸入:115輸出:32 輸入:116 輸入:97輸出:256 輸入:99 輸入:107輸出:258 輸入:32 輸入:111 輸出:260輸入:118 輸入:101輸出:262 輸入:114 輸入:102輸出:264 輸入:108 輸入:111輸出:266 輸入:119 輸入:46輸出:268 生成成功(總時間:0秒)//它應該寫入lzwSize但它不會寫入

解壓後: 輸出:115 輸出:116 輸出:97 輸出:99 輸出:107 輸出:32 輸出:111 輸出:118 輸出:101 輸出:114 輸出:102 輸出:108 輸出:111 輸出:119 輸出:32 輸出:105 輸出:115 輸出:32 輸出:98 輸出: 101

+2

你的問題是什麼? – 2013-03-25 17:51:50

回答

0
while((ab=getCode(fileIn))!=lzwSize)// I think here is the problem 

應該

while((ab=getCode(fileIn))<=lzwSize) 

因爲可能會引用代碼(FILEIN)將永遠不會返回lzSize,它會永遠循環。我不知道lzw解壓只是通過查看你的代碼來猜測。

+0

如果我爲(int i = 0; i user2159239 2013-03-25 17:56:35