title: ZipOutputStream.closeEntry() hangs (java/jersey)
body: <p>I'm trying to post an InputStream to a RESTful service. For normal files, this is fine. </p>
  
  <p>In another place I'm trying to write a number of files to a piped zip stream on the fly. To do this I have a class that extends <code>InputStream</code> and when <code>read()</code> is called it writes the next file to the pipe. When the first file has been written I call <code>ZipOutputStream.closeEntry()</code> but it hangs. Why??</p>
  
  <p>When I test this class in a unit test it works fine. When I try to post this object it hangs. The debugger is telling me its waiting for lock on <code>SocketWrapper</code>.</p>
  
  <p>Note, I also tried setting media type to <code>application/octet-stream</code>. Also, the RESTful method is never called.</p>
  
  <p>The stream class...</p>
  
  <pre><code>static class MultiStreamZipInputStream extends InputStream {
  
      private final Iterator&lt;InputStream&gt; streams;
      private final byte[] buffer = new byte[4096];
      private InputStream inputStream;
      private ZipOutputStream zipOutputStream;
      private InputStream currentStream;
      private int counter = 0;
  
      public MultiStreamZipInputStream(List&lt;InputStream&gt; streamList) {
  
          streams = streamList.iterator();
          currentStream = streams.next();
  
          try {
              PipedOutputStream out = new PipedOutputStream();
              inputStream = new PipedInputStream(out);
  
              zipOutputStream = new ZipOutputStream(out);
              ZipEntry entry = new ZipEntry(String.valueOf(counter++)); // Use counter for random name
              zipOutputStream.putNextEntry(entry);
  
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  
      @Override
      public int read()
          throws IOException {
  
          if (inputStream.available() != 0)
              return inputStream.read();
  
          if (currentStream == null)
              return -1;
  
          int bytesRead = currentStream.read(buffer);
          if (bytesRead &gt;= 0) {
  
              zipOutputStream.write(buffer,  0,  bytesRead);
              zipOutputStream.flush();
  
          } else  {
  
              currentStream.close();
              zipOutputStream.closeEntry();
  
              if (!streams.hasNext()) {
                  currentStream = null;
                  return -1;
              }
  
              currentStream = streams.next();
              zipOutputStream.putNextEntry(new ZipEntry(String.valueOf(counter++)));
          }
  
          return read();
      }
  }
  </code></pre>
  
  <p>The posting code...</p>
  
  <pre><code>        MultiStreamZipInputStream myStream = ...
  
          Client client = Client.create();
          Builder webResource = client.resource("some URL").type("application/x-zip-compressed");
          webResource.post(ClientResponse.class, myStream);
  </code></pre>
  
  <p>The REST method on the other end...</p>
  
  <pre><code>@POST
  @Path("/somemethod")
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes({"application/x-zip-compressed"})
  public Response someMethod(InputStream data) {...
  </code></pre>