ByteBuffer putShort() in Java

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

Approach 1: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.putShort(short value)

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

Parameters: One parameter is required for this method.

value: The short value to be written.

Returns: This buffer.

Throws:

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

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

Java

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

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

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

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

    }
}

Output:

[0, 12, 3, 4]


Approach 1.1: BufferOverflowException

Java

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

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

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

        short value = 12;
        ByteBuffer newBB = bb.putShort(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.putShort(HeapByteBuffer.java:390)


Approach 1.2: ReadOnlyBufferException 

Java

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

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

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

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

    }
}


Output:

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


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.putShort(int index, short value)

This method takes two arguments one of type int and another of type short. This method writes two bytes containing the given short 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 short value to be written.

Returns: This buffer.

Throws:

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

2. ReadOnlyBufferException - If this buffer is read-only

Java

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

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

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

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

    }
}

Output:

[1, 2, 0, 12, 6, 7]


Approach 2.1: IndexOutOfBoundsException

Java

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

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

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

        int index = 6;
        short value = 12;
        ByteBuffer newBB = bb.putShort(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.putShort(HeapByteBuffer.java:400)


Approach 2.2: ReadOnlyBufferException 

Java

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

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

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

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

    }
}


Output:

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


No comments:

Post a Comment