Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just write in blocks instead of copying it entirely into Java's memory first. The below basic example writes it in blocks of 10KB. This way you end up with a consistent memory usage of only 10KB instead of the complete content length. Also the enduser will start getting parts of the content much sooner.</p> <pre><code>response.setContentLength(getContentLength()); byte[] buffer = new byte[10240]; try ( InputStream input = getInputStream(); OutputStream output = response.getOutputStream(); ) { for (int length = 0; (length = input.read(buffer)) &gt; 0;) { output.write(buffer, 0, length); } } </code></pre> <hr> <p>As creme de la creme with regard to performance, you could use NIO <a href="http://docs.oracle.com/javase/8/docs/api/java/nio/channels/Channels.html" rel="noreferrer"><code>Channels</code></a> and a directly allocated <a href="http://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html" rel="noreferrer"><code>ByteBuffer</code></a>. Create the following utility/helper method in some custom utility class, e.g. <code>Utils</code>:</p> <pre><code>public static long stream(InputStream input, OutputStream output) throws IOException { try ( ReadableByteChannel inputChannel = Channels.newChannel(input); WritableByteChannel outputChannel = Channels.newChannel(output); ) { ByteBuffer buffer = ByteBuffer.allocateDirect(10240); long size = 0; while (inputChannel.read(buffer) != -1) { buffer.flip(); size += outputChannel.write(buffer); buffer.clear(); } return size; } } </code></pre> <p>Which you then use as below:</p> <pre><code>response.setContentLength(getContentLength()); Utils.stream(getInputStream(), response.getOutputStream()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload