ByteBuffer slice() method in Java with Examples
Last Updated :
07 Aug, 2021
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
// 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");
}
}
}
OutputOriginal 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
// 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");
}
}
}
OutputOriginal 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--
Similar Reads
DoubleBuffer slice() method in Java with Examples The slice() method of java.nio.DoubleBuffer Class is used to creates a new double 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
3 min read
ByteBuffer order() method in Java with Examples order() The order() method of java.nio.ByteBuffer class is used to retrieve this buffer's byte order. The byte order is used when reading or writing multibyte values, and when creating buffers that are views of this byte buffer. The order of a newly-created byte buffer is always BIG_ENDIAN. Syntax:
4 min read
ByteBuffer toString() method in Java with Examples The toString() method of ByteBuffer class is the inbuilt method used to returns a string representing the data contained by ByteBuffer Object. A new String object is created and initialized to get the character sequence from this ByteBuffer object and then String is returned by toString(). Subsequen
2 min read
ByteBuffer wrap() methods in Java with Examples wrap(byte[] array) The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte array, i.e., modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be arra
4 min read
ByteBuffer get() method in Java with Examples get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow
6 min read
ByteBuffer flip() methods in Java with Examples The flip() method of java.nio.ByteBuffer Class is used to flip this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of
3 min read
ByteBuffer putInt() methods in Java with Examples putInt(int value) The putInt(int value) method of java.nio.ByteBuffer Class is used to write four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four. Syntax : public abstract ByteBuffer putInt(int value)
6 min read
ByteBuffer rewind() methods in Java with Examples The rewind() method of java.nio.ByteBuffer Class is used to rewind this buffer. The position is set to zero and the mark is discarded. Invoke this method before a sequence of channel-write or get operations, assuming that the limit has already been set appropriately. Invoking this method neither cha
2 min read
ByteBuffer position() methods in Java with Examples The position(int newPosition) method of java.nio.ByteBuffer Class is used to Sets this buffer's position. If the mark is defined and larger than the new position then it is discarded. Syntax: public ByteBuffer position(int newPosition) Parameters: This method takes the newPosition as parameter which
2 min read
ByteBuffer wrap() method in Java with Examples wrap(byte[] array) The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be a
4 min read