title: OutputStream OutOfMemoryError when sending HTTP
body: <p>I am trying to publish a large video/image file from the local file system to an http path, but I run into an out of memory error after some time...</p>
  
  <p>here is the code</p>
  
  <pre><code>public boolean publishFile(URI publishTo, String localPath) throws Exception {
      InputStream istream = null;
      OutputStream ostream = null;
      boolean isPublishSuccess = false;
  
      URL url = makeURL(publishTo.getHost(), this.port, publishTo.getPath());
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  
  
      if (conn != null) {
  
          try {
  
              conn.setDoOutput(true);
              conn.setDoInput(true);
              conn.setRequestMethod("PUT");
              istream = new FileInputStream(localPath);
              ostream = conn.getOutputStream();
  
              int n;
              byte[] buf = new byte[4096];
              while ((n = istream.read(buf, 0, buf.length)) &gt; 0) {
                  ostream.write(buf, 0, n); //&lt;--- ERROR happens on this line.......???
              }
  
              int rc = conn.getResponseCode();
  
              if (rc == 201) {
                  isPublishSuccess = true;
              }
  
          } catch (Exception ex) {
              log.error(ex);
          } finally {
              if (ostream != null) {
                  ostream.close();
              }
  
              if (istream != null) {
                  istream.close();
              }
          }
      }
  
      return isPublishSuccess;
  
  }
  </code></pre>
  
  <p>HEre is the error i am getting...</p>
  
  <pre><code>Exception in thread "Thread-8773" java.lang.OutOfMemoryError: Java heap space
      at java.util.Arrays.copyOf(Arrays.java:2786)
      at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
      at sun.net.www.http.PosterOutputStream.write(PosterOutputStream.java:61)
      at com.test.HTTPClient.publishFile(HTTPClient.java:110)
      at com.test.HttpFileTransport.put(HttpFileTransport.java:97)
  </code></pre>