我试图了解b + Tree的实现。我不明白这个重载方法究竟是做什么的。为什么在以Inputsteam is
作为参数的第一种方法中声明了4个变量,即i1,i2,i3和i4。在使用ObjectInput in
作为参数的第二种方法中,我理解它返回0到255之间的字节,为什么结果= 251?解释每一行及其作用将会很有帮助。
第一种方法:
public final static int readLuposInt(final InputStream is) throws IOException {
final int i1 = is.read();
if (i1 < 0) {
return i1;
}
final int i2 = is.read();
if (i2 < 0) {
return i2;
}
final int i3 = is.read();
if (i3 < 0) {
return i3;
}
final int i4 = is.read();
if (i4 < 0) {
return i4;
}
return (0xFF & i1) | ((0xFF & i2) | ((0xFF & i3) | (0xFF & i4) << 8) << 8) << 8;
}
重载方法:
public final static int readLuposInt(final ObjectInput in) throws IOException {
final int i0 = in.read();
if (i0 <= 251){
return i0;
}
int result = 251;
int offset = 1;
for (int i = 1; i <= i0 - 251; i++) {
result += in.read() * offset;
offset <<= 8;
}
return result;
}
答案 0 :(得分:2)
您可以使用调试器查找以下结果。
第一种方法从输入流中读取4字节整数。它似乎存储为little-endian值。
-1
。示例:的
2293742
代表十六进制数22 FF EE
,将以相反的顺序存储:0xEE 0xFF 0x22 0x00
i1 = 0xEE
i2 = 0xFF
i3 = 0x22
i4 = 0x00
(0xFF & i4) << 8 = (0xFF & 0x00) << 8 = 0x0000
((0xFF & i3) | (0xFF & i4) << 8) << 8) = ((0x22 | 0x0000) << 8) = (0x0022 << 8) = 0x002200
((0xFF & i2) | ((0xFF & i3) | (0xFF & i4) << 8) << 8) << 8 = (0xFF | 0x002200) << 8 = 0x0022FF00
(0xFF & i1) | ((0xFF & i2) | ((0xFF & i3) | (0xFF & i4) << 8) << 8) << 8 = 0xEE | 0x0022FF00 = 0x0022FFEE
第二种方法从流中读取unicode字符,以UTF-8 编码进行编码。关于unicode及其角色编码可以说很多,请参阅Wikipedia这是如何工作的。