ByteBuffer asReadOnlyBuffer() in Java

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

Syntax:

ByteBuffer java.nio.ByteBuffer.asReadOnlyBuffer()

This method creates a new, read-only byte buffer that shares this buffer content.

Note: The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer, and its byte order will be BIG_ENDIAN.

Parameters: NA

Returns: The new, read-only byte buffer.

Exceptions: NA

Approach

Java

import java.nio.ByteBuffer;

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

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

        System.out.println(bb.asReadOnlyBuffer());

    }
}

Output:

java.nio.HeapByteBufferR[pos=0 lim=4 cap=4]

No comments:

Post a Comment