Skip to main content

ShortBuffer class in java

The java.nio.ShortBuffer class provides several methods for manipulating the buffer's position, limit, and capacity, as well as reading and writing short values to and from the buffer's underlying storage. Here is a list of all the public methods provided by the java.nio.ShortBuffer class:

Factory methods

  • static ShortBuffer allocate(int capacity) - Allocates a new short buffer with the given capacity.
  • static ShortBuffer wrap(short[] array) - Wraps a short array into a buffer.
  • static ShortBuffer wrap(short[] array, int offset, int length) - Wraps a portion of a short array into a buffer.

Reading and writing methods

  • abstract short get() - Reads the next short from the buffer.
  • abstract short get(int index) - Reads the short at the given index from the buffer.
  • abstract ShortBuffer get(short[] dst) - Reads a sequence of shorts from the buffer into the given array.
  • abstract ShortBuffer get(short[] dst, int offset, int length) - Reads a sequence of shorts from the buffer into a subsequence of the given array.
  • abstract ShortBuffer put(short s) - Writes the given short to the buffer.
  • abstract ShortBuffer put(int index, short s) - Writes the given short to the buffer at the given index.
  • abstract ShortBuffer put(short[] src) - Writes a sequence of shorts from the given array to the buffer.
  • abstract ShortBuffer put(short[] src, int offset, int length) - Writes a subsequence of the given array to the buffer.

Other methods

  • abstract ShortBuffer compact() - Copies the remaining shorts in the buffer to the beginning of the buffer and sets the position to the number of shorts copied.
  • abstract ShortBuffer duplicate() - Creates a new buffer that shares the same underlying storage as this buffer.
  • abstract ShortBuffer slice() - Creates a new buffer that shares a portion of this buffer's underlying storage.
  • abstract int compareTo(ShortBuffer other) - Compares this buffer to another buffer.
  • abstract boolean equals(Object ob) - Indicates whether this buffer is equal to another object.
  • abstract int hashCode() - Calculates the hash code for this buffer.
  • abstract boolean isDirect() - Indicates whether this buffer is a direct buffer.
  • abstract ByteOrder order() - Returns the byte order used by this buffer.
  • abstract ShortBuffer order(ByteOrder bo) - Sets the byte order used by this buffer.
  • abstract int capacity() - Returns the capacity of this buffer.
  • abstract int position() - Returns the position of this buffer.
  • abstract ShortBuffer position(int newPosition) - Sets the position of this buffer.
  • abstract int limit() - Returns the limit of this buffer.
  • abstract ShortBuffer limit(int newLimit) - Sets the limit of this buffer.
  • abstract int remaining() - Returns the number of shorts remaining in this buffer.
  • abstract boolean hasRemaining() - Indicates whether there are any shorts remaining in this buffer.
  • abstract short[] array() - Returns the short array that backs this buffer (if there is one).
  • abstract int arrayOffset() - Returns the offset within the backing array of the first element of the buffer.

Declaration of this class as follows.

public abstract class ShortBuffer
extends Buffer
implements Comparable<ShortBuffer>

This reference is belong to javadoc

java.nio.ShortBuffer allocate(int) method example

