java.nio.CharBuffer类的asReadOnlyBuffer()方法用于使用该缓冲区的内容创建一个新的只读char缓冲区。新缓冲区是此缓冲区的副本。因此,对该缓冲区内容所做的更改将在新缓冲区中可见。
由于新缓冲区是只读缓冲区,因此不允许对其内容进行任何修改。这两个缓冲区的位置,限制和标记值将是独立的。新缓冲区的容量,限制,位置和标记值将与此缓冲区相同。如果此缓冲区本身是只读的,则此方法的行为与重复方法完全相同。
用法:
public abstract CharBuffer asReadOnlyBuffer()
返回值:此方法返回新的只读char缓冲区,其内容与此缓冲区相同。
下面是说明asReadOnlyBuffer()方法的示例:
范例1:
// 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 CharBuffer
int capacity = 10;
// Creating the CharBuffer
try {
// creating object of charbuffer
// and allocating size capacity
CharBuffer cb = CharBuffer.allocate(capacity);
// putting the value in charbuffer
cb.put('a');
cb.put(2, 'b');
cb.rewind();
// print the charBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(cb.array()));
// Creating a read-only copy of CharBuffer
// using asReadOnlyBuffer() method
CharBuffer charBuffer = cb.asReadOnlyBuffer();
// print the CharBuffer
System.out.print("\nReadOnlyBuffer CharBuffer: ");
while (charBuffer.hasRemaining())
System.out.print(charBuffer.get() + ", ");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
输出:
Original CharBuffer: [a, , b, , , , , , , ] ReadOnlyBuffer CharBuffer: a, , b, , , , , , , ,
范例2:
// 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 cb
int capacity1 = 10;
// Declaring the capacity of the cb1
int capacity2 = 5;
// Creating the CharBuffer
try {
//
// cb
//
// creating object of charbuffer cb
// and allocating size capacity
CharBuffer cb = CharBuffer.allocate(capacity1);
// putting the value in cb
cb.put('a');
cb.put(2, 'b');
cb.rewind();
// print the CharBuffer
System.out.println("CharBuffer cb: "
+ Arrays.toString(cb.array()));
//
// cb1
//
// creating object of charbuffer cb1
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity2);
// putting the value in cb1
cb1.put(1, 'c');
cb1.put(2, 'd');
cb1.rewind();
// print the CharBuffer
System.out.println("\nCharBuffer cb1: "
+ Arrays.toString(cb1.array()));
// Creating a read-only copy of CharBuffer
// using asReadOnlyBuffer() method
CharBuffer readOnlyCb = cb.asReadOnlyBuffer();
// print the CharBuffer
System.out.print("\nReadOnlyBuffer CharBuffer: ");
while (readOnlyCb.hasRemaining())
System.out.print(readOnlyCb.get() + ", ");
// try to change readOnlyCb
System.out.println("\n\nTrying to get the array"
+ " from ReadOnlyCb for editing");
char[] fbarr = readOnlyCb.array();
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception thrown: " + e);
}
}
}
输出:
CharBuffer cb: [a, , b, , , , , , , ] CharBuffer cb1: [ , c, d, , ] ReadOnlyBuffer CharBuffer: a, , b, , , , , , , , Trying to get the array from ReadOnlyCb for editing Exception thrown: java.nio.ReadOnlyBufferException
相关用法
- Java LongBuffer asReadOnlyBuffer()用法及代码示例
- Java IntBuffer asReadOnlyBuffer()用法及代码示例
- Java ByteBuffer asReadOnlyBuffer()用法及代码示例
- Java ShortBuffer asReadOnlyBuffer()用法及代码示例
- Java DoubleBuffer asReadOnlyBuffer()用法及代码示例
- Java FloatBuffer asReadOnlyBuffer()用法及代码示例
- Java CharBuffer arrayOffset()用法及代码示例
- Java CharBuffer array()用法及代码示例
- Java CharBuffer compact()用法及代码示例
- Java CharBuffer compareTo()用法及代码示例
- Java CharBuffer equals()用法及代码示例
- Java CharBuffer hasArray()用法及代码示例
- Java CharBuffer duplicate()用法及代码示例
- Java CharBuffer wrap()用法及代码示例
- Java CharBuffer slice()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 CharBuffer asReadOnlyBuffer() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。