The question is listed in the title. I transfer the file in parts to the pipe, it appears -> BufferOverflowException (I understand that this exception appears when the buffer overflows). It was also possible to completely transfer the file to 50 bytes (txt). Video, pictures do not pass. Exceptions appear in different ways, there is no exact limit of successfully transmitted bytes. The buffer reads by position, the position corresponds to the size of the buffer, nothing should overflow.


Addition:

Another small note. If before buffer.flip(); display positionRead -> System.out.println("positionRead " + positionRead); bytes will be written to the channel (Dynamically, as mentioned above with the appearance of an exception). But if the output line is removed - immediately without successfully transferred bytes, I get a BufferOverflowException . I do not know the reason, but perhaps this information will be useful.

For a better understanding of the picture made 4 screen.

Using System.out.println - https://ibb.co/geAL4S transmitted 30kb from 327kb. - https://ibb.co/h36yb7

Without using System.out.println - https://ibb.co/mwJ7jS, nothing was transmitted - https://ibb.co/mXON3n

  • Give the code where you write to the socket, please. - Alexander Martyntsev
  • ByteBuffer buffer = ByteBuffer.allocate(1024); fileChannel.read(buffer, positionRead); buffer.flip(); socketChannel.write(buffer); More info -> stackoverflow.com/questions/50112763/transfer-a-file - Akalit
  • add after the entry buffer.position (0); - Alexander Martyntsev
  • Hello, he added, the result is the same - Akalit

1 answer 1

Perhaps the fact is that you constantly reduce the limit of the buffer without returning it to the capacity of the buffer. You probably know that flip() puts a limit on the current position, and the current position on the 0, so that only the data written to the buffer is written to the channel. But after recording you do not return the limit on the capacity of the buffer, return only the position. So you compress the working part of the buffer constantly with each call of this method, without suspecting it.

I advise you to replace buffer.position(0); at the end on buffer.clear(); This method assigns the buffer position to 0, and also assigns its limit to the buffer (capacity).

  • Replaced, the file is not fully transmitted, now it appears -> IllegalArgumentException: newPosition < 0: (10256 < 0) или IllegalArgumentException: newPosition> limit: (10256> 10256) `and sometimes HeapByteBuffer - Akalit
  • @Akalit so you specify position yourself, give the code what readOperation is, for example, so that I can understand how you get the wrong position - Maxgmer
  • All the code you can see by clicking on this link -> stackoverflow.com/questions/50112763/transfer-a-file - Akalit
  • @Akalit at first glance at this code, did not notice you checking for return -1 from the Future (when the operation is completed, returns -1), this may result in reading the same place several times (maybe you can do it, here already I do not remember, for a long time with net java for netcode did not work). I'll come home, I’ll look, you can still try stopping the method if -1. - Maxgmer
  • Closing the operation when returning -1? Most likely you are not looking there. The problem part of the code is in the condition -> if (key.isWritable()) the max file size is taken there and the Future object returns the value, and when it reaches 1 byte larger than the file size, it exits the cycle - Akalit