当前位置:编程学习 > JAVA >>

重大发现一个jdk的大bug!高人速来!

package com.yicha.utility;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

public class TestUtil {
public static String gunzip(String compressedStr){
if(compressedStr==null){
return null;
}
ByteArrayOutputStream out= new ByteArrayOutputStream();
ByteArrayInputStream in=null;
GZIPInputStream ginzip=null;
byte[] compressed=null;
String decompressed = null;
try {
compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
in=new ByteArrayInputStream(compressed);
ginzip=new GZIPInputStream(in);

byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed=out.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ginzip != null) {
try {
ginzip.close();
} catch (IOException e) {}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {}
}
}
return decompressed;
}

public static void toStart(){
String m = TestUtil.gunzip("H4sIAAAAAAAAAAEYAOf/YWJj5rC055S16LS5YWJj5ZKM6JCo5ouJwwfq+RgAAAA=");
System.out.println("测试:"+m);
}
    //直接运行和在servlet中调用Start()方法结果不一样
public static void main(String[] args) throws IOException {   
toStart();
}
}


这是一个工具类,main方法直接运行打印出结果,
把这个类在servlet中调用toStart(),打印结果不一样。
天大的bug啊。 --------------------编程问答-------------------- 可能跟字符集有关系,应该不是bug
如果你指定base64编码的字符集,我猜结果肯定一样! --------------------编程问答-------------------- --------------------编程问答-------------------- 结果肯定不一样 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 我没看主帖。

一般的这种帖子都是发帖人脑子里面有虫。

先顶后看。
--------------------编程问答-------------------- 具体调用环境你都没说 你这不是扯么 --------------------编程问答--------------------
引用
Converts the buffer's contents into a string decoding bytes using the platform's default character set. The length of the new String is a function of the character set, and hence may not be equal to the size of the buffer. 

servlet运行平台的默认编码和桌面的默认编码一致吗 --------------------编程问答-------------------- 跟环境没关系好不好 --------------------编程问答-------------------- 都是控制台打印,都没懂我意思? --------------------编程问答-------------------- 今天没时间,改天测试下楼主的问题。 --------------------编程问答-------------------- http://bbs.csdn.net/topics/390298088
80分算法题 --------------------编程问答-------------------- 话说,LZ写代码都从来不考虑字符集的吗??
decompressed = out.toString("utf-8");
自己再去试....
自己不会用就说java有bug...你不是在找喷吗 --------------------编程问答-------------------- 拿无知当个性,悲哀! --------------------编程问答-------------------- hh
过来顶下lz
分给我啊
虽然你有点冲动
但是我觉得你很有怀疑精神! --------------------编程问答-------------------- 你用servlet的时候servlet容器会提供一个编码,和虚拟机提供的编码不一样的 --------------------编程问答-------------------- 如果你觉得你发现了BUG,你可以直接提交问题给甲骨文,说不定还能获得一笔奖金。
但是,我觉得99.9999%的可能性,是因为你没有能理解原理而导致的错误,而不是jdk本身的BUG
难道你觉得这种情况,别人都没遇到过吗?又不复杂。。 --------------------编程问答-------------------- 当我看到结贴率的时候,我震惊了 --------------------编程问答-------------------- 无语无语无语
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,