Sunday, November 17, 2024
Google search engine
HomeLanguagesJavajava.nio.ShortBuffer Class in Java

java.nio.ShortBuffer Class in Java

ShortBuffer holds a sequence of integer values to be used in an I/O operation. The ShortBuffer class provides the following four categories of operations upon long buffers:

  • Absolute and relative get method that read single Shorts.
  • Absolute and relative put methods that write single Shorts.
  • Relative bulk put and get methods that transfer contiguous sequences of Shorts from an int array or some other Shorts buffer into this buffer and from this buffer into an array.

Short buffers can be created by:

  • allocate(): This allocates space for the buffer’s content.
  • wrap(): This wraps an existing long array into a buffer.

Most of the methods of ShortBuffer class are directly analogous to the methods defined by ByteBuffer.

Class Declaration:

public abstract class ShortBuffer

extends Buffer

implements Comparable<ShortBuffer>

Methods of ShortBuffer class:

Method Description
allocate(int capacity) This method allocates a new short buffer.
array() This method returns the short array that backs this buffer. 
arrayOffset() This method returns the offset within this buffer’s backing array of the first element of the buffer.
asReadOnlyBuffer() This method creates a new, read-only short buffer that shares this buffer’s content.
compact() This method compacts this buffer.
compareTo(ShortBuffer that) This method compares this buffer to another.
duplicate() This method creates a new short buffer that shares this buffer’s content.
equals(Object ob) This method tells whether this buffer is equal to another object.
get() This method is a relative get method and returns the short at the buffer’s current position.
get(int index) This method is an absolute get method and returns the short at the given index.
get(int[] dst) This method is a relative bulk get method and returns this buffer.
get(int[] dst, int offset, int length) This method relative is a bulk get method and returns this buffer.
hasArray() This method tells whether this buffer is backed by an accessible int array. 
isDirect() This method tells whether this int buffer is direct.
order() This method retrieves this buffer’s byte order.
put(int index, short s) This method is an absolute put method and returns this buffer.  
put(short s) This method is a relative put method and returns this buffer.
put(short[] src) This method is a relative bulk put method and returns this buffer.
put(short[] src, int offset, int length) This method is a relative bulk put method and returns this buffer.
put(ShortBuffer src) This method is a relative bulk put method and returns this buffer.
slice() This method creates a new short buffer whose content is a shared subsequence of this buffer’s content.
toString() This method returns a string summarizing the state of this buffer.
wrap(short[] array) This method wraps a short array into a buffer.
wrap(short[] array, int offset, int length) This method wraps a short array into a buffer.

Following are some programs to demonstrate ShortBuffer class and its methods:

Example 1:

Java




// Java program to demonstrate
// ShortBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ShortBuffer
        int capacity = 10;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of Shortbuffer
            // and allocating size capacity
            ShortBuffer sb = ShortBuffer.allocate(capacity);
  
            // putting the value in Shortbuffer
            sb.put((short)100);
            sb.put(2, (short)9);
            sb.rewind();
  
            // getting array from fb ShortBuffer using
            // array() method
            short[] sbb = sb.array();
  
            // printing the ShortBuffer fb
            System.out.println("ShortBuffer: "
                               + Arrays.toString(sbb));
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println(
                "IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println(
                "ReadOnlyBufferException catched");
        }
    }
}


Output

ShortBuffer: [100, 0, 9, 0, 0, 0, 0, 0, 0, 0]

Example 2:

Java




// Java program to demonstrate
// ShortBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ShortBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the ShortBuffer 2
        int capacity2 = 10;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of Shortbuffer 1
            // and allocating size capacity
            ShortBuffer sb1
                = ShortBuffer.allocate(capacity1);
  
            // creating object of Shortbuffer 2
            // and allocating size capacity
            ShortBuffer fb2
                = ShortBuffer.allocate(capacity2);
  
            // putting the value in Shortbuffer 1
            sb1.put((short)100);
            sb1.put(2, (short)9);
            sb1.rewind();
  
            // putting the value in Shortbuffer 2
            fb2.put((short)100);
            fb2.put(2, (short)9);
            fb2.rewind();
  
            // print the ShortBuffer 1
            System.out.println(
                "ShortBuffer 1: "
                + Arrays.toString(sb1.array()));
  
            // print the ShortBuffer 2
            System.out.println(
                "ShortBuffer 2: "
                + Arrays.toString(fb2.array()));
  
            // checking the equality of both ShortBuffer
            boolean fbb = sb1.equals(fb2);
  
            // checking if else condition
            if (fbb)
                System.out.println("both are equal");
            else
                System.out.println("both are not equal");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println(
                "IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println(
                "ReadOnlyBufferException catched");
        }
    }
}


Output

ShortBuffer 1: [100, 0, 9, 0, 0, 0, 0, 0, 0, 0]
ShortBuffer 2: [100, 0, 9, 0, 0, 0, 0, 0, 0, 0]
both are equal
RELATED ARTICLES

Most Popular

Recent Comments