File Channel reads/adds wrong data

匆匆过客 提交于 2019-12-20 01:39:24

问题


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:


  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.



来源:https://stackoverflow.com/questions/26981232/file-channel-reads-adds-wrong-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!