java.io.EOFException: Unexpected end of ZLIB input stream异常处理

2023-10-04 23:50

本文主要是介绍java.io.EOFException: Unexpected end of ZLIB input stream异常处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 streamat 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];}
}@Testpublic 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{@Overridepublic 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方法被调用即可解决出现的异常。

这篇关于java.io.EOFException: Unexpected end of ZLIB input stream异常处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/2952

相关文章

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain