Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A couple of observations:</p> <ul> <li><p>The one byte read at the start of the loop does nothing to improve performance. Probably the reverse in fact.</p></li> <li><p>The call to <code>inputStream.available()</code> is unnecessary. You should just try to read to "buffer size" characters. A <code>read</code> on a Socket streamwill return as many characters as are currently available, but won't block until the buffer is full. (I cannot find anything in the javadocs that says this, but I'm sure it is the case. A lot of things would perform poorly ... or break ... if <code>read</code> blocked until the buffer was full.)</p></li> <li><p>As @user479257 points out, you should get better throughput by using java.nio and reading and writing ByteBuffers. This will cut down on the amount of data copying that occurs in the JVM.</p></li> <li><p>Your method will leak Socket Streams if a read, write or close operation throws an exception. You should use a <code>try ... finally</code> as follows to ensure that the streams are always closed no matter what happens.</p></li> </ul> <hr> <pre><code>public static void route(InputStream inputStream, OutputStream outputStream) throws IOException { byte[] buffer = new byte[65536]; try { while( true ) { ... b = inputStream.read(...); if( b == - 1 ) { log.info("No data available anymore. Closing stream."); return; } outputStream.write(buffer, 0, b+1); } } finally { try { inputStream.close();} catch (IOException ex) { /* ignore */ } try { outputStream.close();} catch (IOException ex) { /* ignore */ } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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