ShortBuffer slice() method in Java with Examples
Last Updated :
05 Aug, 2021
The slice() method of java.nio.ShortBuffer Class is used to create a new short buffer whose content is a shared subsequence of this buffer's content.
The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
The new buffer's position will be zero, its capacity and its limit will be the number of shorts remaining in this buffer, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
Syntax:
public abstract ShortBuffer slice()
Return Value: This method returns the new short buffer.
Below are the examples to illustrate the slice() method:
Program 1:
Java
// Java program to demonstrate
// slice() method
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 sb1 = ShortBuffer.allocate(capacity);
// putting the value in Shortbuffer
sb1.put((short)856);
sb1.put((short)961);
// print the ShortBuffer
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb1.array()));
// print the ShortBuffer position
System.out.println("\nposition: " + sb1.position());
// print the ShortBuffer capacity
System.out.println("\ncapacity: " + sb1.capacity());
// Creating a shared subsequence buffer of given ShortBuffer
// using slice() method
ShortBuffer sb2 = sb1.slice();
// print the shared subsequence buffer
System.out.println("\nshared subsequence ShortBuffer: "
+ Arrays.toString(sb2.array()));
// print the ShortBuffer position
System.out.println("\nposition: " + sb2.position());
// print the ShortBuffer capacity
System.out.println("\ncapacity: " + sb2.capacity());
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
OutputOriginal ShortBuffer: [856, 961, 0, 0, 0, 0, 0, 0, 0, 0]
position: 2
capacity: 10
shared subsequence ShortBuffer: [856, 961, 0, 0, 0, 0, 0, 0, 0, 0]
position: 0
capacity: 8
Program 2:
Java
// Java program to demonstrate
// slice() method
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 sb1 = ShortBuffer.allocate(capacity);
// putting the value in Shortbuffer
sb1.put((short)856);
sb1.put((short)961);
sb1.put((short)656);
sb1.put((short)361);
// print the ShortBuffer
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb1.array()));
// print the ShortBuffer position
System.out.println("\nposition: " + sb1.position());
// print the ShortBuffer capacity
System.out.println("\ncapacity: " + sb1.capacity());
// Creating a shared subsequence buffer of given ShortBuffer
// using slice() method
ShortBuffer sb2 = sb1.slice();
sb2.put((short)234);
sb2.put((short)634);
// print the shared subsequence buffer
System.out.println("\nshared subsequence ShortBuffer: "
+ Arrays.toString(sb2.array()));
// print the ShortBuffer position
System.out.println("\nposition: " + sb2.position());
// print the ShortBuffer capacity
System.out.println("\ncapacity: " + sb2.capacity());
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
OutputOriginal ShortBuffer: [856, 961, 656, 361, 0, 0, 0, 0, 0, 0]
position: 4
capacity: 10
shared subsequence ShortBuffer: [856, 961, 656, 361, 234, 634, 0, 0, 0, 0]
position: 2
capacity: 6
Similar Reads
ShortBuffer toString() method in Java with Examples The toString() method in java.nio.ShortBuffer is used to return a string summarizing the state of this buffer. Syntax: public String toString() Return Value:The method returns a summary string. Below are the examples to illustrate the toString() method: Program 1: Java // Java program to demonstrate
1 min read
ShortBuffer flip() methods in Java with Examples The flip() method of java.nio.ShortBuffer Class is used to flip this buffer. By flipping this buffer, it meant that the buffer will be trimmed to the current position and the then the position will be changed to zero. During this process, if any mark is there on the buffer, then that mark will be au
2 min read
ShortBuffer rewind() method in Java with Examples The rewind() method of java.nio.ShortBuffer Class is used to rewind this buffer. By rewinding this Buffer, the following actions are taken: Current position is set to zero the mark is discarded, if any, but the mark value is unchanged. Syntax: public ShortBuffer rewind() Parameter: This method do no
2 min read
ShortBuffer compact() method in Java With Examples The compact() method of java.nio.ShortBuffer Class is used to compact the given buffer. The shorts between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the short at index p = position() is copied to index zero, the short at index p + 1 is c
3 min read
ShortBuffer compareTo method in Java with Examples The compareTo() method of java.nio.ShortBuffer class is used to compare one buffer to another. Two short buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of short
4 min read
ShortBuffer arrayOffset() method in Java With Examples The arrayOffset() method of java.nio.ShortBuffer class is used to return the offset within the bufferâs backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). In-order to check whether
3 min read