ByteBuffer 的 get() 方法
在 java.nio.ByteBuffer 类的 get() 方法中可以读取并增量缓冲区的当前位置的值。此方法将返回在当前缓冲区位置上的值。此外,如果出现下溢情况,将抛出 BufferUnderflowException。
以下是展示此方法的程序:
示例
import java.nio.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
int n = 5;
try {
ByteBuffer buffer = ByteBuffer.allocate(n);
buffer.put((byte)1);
buffer.put((byte)2);
buffer.put((byte)3);
buffer.put((byte)4);
buffer.put((byte)5);
buffer.rewind();
System.out.println("The ByteBuffer is: " + Arrays.toString(buffer.array()));
byte val1 = buffer.get();
System.out.println("
The value at current position of ByteBuffer is: " + val1);
byte val2 = buffer.get();
System.out.println("The value at next position of ByteBuffer is: " + val2);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
} catch (BufferUnderflowException e) {
System.out.println("Error!!! BufferUnderflowException");
}
}
}输出
The ByteBuffer is: [1, 2, 3, 4, 5] The value at current position of ByteBuffer is: 1 The value at next position of ByteBuffer is: 2
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP