Java : ShortBuffer with Examples

ShortBuffer (Java SE 20 & JDK 20) API Examples.
You will find code examples on most ShortBuffer methods.


Summary

A short buffer.

Class diagram

final var buffer = ShortBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0

buffer.put((short) 10);
System.out.println(buffer.position()); // 1

buffer.put((short) 20);
System.out.println(buffer.position()); // 2

buffer.put((short) 30);
System.out.println(buffer.position()); // 3

if (buffer.hasArray()) {
    final var ret = buffer.array();
    System.out.println(Arrays.toString(ret)); // [10, 20, 30, 0, 0]
}
final short[] array = {10, 20, 30};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 3
System.out.println(buffer.limit()); // 3
System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1

System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2

System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3

Methods

static ShortBuffer allocate (int capacity)

Allocates a new short buffer.

final var buffer = ShortBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0, 0, 0, 0, 0]
}

final short[] array ()

Returns the short array that backs this buffer (optional operation).

final var buffer = ShortBuffer.allocate(5)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30);

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}

final int arrayOffset ()

Returns the offset within this buffer's backing array of the first element of the buffer (optional operation).

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

final var sliced = buffer.slice(2, 3);

System.out.println(buffer); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0
    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
}

System.out.println(sliced); // java.nio.HeapShortBuffer[pos=0 lim=3 cap=3]
if (sliced.hasArray()) {
    System.out.println(sliced.arrayOffset()); // 2
    System.out.println(sliced.get()); // 30
    System.out.println(sliced.get()); // 40
    System.out.println(sliced.get()); // 50
}

abstract ShortBuffer asReadOnlyBuffer ()

Creates a new, read-only short buffer that shares this buffer's content.

final var buffer = ShortBuffer.allocate(3);
buffer.put((short) 10);

final var readOnlyBuffer = buffer.asReadOnlyBuffer();

System.out.println(buffer); // java.nio.HeapShortBuffer[pos=1 lim=3 cap=3]
System.out.println(readOnlyBuffer); // java.nio.HeapShortBufferR[pos=1 lim=3 cap=3]

readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 10
System.out.println(readOnlyBuffer.get()); // 0
System.out.println(readOnlyBuffer.get()); // 0

buffer.put((short) 20);

readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 10
System.out.println(readOnlyBuffer.get()); // 20
System.out.println(readOnlyBuffer.get()); // 0

try {
    final var ret = readOnlyBuffer.put((short) 30);
} catch (ReadOnlyBufferException e) {
    System.out.println("ReadOnlyBufferException!");
}

// Result
// ↓
//ReadOnlyBufferException!

final ShortBuffer clear ()

Clears this buffer.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

buffer.position(2);
buffer.limit(4);

System.out.println(buffer); // java.nio.HeapShortBuffer[pos=2 lim=4 cap=5]

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false

System.out.println(buffer.clear()); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false

abstract ShortBuffer compact ()

Compacts this buffer (optional operation).

final short[] array = {10, 20, 30, 40, 50, 60};
final var buffer = ShortBuffer.wrap(array);

buffer.position(3);
System.out.println(buffer.compact()); // java.nio.HeapShortBuffer[pos=3 lim=6 cap=6]

System.out.println(Arrays.toString(array)); // [40, 50, 60, 40, 50, 60]
final short[] array = {10, 20, 30, 40, 50, 60};
final var buffer = ShortBuffer.wrap(array);

buffer.position(2);
System.out.println(buffer.compact()); // java.nio.HeapShortBuffer[pos=4 lim=6 cap=6]

System.out.println(Arrays.toString(array)); // [30, 40, 50, 60, 50, 60]
final short[] array = {10, 20, 30, 40, 50, 60};
final var buffer = ShortBuffer.wrap(array);

buffer.position(4);
System.out.println(buffer.compact()); // java.nio.HeapShortBuffer[pos=2 lim=6 cap=6]

System.out.println(Arrays.toString(array)); // [50, 60, 30, 40, 50, 60]

int compareTo (ShortBuffer that)

Compares this buffer to another.

final var buffer1 = ShortBuffer.allocate(3)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30);

final var buffer2 = ShortBuffer.allocate(3)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30);

buffer1.clear();
buffer2.clear();

System.out.println(buffer1.compareTo(buffer2)); // 0
final var buffer1 = ShortBuffer.allocate(3)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30);

final var buffer2 = ShortBuffer.allocate(3)
        .put((short) 5)
        .put((short) 50)
        .put((short) 500);

final var buffer3 = ShortBuffer.allocate(3)
        .put((short) 30)
        .put((short) 20)
        .put((short) 10);

buffer1.clear();
buffer2.clear();
buffer3.clear();

System.out.println(buffer1.compareTo(buffer2)); // 5
System.out.println(buffer1.compareTo(buffer3)); // -20

