private void writeVarInt()

in src/com/amazon/ion/impl/lite/ReverseBinaryEncoder.java [536:619]


    private void writeVarInt(int v)
    {
        if (v == 0)
        {
            writeByte(0x80);
        }
        else
        {
            int offset = myOffset;

            boolean is_negative = (v < 0);
            if (is_negative)
            {
                // note that for Integer.MIN_VALUE (0x80000000) the negative
                // is the same, but that's also the bit pattern we need to
                // write out - so no worries
                v = -v;
            }

            if (v < (1 << (7 * 1 - 1)))           // 1 byte - 6 bits used - 0x3f max
            {
                if (--offset < 0) {
                    offset = growBuffer(offset);
                }
                if (is_negative)
                    v |= 0x40;
                myBuffer[offset]     = (byte) (v | 0x80);
            }
            else if (v < (1 << (7 * 2 - 1)))      // 2 bytes - 13 bits used - 0x1fff max
            {
                if ((offset -= 2) < 0) {
                    offset = growBuffer(offset);
                }
                if (is_negative)
                    v |= 0x2000;
                myBuffer[offset]     = (byte) (v >>> (7 * 1));
                myBuffer[offset + 1] = (byte) (v | 0x80);
            }
            else if (v < (1 << (7 * 3 - 1)))      // 3 bytes - 20 bits used - 0xfffff max
            {
                if ((offset -= 3) < 0) {
                    offset = growBuffer(offset);
                }
                if (is_negative)
                    v |= 0x100000;
                myBuffer[offset]     = (byte) ( v >>> (7 * 2));
                myBuffer[offset + 1] = (byte) ((v >>> (7 * 1)) & 0x7f);
                myBuffer[offset + 2] = (byte) ( v | 0x80);
            }
            else if (v < (1 << (7 * 4 - 1)))      // 4 bytes - 27 bits used - 0x7ffffff max
            {
                if ((offset -= 4) < 0) {
                    offset = growBuffer(offset);
                }
                if (is_negative)
                    v |= 0x8000000;
                myBuffer[offset]     = (byte) ( v >>> (7 * 3));
                myBuffer[offset + 1] = (byte) ((v >>> (7 * 2)) & 0x7f);
                myBuffer[offset + 2] = (byte) ((v >>> (7 * 1)) & 0x7f);
                myBuffer[offset + 3] = (byte) ( v | 0x80);
            }
            else                                  // 5 bytes - 31 bits used - 0x7fffffff max (Integer.MAX_VALUE)
            {
                if ((offset -= 5) < 0) {
                    offset = growBuffer(offset);
                }

                // This is different from the previous if-blocks because we
                // cannot represent a int with more than 32 bits to perform
                // the "OR-assignment" (|=).
                myBuffer[offset]     = (byte) ((v >>> (7 * 4)) & 0x7f);
                if (is_negative) {
                    myBuffer[offset] |= 0x40;
                }

                myBuffer[offset + 1] = (byte) ((v >>> (7 * 3)) & 0x7f);
                myBuffer[offset + 2] = (byte) ((v >>> (7 * 2)) & 0x7f);
                myBuffer[offset + 3] = (byte) ((v >>> (7 * 1)) & 0x7f);
                myBuffer[offset + 4] = (byte) ( v | 0x80);
            }

            myOffset = offset;
        }
    }