/** * 读取变长的32位int变量 * * @param input * @return */ public static int readRawVarint32(SocketReadBuffer input) { byte tmp = input.readByte(); if ((tmp & 0x80) == 0) { return(tmp); } int result = tmp & 0x7f; tmp = input.readByte(); if ((tmp & 0x80) == 0) { result |= tmp << 7; } else { result |= (tmp & 0x7f) << 7; tmp = input.readByte(); if ((tmp & 0x80) == 0) { result |= tmp << 14; } else { result |= (tmp & 0x7f) << 14; tmp = input.readByte(); if ((tmp & 0x80) == 0) { result |= tmp << 21; } else { result |= (tmp & 0x7f) << 21; tmp = input.readByte(); result |= tmp << 28; if ((tmp & 0x80) != 0) { // Discard upper 32 bits. for (int i = 0; i < 5; i++) { if ((input.readByte() & 0x80) == 0) { return(result); } } // throw // InvalidProtocolBufferException.malformedVarint(); } } } } return(result); }
/** * 有符号int读取,变长 * * @param input * @return */ public static int readSInt(SocketReadBuffer input) { return(decodeZigZag32(readRawVarint32(input))); }