[java][nio]IntBuffer


import java.nio.ByteBuffer;
import java.nio.IntBuffer;
/**
 * 
 * 用IntBuffer操作ByteBuffer中的int类型数据.
 *
 */
public class IntBufferDemo {

	private static final int BSIZE = 1024;
	
	public static void main(String[] args) {
		ByteBuffer bb = ByteBuffer.allocate(BSIZE);
		
		IntBuffer ib = bb.asIntBuffer();
		//存入一个int array
		ib.put(new int[]{5,8555,99,78,0,25});
		//绝对定位读写
		System.out.println(ib.get(2));//99
		ib.put(2,55);//修改index为2的数据值
		System.out.println(ib.get(2));//55
		
		System.out.println(bb.position());//0
		System.out.println(ib.position());//6
		
		ib.flip();
		System.out.println(ib.position());//0
		System.out.println(ib.limit());//6
		System.out.println(bb.limit());//1024
		System.out.println();
		//hasRemaining:在current position与limit之间有任何元素
		while(ib.hasRemaining()){
			int i = ib.get();
			System.out.println(i);
		}
		
	}

}


你可能感兴趣的:(java)