File Channel reads/adds wrong data

2019-02-24 19:51发布

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
}

1条回答
在下西门庆
2楼-- · 2019-02-24 20:09
  1. Using a DatagramChannel this way is really never going to work. You're just sending chunks of the file which may or may not arrive, or arrive twice, or more, in any order. Use TCP.

  2. Even if it does magically work, and I have my suspicions that there are further bugs in the 'omitted code', or the receiving code:

    while (fChannel.read(bBuffer) > 0) {
    
        bBuffer.flip();
        datagramChannel.write(bBuffer);
        bBuffer.clear();
    
        //omitted code
    }
    

    The correct version of the copy loop between channels in Java is as follows:

    while (fChannel.read(buffer) > 0 || buffer.position() > 0) {    
        buffer.flip();
        datagramChannel.write(bBuffer);
        buffer.compact();
    }
    

    Note that you must continue writing while there is still anything in the buffer (buffer.position() > 0), and that you must compact() rather than clear() so as not to assume that the write() emptied the buffer.

  3. If it wasn't a DatagramChannel you should use a buffer much larger than 512, for example 8192.

查看更多
登录 后发表回答