首页javanio_bufferJava I/O - 如何测试缓冲区翻转的效果

Java I/O - 如何测试缓冲区翻转的效果

我们想知道如何测试缓冲区翻转的效果。
import java.nio.CharBuffer;

public class Main {
  public static void main(String[] argv) throws Exception {
    CharBuffer cb = CharBuffer.allocate(15);

    cb.put("Hello World");
    println(cb);

    cb.flip();
    println(cb);

    cb.flip();
    println(cb);
  }

  private static void println(CharBuffer cb) {
    System.out.println("pos=" + cb.position() + ", limit=" + cb.limit()
        + ": '" + cb + "'");
  }
}