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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。