ByteBuffer putChar() in Java

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

Approach 1: When the method takes one argument.

Syntax:

ByteBuffer java.nio.ByteBuffer.putChar(char value)

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

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

        char c = 'a';
        System.out.println(Arrays.toString(bb.putChar(c).array()));

    }
}

Output:

[0, 97, 3, 4]


Approach 1.1: BufferOverflowException 

Java

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

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

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

        char c = 'a';
        System.out.println(Arrays.toString(bb.putChar(c).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.putChar(HeapByteBuffer.java:334)


Approach 1.2: ReadOnlyBufferException 

Java

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

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();

        char c = 'a';
        System.out.println(Arrays.toString(readOnly.putChar(c).array()));

    }
}


Output:

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


Approach 2: When the method takes two arguments.

Syntax:

ByteBuffer java.nio.ByteBuffer.putChar(int index, char value)

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

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

        int index = 2;
        char value = 'a';

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

    }
}

Output:

[1, 2, 0, 97]


Approach 2.1: IndexOutOfBoundsException

Java

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

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

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

        int index = 3;
        char value = 'a';

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


Approach 2.1: ReadOnlyBufferException

Java

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

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

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

        ByteBuffer readOnly = bb.asReadOnlyBuffer();
        int index = 3;
        char value = 'a';

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

    }
}


Output:

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


No comments:

Post a Comment