abstract ShortBuffer duplicate ()

Creates a new short buffer that shares this buffer's content.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

buffer.position(1);
buffer.limit(4);

final var duplicated = buffer.duplicate();

System.out.println(buffer); // java.nio.HeapShortBuffer[pos=1 lim=4 cap=5]
System.out.println(duplicated); // java.nio.HeapShortBuffer[pos=1 lim=4 cap=5]

System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40

System.out.println(duplicated.get()); // 20
System.out.println(duplicated.get()); // 30
System.out.println(duplicated.get()); // 40

boolean equals (Object ob)

Tells whether or not this buffer is equal to another object.

final var buffer1 = ShortBuffer.allocate(3)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30);

final var buffer2 = ShortBuffer.allocate(3)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30);

buffer1.clear();
buffer2.clear();

System.out.println(buffer1.equals(buffer2)); // true
final var buffer1 = ShortBuffer.allocate(3)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30);

final var buffer2 = ShortBuffer.allocate(3)
        .put((short) 5)
        .put((short) 50)
        .put((short) 500);

final var buffer3 = ShortBuffer.allocate(3)
        .put((short) 30)
        .put((short) 20)
        .put((short) 10);

buffer1.clear();
buffer2.clear();
buffer3.clear();

System.out.println(buffer1.equals(buffer2)); // false
System.out.println(buffer1.equals(buffer3)); // false

final ShortBuffer flip ()

Flips this buffer.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

System.out.println(buffer); // java.nio.HeapShortBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.flip()); // java.nio.HeapShortBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

abstract short get ()

Relative get method.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50

System.out.println(buffer.position()); // 5

abstract short get (int index)

Absolute get method.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

System.out.println(buffer.get(0)); // 10
System.out.println(buffer.get(1)); // 20
System.out.println(buffer.get(2)); // 30
System.out.println(buffer.get(3)); // 40
System.out.println(buffer.get(4)); // 50

System.out.println(buffer.position()); // 0

ShortBuffer get (int index, short[] dst)

Absolute bulk get method.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

{
    final var dst = new short[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(0, dst);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [10, 20, 30]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new short[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(1, dst);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [20, 30, 40]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new short[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(2, dst);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [30, 40, 50]
    System.out.println(buffer.position()); // 0
}

ShortBuffer get (int index, short[] dst, int offset, int length)

Absolute bulk get method.

final short[] array = {10, 20, 30, 40, 50, 60};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

{
    final var dst = new short[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(0, dst, 0, 3);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [10, 20, 30, 0, 0]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new short[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(2, dst, 1, 3);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [0, 30, 40, 50, 0]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new short[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(4, dst, 3, 2);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 50, 60]
    System.out.println(buffer.position()); // 0
}

ShortBuffer get (short[] dst)

Relative bulk get method.

final short[] array = {10, 20, 30, 40, 50, 60, 70};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 7
System.out.println(buffer.position()); // 0

{
    final var dst = new short[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(dst);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=3 lim=7 cap=7]

    System.out.println(Arrays.toString(dst)); // [10, 20, 30]
    System.out.println(buffer.position()); // 3
}
{
    final var dst = new short[3];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0]

    final var ret = buffer.get(dst);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=6 lim=7 cap=7]

    System.out.println(Arrays.toString(dst)); // [40, 50, 60]
    System.out.println(buffer.position()); // 6
}

try {
    final var dst = new short[3];
    final var ret = buffer.get(dst);
} catch (BufferUnderflowException e) {
    System.out.println("BufferUnderflowException!");
}

// Result
// ↓
//BufferUnderflowException!

ShortBuffer get (short[] dst, int offset, int length)

Relative bulk get method.

final short[] array = {10, 20, 30, 40, 50, 60, 70, 80};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 8
System.out.println(buffer.position()); // 0

{
    final var dst = new short[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(dst, 0, 3);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=3 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [10, 20, 30, 0, 0]
    System.out.println(buffer.position()); // 3
}
{
    final var dst = new short[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(dst, 1, 3);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=6 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [0, 40, 50, 60, 0]
    System.out.println(buffer.position()); // 6
}
{
    final var dst = new short[5];
    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 0, 0]

    final var ret = buffer.get(dst, 3, 2);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=8 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [0, 0, 0, 70, 80]
    System.out.println(buffer.position()); // 8
}

final boolean hasArray ()

Tells whether or not this buffer is backed by an accessible short array.

final var buffer = ShortBuffer.allocate(5)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30);

final var ret = buffer.hasArray();
System.out.println(ret); // true

if (ret) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0]
}
final var buffer = ShortBuffer.allocate(5)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30)
        .asReadOnlyBuffer();

