LongBuffer put(long[], int, int) in Java

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

Syntax:

LongBuffer java.nio.LongBuffer.put(long[] src, int offset, int length)

This method takes three arguments one of type long array and the other two are of type int as its parameter. This method transfers longs into this buffer from the given source array.

Parameters: Three parameters are required for this method.

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

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

length: The number of longs to be read from the given array; must be non-negative and no larger than src.length - offset.

Returns: This buffer.

Throws:

1. BufferOverflowException - If there is insufficient space in this buffer.

2. IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold.

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

Approach 1: When no exceptions.

Java

import java.nio.LongBuffer;

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

        long array[] = { 1, 2, 3, 4 };
        LongBuffer lb = LongBuffer.wrap(array);

        int offset = 0;
        int length = 2;
        long src[] = { 2, 3, 4, 5, 6 };
        System.out.println(lb.put(src, offset, length));

    }
}

Output:

java.nio.HeapLongBuffer[pos=2 lim=4 cap=4]


Approach 2: BufferOverflowException 

Java

import java.nio.LongBuffer;

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

        long array[] = { 1, 2, 3, 4 };
        LongBuffer lb = LongBuffer.wrap(array);

        int offset = 0;
        int length = 5;
        long src[] = { 2, 3, 4, 5, 6 };
        System.out.println(lb.put(src, offset, length));

    }
}


Output:

Exception in thread "main" java.nio.BufferOverflowException at java.base/java.nio.HeapLongBuffer.put(HeapLongBuffer.java:235)



Approach 3: IndexOutOfBoundsException

Java

import java.nio.LongBuffer;

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

        long array[] = { 1, 2, 3, 4 };
        LongBuffer lb = LongBuffer.wrap(array);

        int offset = -1;
        int length = 2;
        long src[] = { 2, 3, 4, 5, 6 };
        System.out.println(lb.put(src, offset, length));

    }
}


Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [-1, -1 + 2) out of bounds for length 5 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.HeapLongBuffer.put(HeapLongBuffer.java:232)



Approach 4: ReadOnlyBufferException

Java

import java.nio.LongBuffer;

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

        long array[] = { 1, 2, 3, 4 };
        LongBuffer lb = LongBuffer.wrap(array);

        int offset = 0;
        int length = 2;
        long src[] = { 2, 3, 4, 5, 6 };

        LongBuffer readOnly = lb.asReadOnlyBuffer();
        System.out.println(readOnly.put(src, offset, length));

    }
}


Output:

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


Some more put Methods.


put(long)


put(long[])


put(LongBuffer)


put(int, long)


put(int, long[])


put(int, long[], int, int)


No comments:

Post a Comment