Java - transferring large files through channels - NIO

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); 
+6
source share
3 answers

The correct way to copy channels through buffers is as follows:

 while (in.read(buffer) >= 0 || buffer.position() > 0) { buffer.flip(); out.write(buffer); buffer.compact(); } 

This applies to all angular cases, including the length of the read! = The length of the record and the data remaining at the end of the input.

NB, which is intended for blocking mode. If you are in non-blocking mode, you should return to the select() loop if read () or write () returns zero.

+3
source

Perhaps you are looking for this:

 channel.transferFrom(0, fc.size(), fc); 
+4
source

If you cannot depend on the socket remaining open, there is no way in your protocol to create a recovery and continuation mechanism. As for telling the client how many bytes to expect, you can find the size of the file without reading it into memory using File.length .

0
source

Source: https://habr.com/ru/post/916213/


All Articles