java.io
Bits
使用大端字节顺序在字节数组中打包/解包原始值的实用程序方法。
big-endian:大端模式:指数据的高位保存在内存的低地址处,数据的低位保存在内存的高地址处。
小端模式:数据的高位保存在内存的高地址处,数据的低位保存在低地址处。
package java.io;
/**
* Utility methods for packing/unpacking primitive values in/out of byte arrays
* using big-endian byte ordering.
* 包内使用
*/
class Bits {
/*
* Methods for unpacking primitive values from byte arrays starting at given offsets.
* 用于从给定偏移量开始的字节数组中解包原始值的方法。
* 首先分析第一个方法,仅仅用于获取byte数组中off坐标下是否存在不为0的数据,因为boolean类型只占1位,即1bit
*/
static boolean getBoolean(byte[] b, int off) {
return b[off] != 0;
}
/*
*1byte = 8bit。1字节=8位
*1char = 16位(bit)
*/
static char getChar(byte[] b, int off) {
return (char) ((b[off + 1] & 0xFF) +
(b[off] << 8));
}
/*
*1byte = 8bit。
*1short = 16位(bit)
*/
static short getShort(byte[] b, int off) {
return (short) ((b[off + 1] & 0xFF) +
(b[off] << 8));
}
/*
*1byte = 8bit。
*1short = 32位(bit)
*/
static int getInt(byte[] b, int off) {
return ((b[o