I need to transfer ~ 100 MB of data through ServerSocket using NIO, but I canβt figure out how to do this without interruption anywhere / saving the transfer status.
My first idea was to send the file size, apparently I can not send the size of these large files, because it does not even fit into RAM right away. Then I thought, why not just transmit until nothing is received, but this is when a problem arises.
Even if I write server data all the time
FileChannel fc = new FileInputStream(f).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while(fc.read(buffer) > 0) { buffer.flip(); while(channel.write(buffer) > 0); buffer.clear(); }
but due to interruptions in file transfer for a while, constantly reading data and breaking when nothing is available, was a bad idea.
I canβt understand how I can tell the client if there is all the data available without having to send each piece of data in the form of a new packet with an operation code, etc., or is it even possible?
I am also wondering if the best way to send the entire buffer is below.
while(channel.write(buffer) > 0);
source share