java.nio.ByteBuffer類的slice()方法用於創建一個新的字節緩衝區,其內容是給定緩衝區內容的共享子序列。
新緩衝區的內容將從該緩衝區的當前位置開始。對該緩衝區內容的更改將在新緩衝區中可見,反之亦然。這兩個緩衝區的位置,限製和標記值將是獨立的。
新緩衝區的位置將為零,其容量和限製將為該緩衝區中剩餘的浮點數,並且其標記將不確定。當且僅當該緩衝區是直接緩衝區時,新緩衝區才是直接緩衝區;當且僅當該緩衝區是隻讀緩衝區時,新緩衝區才是隻讀緩衝區。
用法:
public abstract ByteBuffer slice()
返回值:此方法返回新的字節緩衝區。
下麵是說明slice()方法的示例:
範例1:
// Java program to demonstrate
// slice() 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 bb1
= ByteBuffer.allocate(capacity);
// putting the value in ByteBuffer
bb1.put((byte)10);
bb1.put((byte)20);
// print the ByteBuffer
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb1.array()));
// print the ByteBuffer position
System.out.println("\nposition: "
+ bb1.position());
// print the ByteBuffer capacity
System.out.println("\ncapacity: "
+ bb1.capacity());
// Creating a shared subsequence buffer
// of given ByteBuffer
// using slice() method
ByteBuffer bb2 = bb1.slice();
// print the shared subsequance buffer
System.out.println("\nshared subsequance ByteBuffer: "
+ Arrays.toString(bb2.array()));
// print the ByteBuffer position
System.out.println("\nposition: " + bb2.position());
// print the ByteBuffer capacity
System.out.println("\ncapacity: " + bb2.capacity());
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
輸出:
Original ByteBuffer: [10, 20, 0, 0, 0] position: 2 capacity: 5 shared subsequance ByteBuffer: [10, 20, 0, 0, 0] position: 0 capacity: 3
範例2:
// Java program to demonstrate
// slice() 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 bb1 = ByteBuffer.allocate(capacity);
// putting the value in ByteBuffer
bb1.put((byte)10)
.put((byte)20)
.put((byte)30)
.put((byte)40)
.put((byte)50);
// print the ByteBuffer
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb1.array()));
// print the ByteBuffer position
System.out.println("\nposition: "
+ bb1.position());
// print the ByteBuffer capacity
System.out.println("\ncapacity: "
+ bb1.capacity());
// Creating a shared subsequence buffer
// of given ByteBuffer
// using slice() method
ByteBuffer bb2 = bb1.slice();
// print the shared subsequance buffer
System.out.println("\nshared subsequance ByteBuffer: "
+ Arrays.toString(bb2.array()));
// print the ByteBuffer position
System.out.println("\nposition: " + bb2.position());
// print the ByteBuffer capacity
System.out.println("\ncapacity: " + bb2.capacity());
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
輸出:
Original ByteBuffer: [10, 20, 30, 40, 50] position: 5 capacity: 5 shared subsequance ByteBuffer: [10, 20, 30, 40, 50] position: 0 capacity: 0
參考: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#slice–
相關用法
- Java ByteBuffer get()用法及代碼示例
- Java ByteBuffer getShort()用法及代碼示例
- Java ByteBuffer getLong()用法及代碼示例
- Java ByteBuffer getInt()用法及代碼示例
- Java ByteBuffer getDouble()用法及代碼示例
- Java ByteBuffer getChar()用法及代碼示例
- Java ByteBuffer asDoubleBuffer()用法及代碼示例
- Java ByteBuffer arrayOffset()用法及代碼示例
- Java ByteBuffer asLongBuffer()用法及代碼示例
- Java ByteBuffer asShortBuffer()用法及代碼示例
- Java ByteBuffer getFloat()用法及代碼示例
- Java ByteBuffer toString()用法及代碼示例
- Java ByteBuffer hashCode()用法及代碼示例
- Java ByteBuffer order()用法及代碼示例
- Java ByteBuffer equals()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ByteBuffer slice() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。