简体   繁体   中英

(Java NIO) Should I check both bytes read and buffer position in copying between channels?

This code is similar to example code in the JavaDoc of ByteBuffer.compact()

package com.study.nio;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class BufferCopy {

    public static void main(String[] args) {
        var from = "Hello World".getBytes();

        var buf = ByteBuffer.allocate(5);
        buf.clear();
        try (var outputStream = new ByteArrayOutputStream();
             var inChannel = Channels.newChannel(new ByteArrayInputStream(from));
             var outChannel = Channels.newChannel(outputStream);) {
            while (inChannel.read(buf) >= 0 || buf.position() != 0) {
                buf.flip();
                outChannel.write(buf);
                buf.compact();
            }
            System.out.println(outputStream.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

My question is do we need the "|| buf.position() !=0" in the while condition check?

Just for reference, the code example provided in the documentation of Buffer.compact() is this:

   buf.clear();          // Prepare buffer for use
   while (in.read(buf) >= 0 || buf.position != 0) {
       buf.flip();
       out.write(buf);
       buf.compact();    // In case of partial write
   }

Scenario 1: Condition is Finally Met

  • Now, picture the moment when the channel in reaches its EOF and it adds its final 100 bytes to the buffer buff .
  • The code enters the while loop because in.read(buff) >= 0 , the second condition is not even evaluated.
  • Now while writing out.write(buff) only 50 of the 100 bytes in the buffer are sent (perhaps the output channel has a very low bandwidth or its buffer is too small).
  • The buffer is compacted (pos=50,limit=100,cap=100).
  • Now, the while condition is evaluated again, this time in.read(buff) >= 0 is false because in channel is at its EOF, so it evaluates buf.position != 0 and we discover that there are still bytes in the buffer.
  • So, it goes back into the while loop and writes a few more bytes into the output channel.

Scenario 2: Slow Output Building up

Another way to look at how this condition gradually builds up is by picturing a scenario when the output channel is slower than the input channel, eg low bandwidth and/or a smaller output buffer.

  • The code enters the while loop because in.read(buff) >= 0 , the second condition is not even evaluated. It reads 100 bytes.
  • Now while writing out.write(buff) only 50 of the 100 bytes in the buffer are sent.
  • The buffer is compacted (pos=50,limit=100,cap=100).
  • Now, the while condition is evaluated again, and this time in.read(buff) >= 0 adds 50 more bytes into the buffer and we are once again at 100 bytes total.
  • The while repeats in this fashion until the scenario 1 is reached once more buf.position != 0 is finally evaluated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM