Java ByteBuffer compact()方法及实例
java.nio.ByteBuffer 类的 compact() 方法用于压缩给定的缓冲区。
缓冲区的当前位置和其极限之间的字节(如果有的话)被复制到缓冲区的开头。也就是说,索引p=position()的字节被复制到索引0,索引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 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());
}
}
输出
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
极客教程