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


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


java.nio.ByteBuffer類的compact()方法用於壓縮給定的緩衝區。

緩衝區當前位置與其限製之間的字節(如果有)被複製到緩衝區的開頭。即,將索引p = position()處的字節複製到索引零,將索引p + 1處的字節複製到索引1,依此類推,直到索引limit() – 1處的字節複製到索引n = limit() – 1 – p。然後將緩衝區的位置設置為n + 1,並將其限製設置為其容量。如果定義了該標記,則將其丟棄。

緩衝區的位置設置為複製的字節數,而不是設置為零,以便在調用此方法之後可以立即調用另一個相對的put方法。


從緩衝區寫入數據後,如果寫入未完成,請調用此方法。

用法:

public abstract ByteBuffer compact()

返回值:此方法返回具有與此緩衝區相同內容的新ByteBuffer。

異常:如果此緩衝區是隻讀的,則此方法引發ReadOnlyBufferException。

以下示例程序旨在說明compact()方法:

範例1:

// Java program to demonstrate 
// compact() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 7; 
  
        // Creating the ByteBuffer 
  
        // 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); 
  
        // print the ByteBuffer 
        System.out.println("Original ByteBuffer: "
                           + Arrays.toString(bb.array())); 
  
        System.out.println("Position: " + bb.position()); 
  
        System.out.println("limit: " + bb.limit()); 
  
        // Creating a compacted  ByteBuffer of same ByteBuffer 
        // using compact() method 
        ByteBuffer cbb = bb.compact(); 
  
        // print the ByteBuffer 
        System.out.println("\nCompacted ByteBuffer: "
                           + Arrays.toString(cbb.array())); 
  
        System.out.println("Position: " + cbb.position()); 
  
        System.out.println("limit: " + cbb.limit()); 
  
        // putting the int to byte typcast value in compacted ByteBuffer 
        cbb.put((byte)50); 
  
        // print the ByteBuffer 
        System.out.println("\nUpdated Compacted ByteBuffer: "
                           + Arrays.toString(cbb.array())); 
        System.out.println("Position: " + cbb.position()); 
        System.out.println("limit: " + cbb.limit()); 
    } 
}
輸出:
Original ByteBuffer: [20, 30, 40, 0, 0, 0, 0]
Position: 3
limit: 7

Compacted ByteBuffer: [0, 0, 0, 0, 0, 0, 0]
Position: 4
limit: 7

Updated Compacted ByteBuffer: [0, 0, 0, 0, 50, 0, 0]
Position: 5
limit: 7

範例2:

// Java program to demonstrate 
// compact() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 5; 
  
        // 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.rewind(); 
  
            // Creating a read-only copy of ByteBuffer 
            // using asReadOnlyBuffer() method 
            ByteBuffer bb1 = bb.asReadOnlyBuffer(); 
  
            // print the ReadOnlyBuffer 
            System.out.print("ReadOnlyBuffer ByteBuffer: "); 
            while (bb1.hasRemaining()) 
                System.out.print(bb1.get() + ", "); 
            System.out.println(""); 
  
            // print the Position of ByteBuffer bb 
            System.out.println("\nPosition: " + bb.position()); 
  
            // print the Limit of ByteBuffer bb 
            System.out.println("\nlimit: " + bb.limit()); 
  
            // Creating a compacted  ByteBuffer of same ReadOnlyBuffer 
            // using compact() method 
            System.out.println("\nTrying to compact the ReadOnlyBuffer bb1"); 
            ByteBuffer rbb = bb1.compact(); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception throws " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws " + e); 
        } 
    } 
}
輸出:
ReadOnlyBuffer ByteBuffer: 20, 30, 40, 0, 0, 

Position: 0

limit: 5

Trying to compact the ReadOnlyBuffer bb1
Exception throws java.nio.ReadOnlyBufferException


相關用法


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