java的duplicate用法_Java ByteBuffer duplicate()用法及代码示例
java.nio.ByteBuffer類的duplicate()方法用于創(chuàng)建共享該緩沖區(qū)內(nèi)容的新字節(jié)緩沖區(qū)。
新緩沖區(qū)的內(nèi)容就是該緩沖區(qū)的內(nèi)容。對該緩沖區(qū)內(nèi)容的更改將在新緩沖區(qū)中可見,反之亦然;這兩個緩沖區(qū)的位置,限制和標(biāo)記值將是獨立的。
新緩沖區(qū)的容量,限制,位置和標(biāo)記值將與此緩沖區(qū)相同。當(dāng)且僅當(dāng)該緩沖區(qū)是直接緩沖區(qū)時,新緩沖區(qū)才是直接緩沖區(qū);當(dāng)且僅當(dāng)該緩沖區(qū)是只讀緩沖區(qū)時,新緩沖區(qū)才是只讀緩沖區(qū)。
用法:
public abstract ByteBuffer duplicate()
返回值:此方法返回承載先前字節(jié)緩沖區(qū)內(nèi)容的新字節(jié)緩沖區(qū)。
下面是說明duplicate()方法的示例:
范例1:使用直接ByteBuffer
// Java program to demonstrate
// duplicate() method
// Using direct ByteBuffer
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 bb1 = ByteBuffer.allocate(capacity);
// putting the int to byte typecast value value in ByteBuffer
bb1.put((byte)20);
bb1.put((byte)30);
bb1.put((byte)40);
bb1.put((byte)50);
bb1.rewind();
// print the Original ByteBuffer
System.out.println("Original ByteBuffer:? "
+ Arrays.toString(bb1.array()));
// Creating a duplicate copy of ByteBuffer
// using duplicate() method
ByteBuffer bb2 = bb1.duplicate();
// print the duplicate copy of ByteBuffer
System.out.print("\nDuplicate ByteBuffer: "
+ Arrays.toString(bb2.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]
Duplicate ByteBuffer: [20, 30, 40, 50]
范例2:使用只讀ByteBuffer
// Java program to demonstrate
// duplicate() method
// using read-only ByteBuffer
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 bb1 = ByteBuffer.allocate(capacity);
// putting the int to byte typecast value in ByteBuffer
bb1.put((byte)20);
bb1.put((byte)30);
bb1.put((byte)40);
bb1.put((byte)50);
bb1.rewind();
// print the Original ByteBuffer
System.out.println("Original ByteBuffer:? "
+ Arrays.toString(bb1.array()));
// Creating a read-only copy of ByteBuffer
// using asReadOnlyBuffer() method
ByteBuffer readonly = bb1.asReadOnlyBuffer();
// print the read-only copy of ByteBuffer
System.out.print("\nRead-only ByteBuffer:? ");
while (readonly.hasRemaining())
System.out.print(readonly.get() + ", ");
System.out.println("");
// Rewinding the readonly ByteBuffer
readonly.rewind();
// Creating a duplicate copy of ByteBuffer
// using duplicate() method
ByteBuffer bb2 = readonly.duplicate();
// print the duplicate copy of ByteBuffer
System.out.print("\nDuplicate copy of read-only ByteBuffer:? ");
while (bb2.hasRemaining())
System.out.print(bb2.get() + ", ");
System.out.println("");
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
Original ByteBuffer: [20, 30, 40, 50]
Read-only ByteBuffer: 20, 30, 40, 50,
Duplicate copy of read-only ByteBuffer: 20, 30, 40, 50,
總結(jié)
以上是生活随笔為你收集整理的java的duplicate用法_Java ByteBuffer duplicate()用法及代码示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 含辞未吐,声若幽兰,史上最强免费人工智能
- 下一篇: JPEG 原理分析及 JPEG 解码器的