日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lzw压缩 java_java实现的LZW 压缩算法源码 | 学步园

發(fā)布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lzw压缩 java_java实现的LZW 压缩算法源码 | 学步园 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

導讀:/*

* LZW.java

*

* Created on 01 Dec 2005

*

* Implementation of LZW compression/decompression algorithm

*/

import java.io.* ;

/**

*

* @author Moshe Fresko

* @courseAlgorithmic Programming 1

* @exercise3

*/

public class LZW implements Compression

{

boolean stopped = false ;

Dict dict ;

// The bits that should be written for each code

int numOfBits ;

// The previous string that we should remember

// in order to insert into the dictionary

final ByteArray emptyBA = new ByteArray() ;

ByteArray w=emptyBA ;

// Constructor gets the number of bits to be written for each code

public LZW()

{

numOfBits = 12 ;

// Create a new Limited Dictionary

// For maximum of 2^bits entries

dict = new LimitedDict(1// Add all ascii characters to the dictionary

for (int i=0;idict.add(new ByteArray((byte)i)) ;

}

// Encodes the next character.

// If there is a code generated returns it.

// If not returns -1.

int encodeOneChar(int n) {

byte c = (byte) n ;

ByteArray nw = w.conc(c) ;

int code = dict.numFromStr(nw) ;

// if it exists then we continue to search for a longer string

if (code!=-1) {

w = nw ;

return -1 ;

} else {

dict.add(nw) ;

nw = w ;

w = new ByteArray(c) ;

return dict.numFromStr(nw) ;

}

}

// If there is something left in w, returns its code

int encodeLast() {

ByteArray nw = w ;

w = emptyBA ;

return dict.numFromStr(nw) ;

}

// Write the code in bits into output stream

void writeCode(OutputStream os, int code) throws IOException

{

for (int i=0;ios.write(code&1) ;

code /= 2 ;

}

}

int readCode(InputStream is) throws IOException

{

int num = 0 ;

for (int i=0;iint next = is.read() ;

if (nextreturn -1 ;

num += next}

return num ;

}

// We need to call the close() method of BitOutputStream,

// but without closing the encompassing OutputStream

private class UnClosedOutputStream extends FilterOutputStream {

public UnClosedOutputStream(OutputStream os)

{ super(os) ; }

public void write(byte b[], int off, int len) throws IOException

{ out.write(b,off,len) ; }

// Does not close anything

public void close() throws IOException

{ }

}

public void compress(InputStream is, OutputStream os) throws IOException {

os = new BitOutputStream(new UnClosedOutputStream(os)) ;

int next ;// next input character

int code ;// next code generated

while ((next=is.read())>=0) {

if (stopped)

break ;

code = encodeOneChar(next) ;

if (code>=0)

writeCode(os,code) ;

}

code = encodeLast() ;

if (code>=0)

writeCode(os,code) ;

os.close() ;

}

ByteArray decodeOne(int code) {

// Either "ABA" or null, w="AB"

ByteArray str = dict.strFromNum(code) ;

if (str==null) {

str = w.conc(w.getAt(0)) ;

dict.add(str) ;

} else

if (! w.isEmpty())

dict.add(w.conc(str.getAt(0))) ;

w = str ;

return w ;

}

public void decompress(InputStream is, OutputStream os) throws IOException {

is = new BitInputStream(is) ;

ByteArray str ;// Next entry

int code ;// Next code to be read

while ((code=readCode(is))>=0) {

if (stopped)

break ;

str = decodeOne(code) ;

os.write(str.getBytes()) ;

}

}

public void stop()

{ stopped = true ; }

public static void main(String args[]){//簡單的測試

LZW lzw=new LZW();

try{

lzw.compress(new FileInputStream("LZW.JAVA"),new FileOutputStream("lzw.lzw"));

lzw.decompress(new FileInputStream("lzw.lzw"),new FileOutputStream("lzw1.java"));

}catch(Exception e){}

}

}

其它文件請下載。

總結

以上是生活随笔為你收集整理的lzw压缩 java_java实现的LZW 压缩算法源码 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。