e162. getting and setting non-byte java types in a bytebuffer.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 24 行
TXT
24 行
The ByteBuffer class provides convenience methods for getting and putting other multibyte Java primitive types. There are two issues to be aware of when using these methods. First, ensure that values will be stored using the desired byte ordering; see e165 Setting the Byte Ordering for a ByteBuffer for more information. Second, the hasRemaining() method cannot be used to determine if the buffer has room for a multibyte put. If your application needs to know this information, see e163 Creating a Non-Byte Java Type Buffer on a ByteBuffer for an example that can provide this information.
// Obtain a ByteBuffer; see also e158 Creating a ByteBuffer
ByteBuffer buf = ByteBuffer.allocate(100);
// Put values of different types
buf.putChar((char)123);
buf.putShort((short)123);
buf.putInt(123);
buf.putLong(123L);
buf.putFloat(12.3F);
buf.putDouble(12.3D);
// Reset position for reading
buf.flip();
// Retrieve the values
char c = buf.getChar();
short s = buf.getShort();
int i = buf.getInt();
long l = buf.getLong();
float f = buf.getFloat();
double d = buf.getDouble();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?