尝试使用 ZipInputStream 解压缩字符串值时出现 IOException 推回缓冲区已满

发布于 2024-08-28 15:27:08 字数 5394 浏览 5 评论 0原文

我正在尝试解压缩字符串值。
但我收到了 java.io.IOException: Push back buffer is full:

public byte[] unzipArray(String stringToUnzip) {
            byte[] inputByteArray = Base64.decode(stringToUnzip);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        inputByteArray);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream);

            try {
                  ZipEntry theEntry = zipInputStream.getNextEntry();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            byte[] buffer = new byte[2048];
            int size = 2048;

            while (true) {
                  try {
                        size = zipInputStream.read(buffer, 0, buffer.length);
                  } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
                  if (size > 0) {
                        byteArrayOutputStream.write(buffer, 0, size);
                  } else {
                        break;
                  }
            }
            return byteArrayOutputStream.toByteArray();
      }

异常堆栈跟踪:

java.io.IOException: Push back buffer is full
                at java.io.PushbackInputStream.unread(Unknown Source)
                at java.util.zip.ZipInputStream.readEnd(Unknown Source)
                at java.util.zip.ZipInputStream.read(Unknown Source)
                at com.ZipHelper.unzipArray(ZipHelper.java:55)
                ......
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                at java.lang.reflect.Method.invoke(Unknown Source)
                at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:168)
                at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
                at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
                at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
                at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
                at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
                at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
                at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
                at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
                at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
                at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
                at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
                at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
                at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:530)
                at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:427)
                at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
                at org.eclipse.jetty.server.session.SessionHandler.handle(SessionHandler.java:182)
                at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:933)
                at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:362)
                at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:867)
                at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
                at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:113)
                at org.eclipse.jetty.server.Server.handle(Server.java:334)
                at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:559)
                at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1007)
                at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:747)
                at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:209)
                at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:406)
                at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:462)
                at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:436)
                at java.lang.Thread.run(Unknown Source)

I am trying to unzip a String value.
But I am getting a java.io.IOException: Push back buffer is full:

public byte[] unzipArray(String stringToUnzip) {
            byte[] inputByteArray = Base64.decode(stringToUnzip);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        inputByteArray);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream);

            try {
                  ZipEntry theEntry = zipInputStream.getNextEntry();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            byte[] buffer = new byte[2048];
            int size = 2048;

            while (true) {
                  try {
                        size = zipInputStream.read(buffer, 0, buffer.length);
                  } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
                  if (size > 0) {
                        byteArrayOutputStream.write(buffer, 0, size);
                  } else {
                        break;
                  }
            }
            return byteArrayOutputStream.toByteArray();
      }

Exception stack trace:

java.io.IOException: Push back buffer is full
                at java.io.PushbackInputStream.unread(Unknown Source)
                at java.util.zip.ZipInputStream.readEnd(Unknown Source)
                at java.util.zip.ZipInputStream.read(Unknown Source)
                at com.ZipHelper.unzipArray(ZipHelper.java:55)
                ......
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                at java.lang.reflect.Method.invoke(Unknown Source)
                at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:168)
                at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
                at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
                at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
                at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
                at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
                at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
                at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
                at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
                at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
                at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
                at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
                at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
                at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:530)
                at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:427)
                at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
                at org.eclipse.jetty.server.session.SessionHandler.handle(SessionHandler.java:182)
                at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:933)
                at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:362)
                at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:867)
                at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
                at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:113)
                at org.eclipse.jetty.server.Server.handle(Server.java:334)
                at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:559)
                at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1007)
                at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:747)
                at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:209)
                at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:406)
                at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:462)
                at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:436)
                at java.lang.Thread.run(Unknown Source)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-09-04 15:27:08

没有足够的证据来确定异常的真正原因,但我可以在您的代码中看到一个重大问题:

您这样做是为了获取 ZIP 文件的第一个条目:

  ZipEntry theEntry = zipInputStream.getNextEntry();

但您没有测试是否 theEntrynull。如果为 null,则表示 ZIP 文件没有条目。

然后您继续使用read。指定从“当前条目”读取,但 javadoc 没有说明如果没有当前条目会发生什么。

There is not sufficient evidence to be sure of the real cause of the exception, but I can see a significant problem in your code:

You do this to get the first entry of the ZIP file:

  ZipEntry theEntry = zipInputStream.getNextEntry();

but you do not test to see if theEntry is null. If it is null, that means that the ZIP file has no entries.

You then proceed to use read. That is specified to read from "the current entry", but the javadoc doesn't say what happens if there is no current entry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文