java lzw_lzw在java中解压缩

我在java中编写了lzw algo的代码 . 在解压缩时它进入无限循环 . 高于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+= 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<

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 input = new ArrayList();

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

//{

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

{

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

{

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(总时间:0秒)//它应该写lzwSize但它不写

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值