System.out.println(buffer.hasArray()); // false
try {
    final var array = buffer.array();
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

int hashCode ()

Returns the current hash code of this buffer.

final var buffer = ShortBuffer.allocate(0);
System.out.println(buffer.hashCode()); // 1
final var buffer = ShortBuffer.allocate(3)
        .put((short) 10)
        .put((short) 20)
        .put((short) 30)
        .clear();

System.out.println(buffer.hashCode()); // 59251
final var buffer = ShortBuffer.allocate(3)
        .put((short) -10)
        .put((short) -20)
        .put((short) -30)
        .clear();

System.out.println(buffer.hashCode()); // 331

abstract boolean isDirect ()

Tells whether or not this short buffer is direct.

final var buffer = ShortBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final short[] array = {10, 20, 30};
final var buffer = ShortBuffer.wrap(array);
System.out.println(buffer.isDirect()); // false
final var buffer = ByteBuffer.allocateDirect(12).asShortBuffer();
System.out.println(buffer.isDirect()); // true

final ShortBuffer limit (int newLimit)

Sets this buffer's limit.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5

System.out.println(buffer.limit(3)); // java.nio.HeapShortBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3

System.out.println(buffer.clear()); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.limit(3)); // java.nio.HeapShortBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30

try {
    final var ret = buffer.get();
} catch (BufferUnderflowException e) {
    System.out.println("BufferUnderflowException!");
}

// Result
// ↓
//BufferUnderflowException!

final ShortBuffer mark ()

Sets this buffer's mark at its position.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20

System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapShortBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50

System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapShortBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50

int mismatch (ShortBuffer that)

Finds and returns the relative index of the first mismatch between this buffer and a given buffer.

final short[] array = {10, 20, 30, 40};
final var buffer = ShortBuffer.wrap(array);

{
    final short[] a = {10};
    final var that = ShortBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 1
}
{
    final short[] a = {10, 20};
    final var that = ShortBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 2
}
{
    final short[] a = {10, 20, 30};
    final var that = ShortBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 3
}
{
    final short[] a = {10, 20, 30, 40};
    final var that = ShortBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // -1
}
{
    final short[] a = {10, 20, 30, 40, 50};
    final var that = ShortBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 4
}
{
    final short[] a = {10, 20, 999};
    final var that = ShortBuffer.wrap(a); // 2

    System.out.println(buffer.mismatch(that));
}
{
    final short[] a = {-90, -80};
    final var that = ShortBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 0
}

abstract ByteOrder order ()

Retrieves this buffer's byte order.

final var os = System.getProperty("os.name");
System.out.println(os); // Windows 10

final var buffer = ShortBuffer.allocate(5);
System.out.println(buffer.order()); // LITTLE_ENDIAN

final ShortBuffer position (int newPosition)

Sets this buffer's position.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 1

System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 2

System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 3

System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4

System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5

System.out.println(buffer.position(3)); // java.nio.HeapShortBuffer[pos=3 lim=5 cap=5]

System.out.println(buffer.get()); // 40
System.out.println(buffer.position()); // 4

System.out.println(buffer.get()); // 50
System.out.println(buffer.position()); // 5

abstract ShortBuffer put (int index, short s)

Absolute put method (optional operation).

final var buffer = ShortBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

System.out.println(buffer.put(0, (short) 10)); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(1, (short) 20)); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(2, (short) 30)); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(3, (short) 40)); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(4, (short) 50)); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]

System.out.println(buffer.position()); // 0

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}

ShortBuffer put (int index, short[] src)

Absolute bulk put method (optional operation).

final var buffer = ShortBuffer.allocate(6);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

{
    final short[] src = {10};

    final var ret = buffer.put(0, src);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
}
{
    final short[] src = {20, 30};

    final var ret = buffer.put(1, src);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
}
{
    final short[] src = {40, 50, 60};

    final var ret = buffer.put(3, src);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
}

System.out.println(buffer.position()); // 0

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
}

ShortBuffer put (int index, short[] src, int offset, int length)

Absolute bulk put method (optional operation).

final var array = new short[6];
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

final short[] src = {10, 20, 30, 40, 50, 60};

final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]

final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]

final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]

System.out.println(buffer.position()); // 0

ShortBuffer put (int index, ShortBuffer src, int offset, int length)

Absolute bulk put method (optional operation).

final var array = new short[6];
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

final short[] srcArray = {10, 20, 30, 40, 50, 60};
final var src = ShortBuffer.wrap(srcArray);

final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]

final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]

final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapShortBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]

System.out.println(buffer.position()); // 0

abstract ShortBuffer put (short s)

Relative put method (optional operation).

final var buffer = ShortBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

System.out.println(buffer.put((short) 10)); // java.nio.HeapShortBuffer[pos=1 lim=5 cap=5]
System.out.println(buffer.put((short) 20)); // java.nio.HeapShortBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.put((short) 30)); // java.nio.HeapShortBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.put((short) 40)); // java.nio.HeapShortBuffer[pos=4 lim=5 cap=5]
System.out.println(buffer.put((short) 50)); // java.nio.HeapShortBuffer[pos=5 lim=5 cap=5]

