ByteBuffer slice() in Java

slice(): This method is available in java.nio.ByteBuffer class of Java.

Approach 1: When the method does not take any argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.slice()

This method creates a new byte buffer whose content is a shared subsequence of this buffer's content.

Note:

1. The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer, its mark will be undefined, and its byte order will be BIG_ENDIAN.

2. 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.

Parameters: NA

Returns: The new byte buffer.

Exceptions: NA

Java

import java.nio.ByteBuffer;

public class ByteBufferslice {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        System.out.println("Before get method " + bb);
        bb.get();
        System.out.println("Before get method " + bb.slice());

    }
}


Output:

Before get method java.nio.HeapByteBuffer[pos=0 lim=4 cap=4] Before get method java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.slice(int index, int length)

This method creates a new byte buffer whose content is a shared subsequence of this buffer's content. The content of the new buffer will start at position index in this buffer and will contain length elements. 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.

Parameters: Two parameters are required for this method.

index: The position in this buffer at which the content of the new buffer will start; must be non-negative and no larger than limit().

length: The number of elements the new buffer will contain; must benon-negative and no larger than limit() - index.

Returns: The new buffer.

Throws:

1. IndexOutOfBoundsException - If index is negative or greater than limit(), length is negative, or length > limit() - index.

Java

import java.nio.ByteBuffer;

public class ByteBufferslice2 {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        int index = 1, length = 2;

        System.out.println("Before slice " + bb);
        ByteBuffer newBB = bb.slice(index, length);
        System.out.println("After slice " + newBB);

    }
}k


Output:

Before slice java.nio.HeapByteBuffer[pos=0 lim=4 cap=4] After slice java.nio.HeapByteBuffer[pos=0 lim=2 cap=2]


Approach 2.1: IndexOutOfBoundsException 

Java

import java.nio.ByteBuffer;

public class ByteBufferslice2 {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        int index = -1, length = 2;

        System.out.println("Before slice " + bb);
        ByteBuffer newBB = bb.slice(index, length);
        System.out.println("After slice " + newBB);

    }
}


Output:

Before slice java.nio.HeapByteBuffer[pos=0 lim=4 cap=4] Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [-1, -1 + 2) out of bounds for length 4 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) at java.base/java.nio.HeapByteBuffer.slice(HeapByteBuffer.java:121)


No comments:

Post a Comment