Manipulating ints in a ByteBuffer with an IntBuffer : IntBuffer « File « Java Tutorial






import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class MainClass {

  private static final int BSIZE = 1024;

  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    IntBuffer ib = bb.asIntBuffer();
 
    ib.put(new int[] { 1, 2, 7, 9, 3, 8, 6 });
 
    System.out.println(ib.get(3));
    ib.put(3, 1811);
    ib.rewind();
    while (ib.hasRemaining()) {
      int i = ib.get();
      if (i == 0)
        break; // Else we'll get the entire buffer
      System.out.println(i);
    }
  }
}
/*
*/
9
1
2
7
1811
3
8
6








11.46.IntBuffer
11.46.1.Convert ByteBuffer to an IntBuffer
11.46.2.Use while loop to read an IntBuffer
11.46.3.Put integers to a mapped IntBuffer
11.46.4.Map FileChannel to an IntBuffer and read from the IntBuffer
11.46.5.Manipulating ints in a ByteBuffer with an IntBuffer