2013-03-25 52 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