java.io.EOFException: Unexpected end of ZLIB input stream异常解决

 更新时间:2023年05月25日 09:44:30   作者:FserSuN  
本文主要介绍了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方法被调用即可解决出现的异常。

到此这篇关于java.io.EOFException: Unexpected end of ZLIB input stream异常解决的文章就介绍到这了,更多相关java.io.EOFException内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中ClassLoader类加载学习总结

    Java中ClassLoader类加载学习总结

    本篇文章主要给大家讲述了Java中ClassLoader类加载的原理以及用法总结,一起学习下。
    2017-12-12
  • Java之Scanner.nextLine()读取回车的问题及解决

    Java之Scanner.nextLine()读取回车的问题及解决

    这篇文章主要介绍了Java之Scanner.nextLine()读取回车的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Java设计模式之迭代器模式解析

    Java设计模式之迭代器模式解析

    这篇文章主要介绍了Java设计模式之迭代器模式解析,迭代器模式提供一个对象来顺序访问聚合对象中的一系列数据,而不暴露聚合对象的内部表示,本文提供了部分代码,需要的朋友可以参考下
    2023-09-09
  • JDK8中stream常用方法操作大全

    JDK8中stream常用方法操作大全

    JDK 1.8引入的Stream API是Java函数式编程的核心特性,它提供了一种高效、声明式的方式来处理集合数据,常见的终止操作包括遍历、收集、归约等,这篇文章给大家介绍JDK8中stream常用方法操作大全,感兴趣的朋友一起看看吧
    2026-01-01
  • 超级详细Java JDK环境配置教程(Mac 版)

    超级详细Java JDK环境配置教程(Mac 版)

    这篇文章详细讲解了在MacOS上安装JDK及配置Java环境的步骤,包括下载JDK安装包、安装JDK、查询安装路径以及配置环境变量,旨在为初学者提供一份保姆级的安装指南,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2024-10-10
  • Java访问权限原理与用法详解

    Java访问权限原理与用法详解

    这篇文章主要介绍了Java访问权限,结合实例形式详细分析了java构造者思想、包、访问修饰符等相关原理、应用与操作注意事项,需要的朋友可以参考下
    2020-02-02
  • Java Agent (代理)探针技术详情

    Java Agent (代理)探针技术详情

    这篇文章主要介绍了Java Agent 探针技术详情,Java 中的 Agent 技术可以让我们无侵入性的去进行代理,最常用于程序调试、热部署、性能诊断分析等场景,下文更多相关资料,感兴趣的小伙伴可以参考一下
    2022-04-04
  • 解决springboot+thymeleaf视图映射报错There was an unexpected error (type=Not Found, status=404)

    解决springboot+thymeleaf视图映射报错There was an unexpected erro

    这篇文章主要介绍了解决springboot+thymeleaf视图映射报错There was an unexpected error (type=Not Found, status=404)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Java中Queue以及Deque用法示例详解

    Java中Queue以及Deque用法示例详解

    在Java集合框架中Queue和Deque接口是两种重要的数据结构,它们用于存储和管理元素序列,这篇文章主要介绍了Java中Queue以及Deque用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-08-08
  • springboot+thymeleaf+druid+mybatis 多模块实现用户登录功能

    springboot+thymeleaf+druid+mybatis 多模块实现用户登录功能

    这篇文章主要介绍了springboot+thymeleaf+druid+mybatis 多模块实现用户登录功能,本文通过示例代码图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07

最新评论