ByteBuffer put(int, byte[], int, int) in Java

put(int, byte[], int, int): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

ByteBuffer java.nio.ByteBuffer.put(int index, byte[] src, int offset, int length)

This method takes four arguments one of type byte array and rest three are of type int as its parameters. This method transfers length bytes from the given array, starting at the given offset in the array and at the given index in this buffer. The position of this buffer is unchanged.

Parameters: Four parameters are required for this method.

index: The index in this buffer at which the first byte will be written; must be non-negative and less than limit().

src: The array from which bytes are to be read.

offset: The offset within the array of the first byte to be read; must be non-negative and less than src.length.

length: The number of  bytes to be read from the given array; must be non-negative and no larger than the smaller of limit() - index and src.length - offset.

Returns: This buffer.

Throws:

1. IndexOutOfBoundsException - If the preconditions on the index, offset, and length parameters do not hold.

2. ReadOnlyBufferException - If this buffer is read-only.

Approach 1: When no exceptions.

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

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

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

        int index = 0;

        byte src[] = { 345678 };

        int offset = 0, length = 2;

        System.out.println(Arrays.toString(bb.put(index, 
src, offset, length).array()));

    }
}

Output:

[34, 56, 3, 4]


Approach 2: IndexOutOfBoundsException 

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

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

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

        int index = -1;

        byte src[] = { 345678 };

        int offset = 0, length = 2;

        System.out.println(Arrays.toString(bb.put(index, 
src, offset, length).array()));

    }
}


Output:

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.put(HeapByteBuffer.java:257)


Approach 3: ReadOnlyBufferException 

Java

import java.nio.ByteBuffer;
import java.util.Arrays;

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        int index = 0;
        byte src[] = { 345678 };
        int offset = 0, length = 2;

        System.out.println(Arrays.toString(readOnly.put(index, 
src, offset, length).array()));

    }
}


Output:

Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.put(HeapByteBufferR.java:262)


No comments:

Post a Comment