System.out.println(buffer.position()); // 5

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}

final ShortBuffer put (short[] src)

Relative bulk put method (optional operation).

final var buffer = ShortBuffer.allocate(6);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

{
    final short[] src = {10};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=1 lim=6 cap=6]
}
{
    final short[] src = {20, 30};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=3 lim=6 cap=6]
}
{
    final short[] src = {40, 50, 60};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=6 lim=6 cap=6]
}

System.out.println(buffer.position()); // 6

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]
}

ShortBuffer put (short[] src, int offset, int length)

Relative bulk put method (optional operation).

final var array = new short[6];
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

final short[] src = {10, 20, 30, 40, 50, 60};

final var ret1 = buffer.put(src, 0, 1);
System.out.println(ret1); // java.nio.HeapShortBuffer[pos=1 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 0, 0, 0, 0, 0]

final var ret2 = buffer.put(src, 1, 2);
System.out.println(ret2); // java.nio.HeapShortBuffer[pos=3 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 0, 0, 0]

final var ret3 = buffer.put(src, 3, 3);
System.out.println(ret3); // java.nio.HeapShortBuffer[pos=6 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50, 60]

System.out.println(buffer.position()); // 6

ShortBuffer put (ShortBuffer src)

Relative bulk put method (optional operation).

final var buffer = ShortBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

{
    final short[] array = {10, 20};
    final var src = ShortBuffer.wrap(array);

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=2 lim=5 cap=5]
}
{
    final short[] array = {30, 40, 50};
    final var src = ShortBuffer.wrap(array);

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapShortBuffer[pos=5 lim=5 cap=5]
}

System.out.println(buffer.position()); // 5

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [10, 20, 30, 40, 50]
}

final ShortBuffer reset ()

Resets this buffer's position to the previously-marked position.

Please see mark().

final ShortBuffer rewind ()

Rewinds this buffer.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

buffer.position(2);
buffer.limit(4);

System.out.println(buffer.position()); // 2
System.out.println(buffer.limit()); // 4

System.out.println(buffer.rewind()); // java.nio.HeapShortBuffer[pos=0 lim=4 cap=5]

System.out.println(buffer.position()); // 0
System.out.println(buffer.limit()); // 4

abstract ShortBuffer slice ()

Creates a new short buffer whose content is a shared subsequence of this buffer's content.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

buffer.position(1);
buffer.limit(4);

final var sliced = buffer.slice();

System.out.println(buffer); // java.nio.HeapShortBuffer[pos=1 lim=4 cap=5]
System.out.println(sliced); // java.nio.HeapShortBuffer[pos=0 lim=3 cap=3]

System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // 20
System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.hasRemaining()); // false

abstract ShortBuffer slice (int index, int length)

Creates a new short buffer whose content is a shared subsequence of this buffer's content.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

final var sliced = buffer.slice(2, 3);

System.out.println(buffer); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
System.out.println(sliced); // java.nio.HeapShortBuffer[pos=0 lim=3 cap=3]

System.out.println(buffer.get()); // 10
System.out.println(buffer.get()); // 20
System.out.println(buffer.get()); // 30
System.out.println(buffer.get()); // 40
System.out.println(buffer.get()); // 50
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // 30
System.out.println(sliced.get()); // 40
System.out.println(sliced.get()); // 50
System.out.println(sliced.hasRemaining()); // false

String toString ()

Returns a string summarizing the state of this buffer.

final var buffer = ShortBuffer.allocate(5)
        .limit(4)
        .position(2);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 4
System.out.println(buffer.position()); // 2

final var str1 = buffer.toString();
System.out.println(str1); // java.nio.HeapShortBuffer[pos=2 lim=4 cap=5]

buffer.clear();

final var str2 = buffer.toString();
System.out.println(str2); // java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]

static ShortBuffer wrap (short[] array)

Wraps a short array into a buffer.

final short[] array = {10, 20, 30, 40, 50};
final var buffer = ShortBuffer.wrap(array);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0

if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
}

static ShortBuffer wrap (short[] array, int offset, int length)

Wraps a short array into a buffer.

final short[] array = {10, 20, 30, 40, 50, 60, 70};
final var buffer = ShortBuffer.wrap(array, 2, 4);

System.out.println(buffer.capacity()); // 7
System.out.println(buffer.limit()); // 6
System.out.println(buffer.position()); // 2

if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0

    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
    System.out.println(buffer.get()); // 60
}

Methods declared in Buffer

capacity, hasRemaining, isReadOnly, limit, position, remaining

Please see the link below.


Related posts

To top of page