-2

我已经在 java 中为 lzw 算法编写了代码。解压时进入无限循环。255 以上的值以不同的方式读取。我用一个例子给出了 ma 代码。

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 输入: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 BUILD SUCCESSFUL (total time: 0 seconds)// 它应该写 lzwSize 但它没有写

解压后:输出:115输出:116输出:97输出:99输出:107输出:32输出:111输出:118输出:101输出:114输出:102输出:108输出:111输出:119输出:32输出: 105输出:115输出:32输出:98输出:101

4

1 回答 1

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

应该

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

因为可能 getCode(fileIn) 永远不会返回 lzSize 并且它将永远循环。我不知道 lzw 解压只是通过查看您的代码来猜测。

于 2013-03-25T17:54:04.940 回答