private static void writeLength ( ByteBuffer buf , int length ) { assert ( length >= 0 ) ; assert ( length == ( length & ( ~ length + 1 ) ) ) ; // check if power of two // shockingly fast log_2 uses intrinsics in JDK >= 1.7 byte log2size = ( byte ) ( 32 - Integer . numberOfLeadingZeros ( length ) ) ; buf . put ( log2size ) ; }
Over - clever way to store lots of lengths in a single byte Length must be a power of two > = 0 .