LongBuffer put(LongBuffer) in Java

put(LongBuffer): This method is available in java.nio.LongBuffer class of Java.

Syntax:

LongBuffer java.nio.LongBuffer.put(LongBuffer src)

This method takes one argument of type LongBuffer as its parameter. This method transfers the longs remaining in the given source buffer into this buffer.

Parameters: One parameter is required for this method.

src: The source buffer from which longs are to be read; must not be this buffer.

Returns: This buffer.

Throws:

1. BufferOverflowException - If there is insufficient space in this buffer for the remaining longs in the source buffer.

2. IllegalArgumentException - If the source buffer is this buffer.

3. ReadOnlyBufferException - If this buffer is read-only

Approach 1: When no exceptions

Java

import java.nio.LongBuffer;

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

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

        long array2[] = { 1, 2, 3 };
        LongBuffer src = LongBuffer.wrap(array2);
        System.out.println(lb.put(src));
    }
}

Output:

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


Approach 2: BufferOverflowException

Java

import java.nio.LongBuffer;

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

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

        long array2[] = { 1, 2, 3, 4, 5, 6 };
        LongBuffer src = LongBuffer.wrap(array2);
        System.out.println(lb.put(src));
    }
}

Output:

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



Approach 3: IllegalArgumentException 

Java

import java.nio.LongBuffer;

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

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

Output:

Exception in thread "main" java.lang.IllegalArgumentException: The source buffer is this buffer at java.base/java.nio.Buffer.createSameBufferException(Buffer.java:261) at java.base/java.nio.LongBuffer.put(LongBuffer.java:949) at java.base/java.nio.HeapLongBuffer.put(HeapLongBuffer.java:247)



Approach 4: ReadOnlyBufferException

Java

import java.nio.LongBuffer;

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

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

        long array2[] = { 1, 2, 3, 4, 5, 6 };
        LongBuffer src = LongBuffer.wrap(array2);

        LongBuffer readOnly = lb.asReadOnlyBuffer();
        System.out.println(readOnly.put(src));
    }
}

Output:

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


Some more put Methods.


put(long)


put(long[])


put(int, long)


put(int, long[])


put(long[], int, int)


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


No comments:

Post a Comment