ByteBuffer putLong() in Java

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

Approach 1: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.putLong(long value)

This method takes one argument of type long as its parameter. This method writes eight bytes containing the given long value, in the current byte order, into this buffer at the current position, and then increments the position by eight.

Parameters: One parameter is required for this method.

value: The long value to be written.

Returns: This buffer.

Throws:

1. BufferOverflowException - If there are fewer than eight bytes remaining in this buffer.

2. ReadOnlyBufferException - If this buffer is read-only

Java

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

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

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

        long value = 12;
        ByteBuffer newBB = bb.putLong(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}

Output:

[0, 0, 0, 0, 0, 0, 0, 12]


Approach 1.1: BufferOverflowException

Java

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

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

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

        long value = 12;
        ByteBuffer newBB = bb.putLong(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

Exception in thread "main" java.nio.BufferOverflowException at java.base/java.nio.Buffer.nextPutIndex(Buffer.java:725) at java.base/java.nio.HeapByteBuffer.putLong(HeapByteBuffer.java:502)



Approach 1.2: ReadOnlyBufferException

Java

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

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        long value = 12;
        ByteBuffer newBB = readOnly.putLong(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

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


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.putLong(int index, long value)

This method takes two arguments one of type int and another of type long as its parameters. This method writes eight bytes containing the given long value, in the current byte order, into this buffer at the given index.

Parameters: Two parameters are required for this method.

index: The index at which the bytes will be written.

value: The long value to be written.

Returns: This buffer.

Throws:

1. IndexOutOfBoundsException - If the index is negative or not smaller than the buffer's limit, minus seven.

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

Java

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

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

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

        int index = 2;
        long value = 12;
        ByteBuffer newBB = bb.putLong(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}

Output:

[1, 2, 0, 0, 0, 0, 0, 0, 0, 12, 24, 56, 78]


Approach 2.1: IndexOutOfBoundsException

Java

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

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

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

        int index = 7;
        long value = 12;
        ByteBuffer newBB = bb.putLong(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.Buffer.checkIndex(Buffer.java:744) at java.base/java.nio.HeapByteBuffer.putLong(HeapByteBuffer.java:512)


Approach 2.2: ReadOnlyBufferException

Java

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

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        int index = 2;
        long value = 12;
        ByteBuffer newBB = readOnly.putLong(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

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


No comments:

Post a Comment