ShortBuffer asReadOnlyBuffer() method in Java with Examples
Last Updated :
13 Feb, 2023
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
Similar Reads
ShortBuffer arrayOffset() method in Java With Examples The arrayOffset() method of java.nio.ShortBuffer class is used to return the offset within the bufferâs backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). In-order to check whether
3 min read
ShortBuffer array() Method in Java with Examples The array() method of java.nio.ShortBuffer is used to return the short array that backs this buffer(optional). 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
3 min read
ShortBuffer allocate() method in Java With Examples The allocate() method of java.nio.ShortBuffer Class is used to allocate a new short buffer.The position of the new Buffer will be zero & it's limit is its capacity, though the mark is undefined, and each of its elements is initialized to zero. It will be having a backing array, and the array off
2 min read
ShortBuffer duplicate() method in Java With Examples The duplicate() method of java.nio.ShortBuffer Class creates a new short buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark
3 min read
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 asReadOnlyBuffer() method in Java with Examples The asReadOnlyBuffer() method of java.nio.ByteBuffer class is used to create a new, read-only byte buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer; the new buffer itself, however,
3 min read