资源简介
数据压缩在很多领域都要用到,本程序实现了数据的压缩和解压双向过程
代码片段和文件信息
package demo1;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class BitInputStream extends FilterInputStream
{
public BitInputStream(InputStream is)
{ super(is) ; }
class BitManager {
private int[] buf = new int[8] ;
private int cnt = -1 ;
boolean atTheEnd()
{ return ((buf[7]==1)&&(cnt<0)) ; }
void setTheEnd()
{ buf[7]=1; cnt=-1; }
boolean noMoreBuffer()
{ return cnt<0 ; }
void setNext(int next)
{
for (cnt=0; cnt<8; ++cnt) {
buf[cnt] = next % 2 ;
next /= 2 ;
}
if (buf[7]==1) {
for (cnt=7;cnt>=0;cnt--)
if (buf[cnt]==0)
break ;
cnt-- ;
} else {
cnt = 6 ;
}
}
int getNext()
{ return buf[cnt--] ; }
int left()
{ return cnt+1 ; }
} ;
BitManager bitManager = new BitManager() ;
byte[] tempBuf = null ;
int tempBufPtr = 0 ;
int tempBufLen = 0 ;
private int readNextByte() throws IOException
{ int val = -1 ;
if (tempBufPtr==tempBufLen)
val = super.read() ;
else {
byte b = tempBuf[tempBufPtr++] ;
if ((b&0x80)>0)
val = ((int)(b&0x7F))|0x80 ;
else
val = b ;
}
return val ;
}
public int read() throws IOException {
if (bitManager.atTheEnd())
return -1 ;
if (bitManager.noMoreBuffer()) {
int i = readNextByte() ;
if (i<0)
bitManager.setTheEnd() ;
else
bitManager.setNext(i) ;
return read() ;
}
return bitManager.getNext() ;
}
public int read(byte[] arr) throws IOException
{ return read(arr0arr.length) ; }
public int read(byte[] arr int off int len) throws IOException
{ int bytelen = ( (len-bitManager.left()) / 7 ) ;
tempBuf = new byte[bytelen] ;
tempBufLen = in.read(tempBuf) ;
tempBufPtr = 0 ;
for (int i=0;i int next = read() ;
if (next<0)
return i ;
arr[off+i]=(byte) next ;
}
return len ;
}
public static void main(String[] args)
{
if (args.length<2)
{
System.out.println(“Usage: java BitInputStream FromFile ToFile“) ;
System.out.println(“where ‘FromFile‘ is a file to be open as a Bit Stream“) ;
System.out.println(“and they are written as characters of ‘0‘s and ‘1‘s“) ;
System.out.println(“every line having one char“) ;
System.exit(1) ;
}
try {
InputStream is = new BitInputStream(new BufferedInputStream(new FileInputStream(args[0]))) ;
PrintWriter os = new PrintWriter(new OutputStreamWriter(new FileOutputStream(args[1]))) ;
int next ;
while ((next=is.read())>=0)
os.println(next) ;
is.close() ;
os.close() ;
} catch (FileNotFoundException fnfe) {
System.o
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 232 2009-11-21 21:39 demo1\.classpath
文件 381 2009-11-21 21:39 demo1\.project
文件 1251 2010-01-07 12:31 demo1\bin\demo1\BitInputStream$BitManager.class
文件 3374 2010-01-07 12:31 demo1\bin\demo1\BitInputStream.class
文件 969 2010-01-07 12:31 demo1\bin\demo1\BitOutputStream$BitManager.class
文件 2915 2010-01-07 12:31 demo1\bin\demo1\BitOutputStream.class
文件 2102 2010-01-07 12:33 demo1\bin\demo1\ByteArray.class
文件 256 2010-01-07 12:34 demo1\bin\demo1\Compression.class
文件 1367 2010-01-07 12:33 demo1\bin\demo1\Dict.class
文件 522 2010-01-07 12:29 demo1\bin\demo1\LimitedDict.class
文件 836 2010-01-07 14:11 demo1\bin\demo1\LZW$UnClosedOutputStream.class
文件 3753 2010-01-07 14:11 demo1\bin\demo1\LZW.class
文件 3218 2010-01-07 12:30 demo1\src\demo1\BitInputStream.java
文件 2474 2010-01-07 12:31 demo1\src\demo1\BitOutputStream.java
文件 1386 2010-01-07 12:32 demo1\src\demo1\ByteArray.java
文件 291 2010-01-07 12:34 demo1\src\demo1\Compression.java
文件 657 2010-01-07 12:33 demo1\src\demo1\Dict.java
文件 225 2010-01-07 12:29 demo1\src\demo1\LimitedDict.java
文件 2932 2010-01-07 14:11 demo1\src\demo1\LZW.java
目录 0 2010-01-07 12:34 demo1\bin\demo1
目录 0 2010-01-07 12:33 demo1\src\demo1
目录 0 2010-01-07 12:40 demo1\bin
目录 0 2010-01-07 12:40 demo1\src
目录 0 2010-01-07 14:12 demo1
----------- --------- ---------- ----- ----
29141 24
- 上一篇:db.properties
- 下一篇:win8加载圆圈动画(含源码/demo)
评论
共有 条评论