ByteBuffer putDouble() in Java

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

Approach 1: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.putDouble(double value)

This method takes one argument of type double as its parameter. This method writes eight bytes containing the given double 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 double 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 ByteBufferputDouble {
    public static void main(String[] args) {

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

        double value = 145.99;
        ByteBuffer newBB = bb.putDouble(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}

Output:

[64, 98, 63, -82, 20, 122, -31, 72, 89]


Approach 1.1: BufferOverflowException 

Java

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

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

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

        double value = 145.99;
        ByteBuffer newBB = bb.putDouble(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.putDouble(HeapByteBuffer.java:621)


Approach 1.2: ReadOnlyBufferException

Java

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

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        double value = 145.99;
        ByteBuffer newBB = readOnly.putDouble(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

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


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.putDouble(int index, double value)

This method takes two arguments one of type int and another of type double as its parameters. This method writes eight bytes containing the given double 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 double 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 ByteBufferputDouble2 {
    public static void main(String[] args) {

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

        int index = 2;
        double value = 18.5;
        ByteBuffer newBB = bb.putDouble(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}

Output:

[1, 2, 64, 50, -128, 0, 0, 0, 0, 0]


Approach 2.1: IndexOutOfBoundsException

Java

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

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

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

        int index = 6;
        double value = 18.5;
        ByteBuffer newBB = bb.putDouble(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.putDouble(HeapByteBuffer.java:632)


Approach 2.2: ReadOnlyBufferException 

Java

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

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        int index = 6;
        double value = 18.5;
        ByteBuffer newBB = readOnly.putDouble(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

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


No comments:

Post a Comment