java.io.EOFException: Unexpected end of ZLIB input stream异常处理
来源:互联网 发布:sql dense rank 编辑:程序博客网 时间:2024/05/23 20:24
java.io.EOFException: Unexpected end of ZLIB input stream异常处理
因需要完成压缩与解压缩功能,所以使用到java.util.zip中的类。同时使用了jdk 1.7 try with resource 的特性,结果暴出java.io.EOFException: Unexpected end of ZLIB input stream异常。
java.io.EOFException: Unexpected end of ZLIB input stream at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240) at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158) at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:117) at java.io.FilterInputStream.read(FilterInputStream.java:107) at com.sf.framework.rpc.util.NioUtils.unzip(NioUtils.java:27) at framework.rpc.util.NioUtilsTest.unzipTest(NioUtilsTest.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
代码如下:
public class NioUtils { public static byte[] zip(byte[] bytes) { if (bytes != null && bytes.length > 0) { ByteArrayOutputStream byteOs = new ByteArrayOutputStream(); try(GZIPOutputStream gZipOs = new GZIPOutputStream(byteOs)) { gZipOs.write(bytes); return byteOs.toByteArray(); } catch (IOException e) { e.printStackTrace(); } } return new byte[0]; } public static byte[] unzip(byte[] bytes){ try (GZIPInputStream gZipIs = new GZIPInputStream (new ByteArrayInputStream(bytes)); ByteArrayOutputStream bos = new ByteArrayOutputStream()){ byte[] buff = new byte[512]; int read = gZipIs.read(buff); while(read > 0){ bos.write(buff,0,read); read = gZipIs.read(buff); } return bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return new byte[0]; }} @Test public void unzipTest(){// NioUtils.closeTest(); byte[] bytes = str.getBytes(); byte[] ziped = NioUtils.zip(bytes); byte[] unziped = NioUtils.unzip(ziped); String unZipedStr = new String(unziped); Assert.assertTrue(str.equals(unZipedStr)); }
原本是想里用try-with-resource完成自动close,结果在解压缩获取到的压缩数据时出现异常。随后看了GZIPOutputStream 的源码,原来调用close的时候会填充一些压缩信息,这样才能在解压缩时正常解压缩。而上面的代码,bos.toByteArray();是在bos.close()调用前被调用,因此返回了不正确的字节数组,造成解压缩失败。
下面是执行try-with-resource执行顺序的一个测试。
public class NioUtils { public static T1 closeTest(){ try(T t = new T()){ return new T1(); } catch(Exception e){ e.printStackTrace(); } return null; }}class T implements AutoCloseable{ @Override public void close() throws Exception { System.out.println("AutoCloseable"); }}class T1{ T1(){ System.out.println("a class for test."); }}输出:a class for test.AutoCloseable
可以看到close是在return 中new T()调用完后执行。
最后保证在对于解压缩,保证在压缩返回字节数组前close方法被调用即可解决出现的异常。
0 0
- java.io.EOFException: Unexpected end of ZLIB input stream异常处理
- java.io.EOFException: Unexpected end of ZLIB input stream
- java.io.EOFException: Unexpected end of ZLIB input stream解决
- java.io.EOFException: Unexpected end of input stream
- hive Caused by: java.io.EOFException: Unexpected end of input stream排查
- JAVA_SAE_Fatal_error: Unexpected end of ZLIB input stream
- Unexpected end of ZLIB input stream
- maven打包异常, Error creating shaded jar: Unexpected end of ZLIB input stream
- Unexpected end of ZLIB input stream的解决办法
- java.io.IOException: unexpected end of stream
- 使用GZIPOutputStream解压byte[]出现Unexpected end of ZLIB input stream的解决方法
- java.io.IOException: unexpected end of stream on okhttp3.Address
- Unexpected end of stream.
- java.net.ProtocolException: unexpected end of stream
- java.io.EOFException: No content to map to Object due to end of input
- java.io.EOFException: No content to map to Object due to end of input
- java.io.EOFException: No content to map to Object due to end of input
- java.io.IOException: unexpected end of stream on okhttp3.Address@33c2c47
- 【leetcode】36. Valid Sudoku
- PCB设计经验:布局基本要领
- setTimeout初探(二):零延迟的定时任务
- 关于字符数组与字符串指针的区分
- 61%的美国民众担心自己的私家车以及家庭安保摄像探头会遭到黑客攻击
- java.io.EOFException: Unexpected end of ZLIB input stream异常处理
- Android手机抓包教程
- CentOS卸载OpenJDK并安装Sun JDK
- Git中Read.MD文件格式:Markdown语言详解
- cocos2dx学习之路----第十一篇(对声音的操作处理)
- 排序算法 之 选择排序SelectionSort
- C# Socket编程
- 数据结构实验之栈三:后缀式求值
- Linq 的基本操作 之Union All/Union/Intersect 操作