Getting IOException Push back buffer is full while trying to unzip a String value using ZipInputStream
Asked Answered
A

1

6

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)
Auten answered 13/4, 2010 at 9:17 Comment(1)
You are catching exceptions and then proceeding as though they hadn't happened. Don't write code like this. Code that depends on the success of code in a prior trry block should be inside that try block.Corner
U
0

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.

Understandable answered 16/12, 2020 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.