ByteBuffer: position(int newPosition) : ByteBuffer « java.nio « Java by API






ByteBuffer: position(int newPosition)

 

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;

/**
 * Test asCharBuffer view.
 */
public class Main {
  public static void main(String[] argv) throws Exception {
    ByteBuffer byteBuffer = ByteBuffer.allocate(7).order(ByteOrder.BIG_ENDIAN);
    CharBuffer charBuffer = byteBuffer.asCharBuffer();

    byteBuffer.put(0, (byte) 0);
    byteBuffer.put(1, (byte) 'H');
    byteBuffer.put(2, (byte) 0);
    byteBuffer.put(3, (byte) 'i');
    byteBuffer.put(4, (byte) 0);
    byteBuffer.put(5, (byte) '!');
    byteBuffer.put(6, (byte) 0);

    println(byteBuffer);
    println(charBuffer);

    // now slice it differently
    byteBuffer.position(4);
    charBuffer = byteBuffer.asCharBuffer();

    println(byteBuffer);
    println(charBuffer);
  }

  // Print info about a buffer
  private static void println(Buffer buffer) {
    System.out.println("pos=" + buffer.position() + ", limit=" + buffer.limit() + ", capacity="
        + buffer.capacity() + ": '" + buffer.toString() + "'");
  }
}

   
  








Related examples in the same category

1.ByteBuffer: allocate(int capacity)
2.ByteBuffer: allocateDirect(int size)
3.ByteBuffer: asCharBuffer()
4.ByteBuffer: asDoubleBuffer()
5.ByteBuffer: asFloatBuffer()
6.ByteBuffer: asLongBuffer()
7.ByteBuffer: asShortBuffer()
8.ByteBuffer: capacity()
9.ByteBuffer: clear()
10.ByteBuffer: compact()
11.ByteBuffer: flip()
12.ByteBuffer: get()
13.ByteBuffer: get(byte[] dst, int offset, int length)
14.ByteBuffer: get(int index)
15.ByteBuffer: getChar()
16.ByteBuffer: getDouble()
17.ByteBuffer: getFloat()
18.ByteBuffer: getInt()
19.ByteBuffer: getLong()
20.ByteBuffer: isDirect()
21.ByteBuffer: limit()
22.ByteBuffer: order()
23.ByteBuffer: order(ByteOrder bo)
24.ByteBuffer: position()
25.ByteBuffer: put(byte b)
26.ByteBuffer: putChar(char ch)
27.ByteBuffer: putDouble(double value)
28.ByteBuffer: putFloat(float value)
29.ByteBuffer: putInt(int i)
30.ByteBuffer: putLong(long value)
31.ByteBuffer: putShort(short value)
32.ByteBuffer: putShort(int index, short value)
33.ByteBuffer: rewind()
34.ByteBuffer: wrap(byte[] array)