首页 > 代码库 > 演示缓冲区的操作流程
演示缓冲区的操作流程
import java.nio.IntBuffer;
public class IntBufferDemo01 {
public static void main(String[] args) {
IntBuffer buf = IntBuffer.allocate(10);
System.out.print("The position,limit and capacity before input data:");
System.out.println("position=" + buf.position() + ",limit="
+ buf.limit() + ",capacity=" + buf.capacity());
int temp[] = { 3, 4, 5 };
buf.put(3);
buf.put(temp);
System.out.print("The position,limit and capacity after input data:");
System.out.println("position=" + buf.position() + ",limit="
+ buf.limit() + ",capacity=" + buf.capacity());
buf.flip();
System.out.print("The position,limit and capacity when outputing:");
System.out.println("positon=" + buf.position() + ",limt=" + buf.limit()
+ ",capacity=" + buf.capacity());
System.out.print("The content in buffer:");
while (buf.hasRemaining()) {
int x = buf.get();
System.out.print(x + ",");
}
}
}
The output:
The position,limit and capacity before input data:position=0,limit=10,capacity=10
The position,limit and capacity after input data:position=4,limit=10,capacity=10
The position,limit and capacity when outputing:positon=0,limt=4,capacity=10
The content in buffer:3,3,4,5,
演示缓冲区的操作流程