📜  Java中的 ByteBuffer compact() 方法及示例

📅  最后修改于: 2022-05-13 01:55:05.610000             🧑  作者: Mango

Java中的 ByteBuffer compact() 方法及示例

Java.nio.ByteBuffer类的compact()方法用于压缩给定的缓冲区。
缓冲区的当前位置和它的限制之间的字节(如果有的话)被复制到缓冲区的开头。也就是说,索引 p = position() 处的字节被复制到索引 0,索引 p + 1 处的字节被复制到索引 1,依此类推,直到索引 limit() – 1 处的字节被复制到索引 n =限制() - 1 - 页。然后将缓冲区的位置设置为 n+1,并将其限制设置为其容量。标记(如果已定义)将被丢弃。
缓冲区的位置设置为复制的字节数,而不是零,以便调用此方法后可以立即调用另一个相对 put 方法。
从缓冲区写入数据后调用此方法,以防写入不完整。
句法 :

public abstract ByteBuffer compact()

返回值:该方法返回与本缓冲区内容相同的新ByteBuffer。
异常:此方法抛出 ReadOnlyBufferException,如果此缓冲区是只读的。
下面的程序说明了 compact() 方法:
示例 1:

Java
// 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 typecast 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());
    }
}


Java
// 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);
        }
    }
}


输出:
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

// 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