当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。