I am using a filechannel with a byte buffer to send packets over the network. My problem is that when the filechannel reads the last few bytes it appends the last bit of data from previous bytes read even though I am clearing the byte buffer after I write.
For example,
Byte Buffer size = 512 For the last iteration, the remaining bytes to send is 372. It reads the last 372 but it also appends another 140 bytes (512-372) to the end of it, and appears that last 140 bytes is from the previous 512 bytes sent.
Heres my code:
ByteBuffer bBuffer = ByteBuffer.allocate(512);
while (fChannel.read(bBuffer) > 0) {
bBuffer.flip();
datagramChannel.write(bBuffer);
bBuffer.clear();
//omitted code
}