public static int varIntBytesToInt ( byte [ ] bytes , int offset ) { byte tmp = bytes [ offset ++ ] ; if ( tmp >= 0 ) { return tmp ; } int result = tmp & 0x7f ; if ( ( tmp = bytes [ offset ++ ] ) >= 0 ) { result |= tmp << 7 ; } else { result |= ( tmp & 0x7f ) << 7 ; if ( ( tmp = bytes [ offset ++ ] ) >= 0 ) { result |= tmp << 14 ; } else { result |= ( tmp & 0x7f ) << 14 ; if ( ( tmp = bytes [ offset ++ ] ) >= 0 ) { result |= tmp << 21 ; } else { result |= ( tmp & 0x7f ) << 21 ; result |= ( tmp = bytes [ offset ++ ] ) << 28 ; if ( tmp < 0 ) { // Discard upper 32 bits. for ( int i = 0 ; i < 5 ; i ++ ) { if ( bytes [ offset ++ ] >= 0 ) { return result ; } } //Should never happen since we wrote the varint value. If this occurs due to an internal bug //this exception is caught and wrapped further up the chain. throw new IllegalStateException ( "Varint representation is invalid or exceeds 32-bit value" ) ; } } } } return result ; }
Reads an int from the byte array in the Varint format .