private int readVarInt ( int firstByte ) throws IOException { // VarInt uses the high-order bit of the last octet as a marker; some (but not all) 5-byte VarInts can fit // into a Java int. // To validate overflows we accumulate the VarInt in a long and then check if it can be represented by an int // // see http://amzn.github.io/ion-docs/docs/binary.html#varuint-and-varint-fields long retValue = 0 ; int b = firstByte ; boolean isNegative = false ; for ( ; ; ) { if ( b < 0 ) throwUnexpectedEOFException ( ) ; if ( ( b & 0x40 ) != 0 ) { isNegative = true ; } retValue = ( b & 0x3F ) ; if ( ( b & 0x80 ) != 0 ) break ; if ( ( b = read ( ) ) < 0 ) throwUnexpectedEOFException ( ) ; retValue = ( retValue << 7 ) | ( b & 0x7F ) ; if ( ( b & 0x80 ) != 0 ) break ; // for the rest, they're all the same if ( ( b = read ( ) ) < 0 ) throwUnexpectedEOFException ( ) ; retValue = ( retValue << 7 ) | ( b & 0x7F ) ; if ( ( b & 0x80 ) != 0 ) break ; // for the rest, they're all the same if ( ( b = read ( ) ) < 0 ) throwUnexpectedEOFException ( ) ; retValue = ( retValue << 7 ) | ( b & 0x7F ) ; if ( ( b & 0x80 ) != 0 ) break ; // for the rest, they're all the same if ( ( b = read ( ) ) < 0 ) throwUnexpectedEOFException ( ) ; retValue = ( retValue << 7 ) | ( b & 0x7F ) ; if ( ( b & 0x80 ) != 0 ) break ; // Don't support anything above a 5-byte VarInt for now, see https://github.com/amzn/ion-java/issues/146 throwVarIntOverflowException ( ) ; } if ( isNegative ) { retValue = - retValue ; } int retValueAsInt = ( int ) retValue ; if ( retValue != ( ( long ) retValueAsInt ) ) { throwVarIntOverflowException ( ) ; } return retValueAsInt ; }
reads a varInt after the first byte was read . The first byte is used to specify the sign and - 0 has different representation on the protected API that was called