LongBuffer put(long[]) array in Java

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

Syntax:

LongBuffer java.nio.LongBuffer.put(long[] src)

This method takes one argument of type long array as its parameter. This method transfers the entire content of the given source long array into this buffer.

Parameters: One parameter is required for this method.

src: The source array.

Returns: This buffer.

Throws:

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

2. ReadOnlyBufferException - If this buffer is read-only

Approach 1: When no exceptions.

Java

import java.nio.LongBuffer;

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

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

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

Output:

java.nio.HeapLongBuffer[pos=5 lim=6 cap=6]


Approach 2: BufferOverflowException 

Java

import java.nio.LongBuffer;

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

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

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


Output:

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


Approach 3: ReadOnlyBufferException

Java

import java.nio.LongBuffer;

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

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

        LongBuffer readOnly = lb.asReadOnlyBuffer();

        long src[] = { 1, 2, 3, 4, 5 };
        System.out.println(readOnly.put(src));
    }
}


Output:

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


Some more put Methods.


put(long)


put(LongBuffer)


put(int, long)


put(int, long[])


put(long[], int, int)


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


No comments:

Post a Comment