import java.nio.ShortBuffer;
public class Example
{
    public static void main(String[] args)
    {
        try
        {
            ShortBuffer buffer = ShortBuffer.allocate(5);
            // Put short value
            buffer.put((short) 14);
            buffer.put((short) 32);
            buffer.put((short) 23);
            buffer.put((short) 45);
            buffer.put((short) 34);
            buffer.rewind();
            System.out.println(buffer);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}
Java ShortBuffer class implementation
java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]

short[] array() method example

// Java program for java.nio.ShortBuffer class array() method
// Useful packages
import java.nio.ShortBuffer;
public class Example
{
    public static void main(String[] args)
    {
        try
        {
            ShortBuffer buffer = ShortBuffer.allocate(5);
            // Put short value
            buffer.put((short) 14);
            buffer.put((short) 32);
            buffer.put((short) 23);
            buffer.put((short) 45);
            buffer.put((short) 34);
            buffer.rewind();
            System.out.println(buffer);
            // Get short array
            short[] result = buffer.array();
            for (short value: result)
            {
                System.out.println(value);
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}
java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
14
32
23
45
34

java.nio.ShortBuffer arrayOffset() method example

// Java program for java.nio.ShortBuffer class arrayOffset() method
// Useful packages
import java.nio.ShortBuffer;
class Example
{
    public static void main(String[] args)
    {
        ShortBuffer buffer = ShortBuffer.allocate(5);
        // Put short value
        buffer.put((short) 10);
        buffer.put((short) 20);
        buffer.put((short) 30);
        buffer.rewind();
        int result = buffer.arrayOffset();
        // Display result 
        System.out.println(result);
    }
}
0

java.nio.ShortBuffer asReadOnlyBuffer() method example

// Java program for java.nio.ShortBuffer class, asReadOnlyBuffer() method
// Useful packages
import java.nio.ShortBuffer;
class Example {
    public static void main(String[] args) {
        
        
        ShortBuffer buffer = ShortBuffer.allocate(5);
        // Put short value
        buffer.put((short) 10);
        buffer.put((short) 20);
        buffer.put(2,(short) 30);
        buffer.rewind();
      
        ShortBuffer read = buffer.asReadOnlyBuffer();
        // Value are not change
        // read.put(2,(short) 30);

        // Display read buffer 
        while (read.hasRemaining()){
            System.out.println(read.get());
        } 
    }
}
asReadOnlyBuffer method in Java ShortBuffer class
10
20
30
0
0

java.nio.ShortBuffer compact() method example

// Java program for java.nio.ShortBuffer class compact() method
// Useful packages
import java.nio.ShortBuffer;
import java.util.Arrays;
class Example
{
    public static void main(String[] args)
    {
        ShortBuffer buffer = ShortBuffer.allocate(6);
        // Put short value
        buffer.put((short) 10);
        buffer.put((short) 20);
        buffer.put((short) 30);

        System.out.println(Arrays.toString(buffer.array()));
        ShortBuffer compactBuffer = buffer.compact();
        System.out.println(Arrays.toString(compactBuffer.array()));
        // Update value
        compactBuffer.put(4,(short) 50);
        System.out.println(Arrays.toString(compactBuffer.array()));

    }
}
[10, 20, 30, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 50, 0]

java.nio.ShortBuffer compareTo(ShortBuffer) method example

// Java program for java.nio.ShortBuffer class compact() method
// Useful packages
import java.nio.ShortBuffer;
import java.util.Arrays;
class Example
{
    public static void main(String[] args)
    {
        ShortBuffer b1 = ShortBuffer.allocate(5);
        ShortBuffer b2 = ShortBuffer.allocate(5);
        ShortBuffer b3 = ShortBuffer.allocate(6);
        // Put short value in b1
        b1.put((short) 10);
        b1.put((short) 20);
        b1.put((short) 30);
        b1.rewind();
        // Put short value in b2
        b2.put((short) 10);
        b2.put((short) 20);
        b2.put((short) 30);
        b2.rewind();
        // Put short value in b3
        b3.put((short) 31);
        b3.put((short) 44);
        b3.put((short) 34);
        b3.rewind();
        // lexicographically equal 
        System.out.println(b1.compareTo(b2));
        // b2 lexicographically less of b3
        System.out.println(b2.compareTo(b3));
        // b3 lexicographically greater of b1
        System.out.println(b3.compareTo(b1));
    }
}
0
-21
21

java.nio.ShortBuffer duplicate() method example

// Java program for java.nio.ShortBuffer class duplicate() method
// Useful packages
import java.nio.ShortBuffer;
import java.util.Arrays;
class Example {
    public static void main(String[] args) {
        
        ShortBuffer b1 = ShortBuffer.allocate(5);

        // Put short value in b1
        b1.put((short) 10);
        b1.put((short) 20);
        b1.put((short) 30);
        b1.rewind();
        ShortBuffer b2 = b1.duplicate();

        System.out.println(Arrays.toString(b1.array()));
        System.out.println(Arrays.toString(b2.array()));
        // Change b1 value 
        b1.put(4,(short)100);

        System.out.println(Arrays.toString(b1.array()));
        System.out.println(Arrays.toString(b2.array()));
    }
}
[10, 20, 30, 0, 0]
[10, 20, 30, 0, 0]
[10, 20, 30, 0, 100]
[10, 20, 30, 0, 100]

java.nio.ShortBuffer equals(Object) method example

// Java program for java.nio.ShortBuffer class equals() method
// Useful packages
import java.nio.ShortBuffer;
import java.util.Arrays;
class Example
{
    public static void main(String[] args)
    {
        ShortBuffer b1 = ShortBuffer.allocate(5);
        ShortBuffer b2 = ShortBuffer.allocate(5);
        ShortBuffer b3 = ShortBuffer.allocate(6);
        // Put short value in b1
        b1.put((short) 10);
        b1.put((short) 20);
        b1.put((short) 30);
        b1.rewind();
        // Put short value in b2
        b2.put((short) 10);
        b2.put((short) 20);
        b2.put((short) 30);
        b2.rewind();
        // Put short value in b3
        b3.put((short) 31);
        b3.put((short) 44);
        b3.put((short) 34);
        b3.rewind();
        System.out.println(b1.equals(b2));
        System.out.println(b2.equals(b3));
        System.out.println(b3.equals(b1));
        System.out.println(b3.equals(b3));
    }
}
true
false
false
true




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment