这段代码是程序中最耗时的部分
dis = new DataInputStream(new BufferedInputStream(new FileInputStream(targetFile)));
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile)));
writeInfo();
int pack=0;
int packLeft=32;
int in;
NodeItem item;
for(long i=0;i<targetFile.length();i++){
in=dis.readByte()+128;
item=table[in/16][in%16];
if(item.length<=packLeft){
packLeft-=item.length;
pack=(pack<<item.length)|item.code;
if(packLeft==0){
dos.writeInt(pack);
packLeft=32;
}
}
else{
pack<<=packLeft;
int temp=item.code;
temp>>>=item.length-packLeft;
pack|=temp;
dos.writeInt(pack);
pack=0;
pack|=item.code;
packLeft=32-(item.length-packLeft);
}
}
item是这个类的一个对象
package datastructure;
public class NodeItem {
public int code;
public byte length;
public int freq;
public byte ch;
public NodeItem(int code,byte length,byte c){
this.code=code;
this.length=length;
ch=c;
}
public NodeItem(int f){
freq=f;
}
public NodeItem(byte c,int f){
freq=f;
ch=c;
}
public int key() {
return freq;
}