最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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異常解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

因需要完成壓縮與解壓縮功能,所以使用到j(luò)ava.util.zip中的類。同時使用了jdk 1.7 try with resource 的特性,結(jié)果暴出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,結(jié)果在解壓縮獲取到的壓縮數(shù)據(jù)時出現(xiàn)異常。隨后看了GZIPOutputStream 的源碼,原來調(diào)用close的時候會填充一些壓縮信息,這樣才能在解壓縮時正常解壓縮。而上面的代碼,bos.toByteArray();是在bos.close()調(diào)用前被調(diào)用,因此返回了不正確的字節(jié)數(shù)組,造成解壓縮失敗。

下面是執(zhí)行try-with-resource執(zhí)行順序的一個測試。

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()調(diào)用完后執(zhí)行。

最后保證在對于解壓縮,保證在壓縮返回字節(jié)數(shù)組前close方法被調(diào)用即可解決出現(xiàn)的異常。

到此這篇關(guān)于java.io.EOFException: Unexpected end of ZLIB input stream異常解決的文章就介紹到這了,更多相關(guān)java.io.EOFException內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中ClassLoader類加載學(xué)習(xí)總結(jié)

    Java中ClassLoader類加載學(xué)習(xí)總結(jié)

    本篇文章主要給大家講述了Java中ClassLoader類加載的原理以及用法總結(jié),一起學(xué)習(xí)下。
    2017-12-12
  • Java之Scanner.nextLine()讀取回車的問題及解決

    Java之Scanner.nextLine()讀取回車的問題及解決

    這篇文章主要介紹了Java之Scanner.nextLine()讀取回車的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java設(shè)計(jì)模式之迭代器模式解析

    Java設(shè)計(jì)模式之迭代器模式解析

    這篇文章主要介紹了Java設(shè)計(jì)模式之迭代器模式解析,迭代器模式提供一個對象來順序訪問聚合對象中的一系列數(shù)據(jù),而不暴露聚合對象的內(nèi)部表示,本文提供了部分代碼,需要的朋友可以參考下
    2023-09-09
  • JDK8中stream常用方法操作大全

    JDK8中stream常用方法操作大全

    JDK 1.8引入的Stream API是Java函數(shù)式編程的核心特性,它提供了一種高效、聲明式的方式來處理集合數(shù)據(jù),常見的終止操作包括遍歷、收集、歸約等,這篇文章給大家介紹JDK8中stream常用方法操作大全,感興趣的朋友一起看看吧
    2026-01-01
  • 超級詳細(xì)Java?JDK環(huán)境配置教程(Mac?版)

    超級詳細(xì)Java?JDK環(huán)境配置教程(Mac?版)

    這篇文章詳細(xì)講解了在MacOS上安裝JDK及配置Java環(huán)境的步驟,包括下載JDK安裝包、安裝JDK、查詢安裝路徑以及配置環(huán)境變量,旨在為初學(xué)者提供一份保姆級的安裝指南,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-10-10
  • Java訪問權(quán)限原理與用法詳解

    Java訪問權(quán)限原理與用法詳解

    這篇文章主要介紹了Java訪問權(quán)限,結(jié)合實(shí)例形式詳細(xì)分析了java構(gòu)造者思想、包、訪問修飾符等相關(guān)原理、應(yīng)用與操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • Java?Agent?(代理)探針技術(shù)詳情

    Java?Agent?(代理)探針技術(shù)詳情

    這篇文章主要介紹了Java?Agent?探針技術(shù)詳情,Java?中的?Agent?技術(shù)可以讓我們無侵入性的去進(jìn)行代理,最常用于程序調(diào)試、熱部署、性能診斷分析等場景,下文更多相關(guān)資料,感興趣的小伙伴可以參考一下
    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接口是兩種重要的數(shù)據(jù)結(jié)構(gòu),它們用于存儲和管理元素序列,這篇文章主要介紹了Java中Queue以及Deque用法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-08-08
  • springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能

    springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能

    這篇文章主要介紹了springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能,本文通過示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07

最新評論

花莲县| 祁连县| 托克逊县| 龙南县| 沙田区| 平顶山市| 墨脱县| 黑龙江省| 天柱县| 吉隆县| 深泽县| 盐城市| 漳平市| 高州市| 宣城市| 英山县| 苏尼特左旗| 平塘县| 隆回县| 偏关县| 怀安县| 高台县| 徐水县| 英吉沙县| 北流市| 肥城市| 二连浩特市| 连州市| 龙江县| 六枝特区| 温州市| 武乡县| 黔江区| 古田县| 越西县| 公主岭市| 库车县| 淳安县| 焦作市| 丹寨县| 武山县|