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)


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)


ByteBuffer putInt() in Java

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

Approach 1: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.putInt(int value)

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

Parameters: One parameter is required for this method.

value: The int value to be written.

Returns: This buffer.

Throws:

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

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

Java

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

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

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

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

    }
}

Output:

[0, 0, 0, 12]


Approach 1.1: BufferOverflowException 

Java

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

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

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

        int value = 12;
        ByteBuffer newBB = bb.putInt(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.putInt(HeapByteBuffer.java:446)


Approach 1.2: ReadOnlyBufferException 

Java

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

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

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

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

    }
}


Output:

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


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.putInt(int index, int value)

This method takes two arguments of type int as its parameters. This method writes four bytes containing the given int 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 int value to be written.

Returns: This buffer.

Throws:

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

2. ReadOnlyBufferException - If this buffer is read-only

Java

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

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

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

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

    }
}

Output:

[1, 2, 0, 0, 0, 12, 8]


Approach 2.1: IndexOutOfBoundsException 

Java

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

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

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

        int index = 5;
        int value = 12;
        ByteBuffer newBB = bb.putInt(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.putInt(HeapByteBuffer.java:456)


Approach 2.2: ReadOnlyBufferException

Java

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

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

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

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

    }
}


Output:

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


ByteBuffer putFloat() in Java

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

Approach 1: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.putFloat(float value)

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

Parameters: One parameter is required for this method.

value: The float value to be written.

Returns: This buffer.

Throws:

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

2. ReadOnlyBufferException - If this buffer is read-only

Java

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

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

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

        float value = 12.3f;

        ByteBuffer newBB = bb.putFloat(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}

Output:

[65, 68, -52, -51]


Approach 1.1: BufferOverflowException

Java

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

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

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

        float value = 12.3f;

        ByteBuffer newBB = bb.putFloat(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.putFloat(HeapByteBuffer.java:561)


Approach 1.2:ReadOnlyBufferException 

Java

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

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        float value = 12.3f;

        ByteBuffer newBB = readOnly.putFloat(value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

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


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.putFloat(int index, float value)

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

Returns: This buffer.

Throws:

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

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

Java

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

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

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

        int index = 2;
        float value = 12.45f;

        ByteBuffer newBB = bb.putFloat(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}

Output:

[1, 2, 65, 71, 51, 51, 7]


Approach 2.1: IndexOutOfBoundsException

Java

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

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

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

        int index = 5;
        float value = 12.45f;

        ByteBuffer newBB = bb.putFloat(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.putFloat(HeapByteBuffer.java:572)


Approach 2.2: ReadOnlyBufferException 

Java

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

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        int index = 2;
        float value = 12.45f;

        ByteBuffer newBB = readOnly.putFloat(index, value);
        System.out.println(Arrays.toString(newBB.array()));

    }
}


Output:

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