learning java NIO 之 CharBuffer
import java.nio.CharBuffer; public class BufferTest { public static void main(String[] args) { CharBuffer buff = CharBuffer.allocate(8); System.out.println("capacity: " + buff.capacity()); System.out.println("limit: " + buff.limit());; System.out.println("position:" + buff.position()); buff.put('a'); buff.put('b'); buff.put('c'); System.out.println("position:" + buff.position()); buff.flip(); System.out.println("limit: " + buff.limit());; System.out.println("position:" + buff.position()); System.out.println(buff.get()); System.out.println(buff.position()); buff.clear(); System.out.println("limit: " + buff.limit());; System.out.println("position:" + buff.position()); System.out.println(buff.get(2)); System.out.println("position:" + buff.position()); } }
output:
capacity: 8 limit: 8 position:0 position:3 limit: 3 position:0 a 1 limit: 8 position:0 c position:0