SharedBuffer.java

import java.nio.*;
class SharedBuffer {
  public static void main(String[] args) {
    ByteBuffer bb1 = ByteBuffer.allocate(10);
    ByteBuffer bb2 = bb1.duplicate();
    for (int i=0; i<bb1.capacity(); i++){
      bb1.put((byte)i);
    }
    System.out.println(bb1.position()); // affiche 10
    System.out.println(bb2.position()); // affiche 0
    bb2.put((byte)3);                   // place la position en 1
    System.out.println(bb1.get(0));     // affiche 3
    System.out.println(bb1.position()); // affiche 10
    ByteBuffer bb3 = bb2.asReadOnlyBuffer();
    System.out.println(bb3.position()); // affiche 1
    bb3.rewind();                       // place la position en 0
    System.out.println(bb3.get());      // affiche 3
    bb3.put((byte)4); // throws java.nio.ReadOnlyBufferException
  }
}