java.nio.ShortBuffer Class in Java
Last Updated :
29 Mar, 2021
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
ShortBuffer sb = ShortBuffer.allocate(capacity);
sb.put(( short ) 100 );
sb.put( 2 , ( short ) 9 );
sb.rewind();
short [] sbb = sb.array();
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 10 ;
int capacity2 = 10 ;
try {
ShortBuffer sb1
= ShortBuffer.allocate(capacity1);
ShortBuffer fb2
= ShortBuffer.allocate(capacity2);
sb1.put(( short ) 100 );
sb1.put( 2 , ( short ) 9 );
sb1.rewind();
fb2.put(( short ) 100 );
fb2.put( 2 , ( short ) 9 );
fb2.rewind();
System.out.println(
"ShortBuffer 1: "
+ Arrays.toString(sb1.array()));
System.out.println(
"ShortBuffer 2: "
+ Arrays.toString(fb2.array()));
boolean fbb = sb1.equals(fb2);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...