Flip a CharBuffer : CharBuffer « File « Java Tutorial






import java.nio.CharBuffer;

public class MainClass {
  public static void main(String[] argv) throws Exception {
    char[] chars = new char[60];

    CharBuffer cb = CharBuffer.wrap(chars, 12, 42);

    println(cb);

    cb.put("This is a test String");

    cb.flip();

    println(cb);

    cb.clear();

    cb.put("Foobar");

    println(cb);

    for (int i = 0; i < 20; i++) {
      System.out.println("[" + i + "] = " + chars[i]);
    }
  }

  private static void println(CharBuffer cb) {
    System.out.println("pos=" + cb.position() + ", limit=" + cb.limit() + ", capacity="
        + cb.capacity() + ", arrayOffset=" + cb.arrayOffset());
  }
}
/*
*/
pos=12, limit=54, capacity=60, arrayOffset=0
pos=0, limit=33, capacity=60, arrayOffset=0
pos=6, limit=60, capacity=60, arrayOffset=0
[0] = F
[1] = o
[2] = o
[3] = b
[4] = a
[5] = r
...








11.43.CharBuffer
11.43.1.Buffer Equality
11.43.2.Use CharBuffer
11.43.3.Convert ByteBuffer to a CharBuffer
11.43.4.Use while loop to read a CharBuffer
11.43.5.Save and read text using FileChannel with CharBuffer
11.43.6.Test the effects of buffer flipping
11.43.7.Test buffer duplication
11.43.8.Create a CharBuffer and put in some string
11.43.9.Buffer slice
11.43.10.Wrap a char array to a CharBuffer
11.43.11.Flip a CharBuffer
11.43.12.Get array out of CharBuffer
11.43.13.Convert ByteBuffer to CharBuffer
11.43.14.Fill String to CharBuffer
11.43.15.CharBuffer warps a ByteBuffer