ByteBuffer array() method in Java with Examples Last Updated : 03 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The array() method of java.nio.ByteBuffer class is used to return the byte array that backs the taken buffer.Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.Invoke the hasArray() method before invoking this method in order to ensure that this buffer has an accessible backing array. Syntax : public final byte[] array() Return Value: This method returns the array that backs this buffer.Exception: This method throws the ReadOnlyBufferException, If this buffer is backed by an array but is read-only. Below are the examples to illustrate the array() method: Example 1: Java // Java program to demonstrate // array() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the int to byte typecast value in ByteBuffer bb.put((byte)20); bb.put((byte)30); bb.put((byte)40); bb.put((byte)50); // print the ByteBuffer System.out.println("ByteBuffer: " + Arrays.toString(bb.array())); // getting byte array from ByteBuffer // using array() method byte[] arr = bb.array(); // print the byte array System.out.println("\nbyte array: " + Arrays.toString(arr)); } catch (IllegalArgumentException e) { System.out.println("Exception throws: " + e); } } } Output: ByteBuffer: [20, 30, 40, 50] byte array: [20, 30, 40, 50] Example 2: Java // Java program to demonstrate // array() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the int to byte typecast value // in ByteBuffer bb.put((byte)20); bb.put((byte)30); bb.put((byte)40); bb.put((byte)50); bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: " + Arrays.toString(bb.array())); // Creating a read-only copy of ByteBuffer // using asReadOnlyBuffer() method ByteBuffer bb1 = bb.asReadOnlyBuffer(); bb1.rewind(); // print the ByteBuffer System.out.print("\nReadOnlyBuffer ByteBuffer : "); while (bb1.hasRemaining()) System.out.print(bb1.get() + ", "); // getting byte array from read-only // ByteBuffer using array() method System.out.println("\n\nTrying to get the array" + " from bb1 for editing"); byte[] arr = bb1.array(); } catch (IllegalArgumentException e) { System.out.println("Exception throws: " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws: " + e); } } } Output: Original ByteBuffer: [20, 30, 40, 50] ReadOnlyBuffer ByteBuffer : 20, 30, 40, 50, Trying to get the array from bb1 for editing Exception throws: java.nio.ReadOnlyBufferException Comment More infoAdvertise with us Next Article DoubleBuffer array() method in Java With Examples R rohitprasad3 Follow Improve Article Tags : Misc Java Java-Functions Java-NIO package Java-ByteBuffer +1 More Practice Tags : JavaMisc Similar Reads Buffer array() methods in Java with Examples The array() method of java.nio.Buffer class is used to return the array that backs the taken buffer. This method is intended to allow array-backed buffers to be passed to native code more efficiently. Concrete subclasses provide more strongly-typed return values for this method. Modifications to thi 3 min read ByteBuffer arrayOffset() method in Java with Examples The arrayOffset() method of java.nio.ByteBuffer class is used to return the offset within the given buffer's backing array of the first element of the buffer. If this buffer is backed by an array then buffer position p corresponds to array index p + arrayOffset(). Invoke the hasArray method before i 2 min read DoubleBuffer array() method in Java With Examples The array() method of java.nio.DoubleBuffer Class is used to Return the double array that backs this buffer. Modifications to this bufferâs content will cause the returned arrayâs content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to 3 min read ByteBuffer allocate() method in Java with Examples The allocate() method of java.nio.ByteBuffer class is used to allocate a new byte buffer. The new buffer's position will be zero, its limit will be its capacity, its mark will be undefined, and each of its elements will be initialized to zero. It will have a backing array, and its array offset will 2 min read ByteBuffer get() method in Java with Examples get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow 6 min read Buffer arrayOffset() method in Java with Examples The arrayOffset() method of java.nio.Buffer class is used to return the offset within the given buffer's backing array of the first element of the buffer. If this buffer is backed by an array then buffer position p corresponds to array index p + arrayOffset(). Invoke the hasArray method before invok 3 min read Like