Java : ShortBuffer con ejemplos
ShortBuffer (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos ShortBuffer.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
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)
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 ()
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 ()
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 ()
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 {
var _ = readOnlyBuffer.put((short) 30);
} catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException!");
}
// Result
// ↓
//ReadOnlyBufferException!
final ShortBuffer clear ()
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 ()
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)
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 ()
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)
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 ()
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 ()
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)
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)
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)
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)
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];
var _ = buffer.get(dst);
} catch (BufferUnderflowException e) {
System.out.println("BufferUnderflowException!");
}
// Result
// ↓
//BufferUnderflowException!
ShortBuffer get (short[] dst, int offset, int length)
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 ()
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 {
var _ = buffer.array();
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
int hashCode ()
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 ()
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)
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 {
var _ = buffer.get();
} catch (BufferUnderflowException e) {
System.out.println("BufferUnderflowException!");
}
// Result
// ↓
//BufferUnderflowException!
final ShortBuffer mark ()
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)
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 ()
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 ()
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
final ShortBuffer rewind ()
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 ()
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)
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 ()
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)
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)
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
Consulte el siguiente enlace.
Related posts
- Ejemplos de API