Java stream file to socket as fast as possible
<p>I have a file of bytes <code>mybytes.dat</code>, and I'm writing a TCP Server/Client, where the server sends the contents of <code>mybytes.dat</code> to the client over a socket.</p>

<p>If <code>mybytes.dat</code> is read completely into memory, then the application can send data at about 160MB/s on my local network stack.  However, now I'm trying to stream datafiles that are > 1GB, and shouldn't all be read into memory.</p>

<p>This <a href="https://stackoverflow.com/questions/9588348/java-read-file-by-chunks">related solution for sending a file in chunks</a> seems appropriate; however, would it be more efficient to read large chunks of the file into memory (ie. maybe 1MB at a time in a buffer) and then write these as smaller chunks (32kb) to a socket?  If this is reasonable, how can one use a BufferedFileReader to read large chunks, and then write smaller chunks to an OutputStream?  To get started, let me declare at least some variables:</p>

<pre><code>BufferedInputStream blobReader = new BufferedInputStream(newInputStream("mybytes.dat"), 1024*1024);
OutputStream socketWriter = socket.getOutputStream();
</code></pre>

<p>What is the correct way to connect my blobReader to the socketWriter, such that I always maintain enough bytes in memory to ensure the application is not limited by disk reads?  Or am I completely offbase with this line of reasoning?</p>