Fastest way to copy a file over socket
<p>Which would be the fastest way to copy a file over a socket? I have tried several ways but I am not convinced I found the fastest way concerning transfer and CPU usage. (Best result: 175mBit/s (SSD/GBit Network))</p>

<p>Server:</p>

<pre><code>ByteBuffer bb = ByteBuffer.allocate(packet_size);
DataOutputStream data_out = new DataOutputStream(socket.getOutputStream());

while(working){

    int count =0;
    int packet_size = in.readInt();
    long pos = in.readLong();
    if(filechannel.position()!=requested_pos){
        filechannel.position(requested_pos);
    }
    bb.limit(packet_size);
    bb.position(0);
    if((count=filechannel.read(bb))&gt;0){  //FileInputStream.getChannel()
        data_out.writeInt(count);
        data_out.write(bb.array(),0,count);
    }else{
     working=false;
    }
}
</code></pre>

<p>Client:</p>

<pre><code>for(long i=0;i&lt;=steps;i++){

    data_out.writeInt(packet_size);     //requested packet size
    data_out.writeLong(i*packet_size);  //requested file position

    count=in.readInt();
    bb.clear();
    bb.limit(count);

    lastRead=0;
    while(lastRead&lt;count){
        lastRead+=in.read(bytes,lastRead,count-lastRead);  
    }
    bb.put(bytes,0,count);
    bb.position(0);
    filechannel.write(bb); // filechannel over RandomAccessFile
 }
</code></pre>

<p>any suggestions?</p>