public static void encodeLength(ByteArrayOutputStream out, int length) {
        LOG.entering(CLASS_NAME, "encodeLength", new Object[] { out, length });
        int byteCount = 0;
        long encodedLength = 0;

        do {
            // left shift one byte to make room for new data
            encodedLength <<= 8;
            // set 7 bits of length
            encodedLength |= (byte) (length & 0x7f);
            // right shift out the 7 bits we just set
            length >>= 7;
            // increment the byte count that we need to encode
            byteCount++;
        }
        // continue if there are remaining set length bits
        while (length > 0);

        do {
            // get byte from encoded length
            byte encodedByte = (byte) (encodedLength & 0xff);
            // right shift encoded length past byte we just got
            encodedLength >>= 8;
            // The last length byte does not have the highest bit set
            if (byteCount != 1) {
                // set highest bit if this is not the last
                encodedByte |= (byte) 0x80;
            }
            // write encoded byte
            out.write(encodedByte);
        }
        // decrement and continue if we have more bytes left
        while (--byteCount > 0);
    }
public static void encodeLength ( ByteArrayOutputStream out , int length ) { LOG . entering ( CLASS_NAME , "encodeLength" , new Object [ ] { out , length } ) ; int byteCount = 0 ; long encodedLength = 0 ; do { encodedLength <<= 8 ; encodedLength |= ( byte ) ( length & 0x7f ) ; length >>= 7 ; byteCount ++ ; } while ( length > 0 ) ; do { byte encodedByte = ( byte ) ( encodedLength & 0xff ) ; encodedLength >>= 8 ; if ( byteCount != 1 ) { encodedByte |= ( byte ) 0x80 ; } out . write ( encodedByte ) ; } while ( -- byteCount > 0 ) ; }
/*
Length-bytes are written out in order from most to least significant, but are computed most efficiently (using bit shifts)
from least to most significant. An integer serves as a temporary storage, which is then written out in reversed order.