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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
ShortBuffer sb = ShortBuffer.allocate(capacity);
sb.put(( short ) 856 );
sb.put( 2 , ( short ) 961 );
sb.rewind();
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb.array()));
ShortBuffer shortBuffer = sb.asReadOnlyBuffer();
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args) throws Exception
{
int capacity1 = 10 ;
int capacity2 = 5 ;
try {
ShortBuffer sb = ShortBuffer.allocate(capacity1);
sb.put(( short ) 856 );
sb.put( 2 , ( short ) 961 );
sb.rewind();
System.out.println("ShortBuffer sb: "
+ Arrays.toString(sb.array()));
ShortBuffer sb1 = ShortBuffer.allocate(capacity2);
sb1.put( 1 , ( short ) 456 );
sb1.put( 2 , ( short ) 645 );
sb1.rewind();
System.out.println("\nShortBuffer sb1: "
+ Arrays.toString(sb1.array()));
ShortBuffer readOnlySb = sb.asReadOnlyBuffer();
System.out.print("\nReadOnlyBuffer FloatBuffer: ");
while (readOnlySb.hasRemaining())
System.out.print(readOnlySb.get() + ", ");
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...