private static void writeVLong ( DataOutput stream , long i ) throws IOException { if ( i >= - 112 && i <= 127 ) { stream . writeByte ( ( byte ) i ) ; return ; } int len = - 112 ; if ( i < 0 ) { i ^= - 1L ; // take one's complement' len = - 120 ; } long tmp = i ; while ( tmp != 0 ) { tmp = tmp >> 8 ; len -- ; } stream . writeByte ( ( byte ) len ) ; len = ( len < - 120 ) ? - ( len + 120 ) : - ( len + 112 ) ; for ( int idx = len ; idx != 0 ; idx -- ) { int shiftbits = ( idx - 1 ) * 8 ; long mask = 0xFF L << shiftbits ; stream . writeByte ( ( byte ) ( ( i & mask ) >> shiftbits ) ) ; } }
From org . apache . hadoop . io . WritableUtis