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全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo