ByteBuffer slice() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 subsequance buffer
            System.out.println("\nshared subsequance 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");
        }
    }
}

chevron_right


Output:

Original ByteBuffer: [10, 20, 0, 0, 0]

position:  2

capacity:  5

shared subsequance ByteBuffer: [10, 20, 0, 0, 0]

position:  0

capacity:  3

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 subsequance buffer
            System.out.println("\nshared subsequance 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");
        }
    }
}

chevron_right


Output:

Original ByteBuffer: [10, 20, 30, 40, 50]

position:  5

capacity:  5

shared subsequance ByteBuffer: [10, 20, 30, 40, 50]

position:  0

capacity:  0

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#slice–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.