當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ByteBuffer asReadOnlyBuffer()用法及代碼示例


java.nio.ByteBuffer類的asReadOnlyBuffer()方法用於創建共享該緩衝區內容的新的隻讀字節緩衝區。

新緩衝區的內容就是該緩衝區的內容。對該緩衝區內容的更改將在新緩衝區中可見;但是,新緩衝區本身將是隻讀的,並且不允許修改共享內容。這兩個緩衝區的位置,限製和標記值將是獨立的。

新緩衝區的容量,限製,位置和標記值將與此緩衝區相同。


如果此緩衝區本身是隻讀的,則此方法的行為與重複方法完全相同。

用法:

public abstract ByteBuffer asReadOnlyBuffer()

返回值:此方法返回新的隻讀字節緩衝區,其內容與此緩衝區的內容相同。

下麵是說明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 ByteBuffer 
        int capacity = 4; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the int to byte typecast value in ByteBuffer 
            bb.put((byte)20); 
            bb.put((byte)30); 
            bb.put((byte)40); 
            bb.put((byte)50); 
            bb.rewind(); 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array())); 
  
            // Creating a read-only copy of ByteBuffer 
            // using asReadOnlyBuffer() method 
            ByteBuffer bb1 = bb.asReadOnlyBuffer(); 
  
            // print the ByteBuffer 
            System.out.print("\nReadOnlyBuffer ByteBuffer: "); 
  
            while (bb1.hasRemaining()) 
                System.out.print(bb1.get() + ", "); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer:  [20, 30, 40, 50]

ReadOnlyBuffer ByteBuffer: 20, 30, 40, 50,

範例2:

// 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 ByteBuffer 
        int capacity = 4; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the int to byte typecast value in ByteBuffer 
            bb.put((byte)20); 
            bb.put((byte)30); 
            bb.put((byte)40); 
            bb.put((byte)50); 
            bb.rewind(); 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array())); 
  
            // Creating a read-only copy of ByteBuffer 
            // using asReadOnlyBuffer() method 
            ByteBuffer bb1 = bb.asReadOnlyBuffer(); 
  
            // print the ByteBuffer 
            System.out.print("\nReadOnlyBuffer ByteBuffer: "); 
  
            while (bb1.hasRemaining()) 
                System.out.print(bb1.get() + ", "); 
  
            // try to change read only bytebuffer 
            System.out.println("\n\nTrying to get the array"
                               + " from bb1 for editing"); 
  
            byte[] bbarr = bb1.array(); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer:  [20, 30, 40, 50]

ReadOnlyBuffer ByteBuffer: 20, 30, 40, 50, 

Trying to get the array from bb1 for editing
Exception thrown : java.nio.ReadOnlyBufferException


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ByteBuffer asReadOnlyBuffer() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。