Tuesday, October 7, 2025
HomeLanguagesJavaShortBuffer asReadOnlyBuffer() method in Java with Examples

ShortBuffer asReadOnlyBuffer() method in Java with Examples

The asReadOnlyBuffer() method of java.nio.ShortBuffer Class is used to create a new, read-only short buffer with this buffer’s content. The new buffer is a replica of this buffer. Hence changes made to this buffer’s content will be visible in the new buffer. Since the new buffer is read-only, therefore any modification to its content won’t be allowed. The two buffers’ position, limit, and mark values will be independent. The new buffer’s capacity, limit, position, and mark values will be identical to those of this buffer. If this buffer is itself read-only then this method behaves in exactly the same way as the duplicate method. Syntax :

public abstract ShortBuffer asReadOnlyBuffer()

Return Value:This method returns the new, read-only short buffer with the same content as that of this buffer. Below are the examples to illustrate the asReadOnlyBuffer() method: Program 1

Java




// Java program to demonstrate
// asReadOnlyBuffer() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ShortBuffer
        int capacity = 10;
 
        // Creating the ShortBuffer
        try {
 
            // creating object of shortbuffer
            // and allocating size capacity
            ShortBuffer sb = ShortBuffer.allocate(capacity);
 
            // putting the value in shortbuffer
            sb.put((short)856);
            sb.put(2, (short)961);
            sb.rewind();
 
            // print the ShortBuffer
            System.out.println("Original ShortBuffer: "
                               + Arrays.toString(sb.array()));
 
            // Creating a read-only copy of ShortBuffer
            // using asReadOnlyBuffer() method
            ShortBuffer shortBuffer = sb.asReadOnlyBuffer();
 
            // print the ShortBuffer
            System.out.print("\nReadOnlyBuffer ShortBuffer: ");
 
            while (shortBuffer.hasRemaining())
                System.out.print(shortBuffer.get() + ", ");
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
 
        catch (ReadOnlyBufferException e) {
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}


Output:

Original ShortBuffer: [856, 0, 961, 0, 0, 0, 0, 0, 0, 0]

ReadOnlyBuffer ShortBuffer: 856, 0, 961, 0, 0, 0, 0, 0, 0, 0,

Program 2

Java




// Java program to demonstrate
// asReadOnlyBuffer() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args) throws Exception
    {
 
        // Declaring the capacity of the sb
        int capacity1 = 10;
 
        // Declaring the capacity of the sb1
        int capacity2 = 5;
 
        // Creating the ShortBuffer
        try {
 
            //
            // sb
            //
            // creating object of shortbuffer sb
            // and allocating size capacity
            ShortBuffer sb = ShortBuffer.allocate(capacity1);
 
            // putting the value in sb
            sb.put((short)856);
            sb.put(2, (short)961);
            sb.rewind();
 
            // print the ShortBuffer
            System.out.println("ShortBuffer sb: "
                               + Arrays.toString(sb.array()));
 
            //
            // sb1
            //
            // creating object of shortbuffer sb1
            // and allocating size capacity
            ShortBuffer sb1 = ShortBuffer.allocate(capacity2);
 
            // putting the value in sb1
            sb1.put(1, (short)456);
            sb1.put(2, (short)645);
            sb1.rewind();
 
            // print the ShortBuffer
            System.out.println("\nShortBuffer sb1: "
                               + Arrays.toString(sb1.array()));
 
            // Creating a read-only copy of ShortBuffer
            // using asReadOnlyBuffer() method
            ShortBuffer readOnlySb = sb.asReadOnlyBuffer();
 
            // print the ShortBuffer
            System.out.print("\nReadOnlyBuffer FloatBuffer: ");
 
            while (readOnlySb.hasRemaining())
                System.out.print(readOnlySb.get() + ", ");
 
            // try to change readOnlySb
            System.out.println("\n\nTrying to get the array"
                               + " from ReadOnlysb for editing");
 
            short[] sbarr = readOnlySb.array();
        }
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}


Output:

ShortBuffer sb: [856, 0, 961, 0, 0, 0, 0, 0, 0, 0]

ShortBuffer sb1: [0, 456, 645, 0, 0]

ReadOnlyBuffer FloatBuffer: 856, 0, 961, 0, 0, 0, 0, 0, 0, 0, 

Trying to get the array from ReadOnly for editing
Exception thrown: java.nio.ReadOnlyBufferException
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6709 POSTS0 COMMENTS
Nicole Veronica
11873 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7091 POSTS0 COMMENTS
Thapelo Manthata
6781 POSTS0 COMMENTS
Umr Jansen
6785 POSTS0 COMMENTS