从j2me polish感受开源代码

2009/12/30 16:02:43
最近再被molebox折磨时,发现里面使用了zlib压缩,不得已,决定再补补zlib压缩的知识。听说j2me polish里面有个zip压缩类,所以就看了下源代码,竟然发现他在生成huffman树的深度过程中处理溢出时,竟然有错误。晕倒,不是网上老说Open Sourse的错误会更快的被发现和纠正,咋现在版本都2.07了,还有错误啊。国内j2me polish感觉用的挺火的,难道手机上就没人用过ZIP这部分功能。感觉网上谈到开源代码时,都是说有很多人帮着修复bug.我在想像很多开源项目Freebsd, linux, GCC的代码究竟有几个人真的懂,又有多少人去读。可能很多开源代码对我们的意义只剩下另一点,如果有bug,因为有代码我们可以自己修复。
下面为存在问题的代码(de.enough.polish.util.zip.ZipHelper类的genTreeLength方法中):
// check for overflow
int overflowCount=0;
for (int i = 0; i < huffmanCodeLength.length; i++) {
if (huffmanCodeLength[i]>max_len) {
overflowCount++;
}
}
// fix the overflow
if (overflowCount!=0){
//#debug
System.out.println(" fixing " + overflowCount + " overflows because of max=" + max_len);

// list the overflows
short overflows[]= new short[overflowCount];
overflowCount=0;
for (short i = 0; i < huffmanCodeLength.length; i++) {
if (huffmanCodeLength[i]>max_len) {
overflows[overflowCount++]=i;
}
}
// find the index of the smalest node
int minNode=0;
for (int i = 0; i < huffmanCodeLength.length; i++) {
if (huffmanCodeLength[i]!=0 && huffmanCodeLength[minNode]>huffmanCodeLength[i]){
minNode=i;
}
}

// ok lets go and fix it....
int exendableNode;
int overflow1, overflow2;
while(overflowCount!=0){
exendableNode=minNode;
// find the largest expandable length
for (int i = 0; i < huffmanCodeLength.length; i++) {
if(huffmanCodeLength[i]<max_len && huffmanCodeLength[exendableNode]<huffmanCodeLength[i]){
exendableNode=i;
}
}

// find the deepest two overflows
overflow1=0;
overflow2=0;
for (int i = 0; i < overflows.length; i++) {
if(huffmanCodeLength[overflows[i]]> huffmanCodeLength[overflow1]){
overflow1=overflows[i];

} else if(huffmanCodeLength[overflows[i]]== huffmanCodeLength[overflow1]) {
overflow2=overflows[i];
}
}

// insert one of them at the best exendableNode
huffmanCodeLength[exendableNode]++;
huffmanCodeLength[overflow1]=huffmanCodeLength[exendableNode];

// lift the other one
huffmanCodeLength[overflow2]--;

// refresh the overflow count
overflowCount--;
if (huffmanCodeLength[overflow2]==max_len){
overflowCount--;
}
}
}
红色部分为有问题的部分。overflow2的值应该为第二最大溢出值。
[email protected]发了封邮件,也算为开源做了点贡献。只是不知问题能不能被修复。

你可能感兴趣的:(linux,gcc,FreeBSD,Go)