Buffer isReadOnly() methods in Java with Examples Last Updated : 16 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The isReadOnly() method of java.nio.Buffer class is used to tell whether or not this buffer is read-only. Syntax: public abstract boolean isReadOnly() Returns: This method will return true if, and only if, this buffer is read-only. Below are the examples to illustrate the isReadOnly() method: Examples 1: Java // Java program to demonstrate // isReadOnly() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 10; // creating object of bytebuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the value in bytebuffer bb.put((byte)10); bb.put((byte)20); bb.rewind(); // Typecast bytebuffer to Buffer Buffer buffer = (Buffer)bb; // checking buffer is backed by array or not boolean isReadOnly = buffer.isReadOnly(); // checking if else condition if (isReadOnly) System.out.println("buffer is" + " ReadOnly buffer"); else System.out.println("buffer is not" + " ReadOnly buffer"); } } Output: buffer is not ReadOnly buffer Examples 2: Java // Java program to demonstrate // isReadOnly() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 10; // creating object of bytebuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the value in bytebuffer bb.put((byte)10); bb.put((byte)20); bb.rewind(); // Creating a read-only copy of ByteBuffer // using asReadOnlyBuffer() method ByteBuffer bb1 = bb.asReadOnlyBuffer(); // Typecast read-only ByteBuffer to read-only buffer Buffer buffer = (Buffer)bb1; // checking buffer is backed by array or not boolean isReadOnly = buffer.isReadOnly(); // checking if else condition if (isReadOnly) System.out.println("buffer is" + " ReadOnly buffer"); else System.out.println("buffer is not" + " ReadOnly buffer"); } } Output: buffer is ReadOnly buffer Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#isReadOnly-- Comment More infoAdvertise with us Next Article ByteBuffer flip() methods in Java with Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-Buffer Practice Tags : Java Similar Reads Buffer isDirect() methods in Java with Examples The isDirect() method of java.nio.Buffer Class is used to tell whether or not this buffer is direct. Syntax: public abstract boolean isDirect() Return Value: This method returns true if, and only if, this buffer is direct. Below are the examples to illustrate the isDirect() method: Examples 1: Java 2 min read Buffer flip() methods in Java with Examples The flip() method of java.nio.ByteBuffer Class is used to flip this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of 3 min read ByteBuffer isDirect() methods in Java with Examples The isDirect() method of java.nio.ByteBuffer Class is used to tell whether or not this byte buffer is direct. Syntax: public abstract boolean isDirect() Return Value: This method returns true if, and only if, this buffer is direct. Below are the examples to illustrate the isDirect() method: Examples 1 min read Buffer hasArray() methods in Java with Examples The hasArray() method of java.nio.Buffer class is used to tell whether or not this buffer is backed by an accessible array. If this method returns true then the array and arrayOffset() methods may safely be invoked. Syntax: public abstract boolean hasArray() Returns: This method will return true if, 2 min read ByteBuffer flip() methods in Java with Examples The flip() method of java.nio.ByteBuffer Class is used to flip this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of 3 min read ByteBuffer getInt() method in Java with Examples getInt() The getInt() method of java.nio.ByteBuffer class is used to read the next four bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by four. Syntax: public abstract int getInt() Return Value: This met 5 min read Like