The slice() method of java.nio.ByteBuffer Class is used to creates a new byte buffer whose content is a shared subsequence of the given 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 floats 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 ByteBuffer slice()
Return Value: This method returns the new byte buffer.
Below are the examples to illustrate the slice() method:
Examples 1:
// 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 ByteBuffer
int capacity = 5 ;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb1
= ByteBuffer.allocate(capacity);
// putting the value in ByteBuffer
bb1.put(( byte ) 10 );
bb1.put(( byte ) 20 );
// print the ByteBuffer
System.out.println( "Original ByteBuffer: "
+ Arrays.toString(bb1.array()));
// print the ByteBuffer position
System.out.println( "\nposition: "
+ bb1.position());
// print the ByteBuffer capacity
System.out.println( "\ncapacity: "
+ bb1.capacity());
// Creating a shared subsequence buffer
// of given ByteBuffer
// using slice() method
ByteBuffer bb2 = bb1.slice();
// print the shared subsequence buffer
System.out.println( "\nshared subsequence ByteBuffer: "
+ Arrays.toString(bb2.array()));
// print the ByteBuffer position
System.out.println( "\nposition: " + bb2.position());
// print the ByteBuffer capacity
System.out.println( "\ncapacity: " + bb2.capacity());
}
catch (IllegalArgumentException e) {
System.out.println( "IllegalArgumentException catched" );
}
catch (ReadOnlyBufferException e) {
System.out.println( "ReadOnlyBufferException catched" );
}
}
} |
Original ByteBuffer: [10, 20, 0, 0, 0] position: 2 capacity: 5 shared subsequence ByteBuffer: [10, 20, 0, 0, 0] position: 0 capacity: 3
Examples 2:
// 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 ByteBuffer
int capacity = 5 ;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb1 = ByteBuffer.allocate(capacity);
// putting the value in ByteBuffer
bb1.put(( byte ) 10 )
.put(( byte ) 20 )
.put(( byte ) 30 )
.put(( byte ) 40 )
.put(( byte ) 50 );
// print the ByteBuffer
System.out.println( "Original ByteBuffer: "
+ Arrays.toString(bb1.array()));
// print the ByteBuffer position
System.out.println( "\nposition: "
+ bb1.position());
// print the ByteBuffer capacity
System.out.println( "\ncapacity: "
+ bb1.capacity());
// Creating a shared subsequence buffer
// of given ByteBuffer
// using slice() method
ByteBuffer bb2 = bb1.slice();
// print the shared subsequence buffer
System.out.println( "\nshared subsequence ByteBuffer: "
+ Arrays.toString(bb2.array()));
// print the ByteBuffer position
System.out.println( "\nposition: " + bb2.position());
// print the ByteBuffer capacity
System.out.println( "\ncapacity: " + bb2.capacity());
}
catch (IllegalArgumentException e) {
System.out.println( "IllegalArgumentException catched" );
}
catch (ReadOnlyBufferException e) {
System.out.println( "ReadOnlyBufferException catched" );
}
}
} |
Original ByteBuffer: [10, 20, 30, 40, 50] position: 5 capacity: 5 shared subsequence ByteBuffer: [10, 20, 30, 40, 50] position: 0 capacity: 0
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#slice–