title: S3 Java client fails a lot with "Premature end of Content-Length delimited message body" or "java.net.SocketException Socket closed"
body: <p>I have an application that does a lot work on S3, mostly downloading files from it. I am seeing a lot of these kind of errors and I'd like to know if this is something on my code or if the service is really unreliable like this.</p>
  
  <p>The code I'm using to read from the S3 object stream is as follows:</p>
  
  <pre><code>public static final void write(InputStream stream, OutputStream output) {
  
    byte[] buffer = new byte[1024];
  
    int read = -1;
  
    try {
  
      while ((read = stream.read(buffer)) != -1) {
        output.write(buffer, 0, read);
      }
  
      stream.close();
      output.flush();
      output.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  
  }
  </code></pre>
  
  <p>This <strong>OutputStream</strong> is a <strong>new BufferedOutputStream( new FileOutputStream( file ) )</strong>. I am using the latest version of the Amazon S3 Java client and this call is retried <strong>four</strong> times before giving up. So, after trying this for 4 times it still fails.</p>
  
  <p>Any hints or tips on how I could possibly improve this are appreciated.</p>