java.io.EOFException: Unexpected end of ZLIB input stream異常解決
因需要完成壓縮與解壓縮功能,所以使用到j(luò)ava.util.zip中的類。同時(shí)使用了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完成自動(dòng)close,結(jié)果在解壓縮獲取到的壓縮數(shù)據(jù)時(shí)出現(xiàn)異常。隨后看了GZIPOutputStream 的源碼,原來(lái)調(diào)用close的時(shí)候會(huì)填充一些壓縮信息,這樣才能在解壓縮時(shí)正常解壓縮。而上面的代碼,bos.toByteArray();是在bos.close()調(diào)用前被調(diào)用,因此返回了不正確的字節(jié)數(shù)組,造成解壓縮失敗。
下面是執(zhí)行try-with-resource執(zhí)行順序的一個(gè)測(cè)試。
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í)行。
最后保證在對(duì)于解壓縮,保證在壓縮返回字節(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能對(duì)比
本文主要介紹了Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能對(duì)比,分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java中ArrayList和LinkedList之間的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java中ArrayList和LinkedList之間的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05mybatis?一對(duì)多映射?column屬性的注意事項(xiàng)說(shuō)明
這篇文章主要介紹了mybatis?一對(duì)多映射?column屬性的注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01鑒權(quán)認(rèn)證+aop+注解+過(guò)濾feign請(qǐng)求的實(shí)例
這篇文章主要介紹了鑒權(quán)認(rèn)證+aop+注解+過(guò)濾feign請(qǐng)求的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03springmvc字符編碼過(guò)濾器CharacterEncodingFilter的使用
這篇文章主要介紹了springmvc字符編碼過(guò)濾器CharacterEncodingFilter的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-08-08startActivityForResult和setResult案例詳解
這篇文章主要介紹了startActivityForResult和setResult案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Java中基于DeferredResult的異步服務(wù)詳解
這篇文章主要介紹了Java中基于DeferredResult的異步服務(wù)詳解,DeferredResult字面意思是"延遲結(jié)果",它允許Spring MVC收到請(qǐng)求后,立即釋放(歸還)容器線程,以便容器可以接收更多的外部請(qǐng)求,提升吞吐量,需要的朋友可以參考下2023-12-12