java/proto/src/main/java/com/pravala/protocol/Codec.java
public static void appendField ( OutputStream toStream, Integer value, int fieldId )
    throws IOException, CodecException
    {
        if ( fieldId < 0 )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }

        if ( value == null || value == 0 )
        {
            appendFieldHeader ( toStream, fieldId, WIRE_TYPE_ZERO, 0 );
            return;
        }

        byte wireType;
        int dataSize = 0;

        if ( value < 0 )
        {
            value = -value;
            wireType = WIRE_TYPE_VAR_LENGTH_B;
        }
        else if ( ( value & 0xFF ) == value )
        {
            dataSize = 1;
            wireType = WIRE_TYPE_1_BYTE;
        }
        else if ( ( value & 0xFFFF ) == value )
        {
            dataSize = 2;
            wireType = WIRE_TYPE_2_BYTES;
        }
        else if ( ( value & 0x1FFFFF ) == value )
        {
            // We can fit the value in 3 bytes using variable length encoding
            dataSize = 3;
            wireType = WIRE_TYPE_VAR_LENGTH_A;
        }
        else
        {
            dataSize = 4;
            wireType = WIRE_TYPE_4_BYTES;
        }

        appendFieldHeader ( toStream, fieldId, wireType, dataSize );

        if ( wireType == WIRE_TYPE_VAR_LENGTH_A || wireType == WIRE_TYPE_VAR_LENGTH_B )
        {
            while ( value != 0 )
            {
                byte byteVal = ( byte ) ( value & 0x7F );

                // & part is to get rid of negative bit being shifted and replicated
                value = ( value >> 7 ) & 0x1FFFFFF;

                if ( value != 0 )
                {
                    byteVal |= 0x80;
                }

                toStream.write ( byteVal );
            }
        }
        else
        {
            for ( int i = 0; i < dataSize; ++i )
            {
                toStream.write ( ( byte ) ( value & 0xFF ) );

                value = value >> 8;
            }
        }
    }
/**
     *  Writes a single value to the stream
     *
     * @param toStream The stream to append the data to
     * @param value The value to append to the stream
     * @param fieldId ID of the field to store in the header
     *
     * @throws IOException Thrown by OutputStream.write, CodecException
     **/
Writes a single value to the stream
@param toStream The stream to append the data to
@param value The value to append to the stream
@param fieldId ID of the field to store in the header


Writes a single value to the stream @param toStream The stream to append the data to @param value The value to append to the stream @param fieldId ID of the field to store in the header
public static void appendField ( OutputStream toStream, Integer value, int fieldId )
    throws IOException, CodecException
    {
        if ( fieldId < 0 )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }
        if ( value == null || value == 0 )
        {
            appendFieldHeader ( toStream, fieldId, WIRE_TYPE_ZERO, 0 );
            return;
        }
        byte wireType;
        int dataSize = 0;
        if ( value < 0 )
        {
            value = -value;
            wireType = WIRE_TYPE_VAR_LENGTH_B;
        }
        else if ( ( value & 0xFF ) == value )
        {
            dataSize = 1;
            wireType = WIRE_TYPE_1_BYTE;
        }
        else if ( ( value & 0xFFFF ) == value )
        {
            dataSize = 2;
            wireType = WIRE_TYPE_2_BYTES;
        }
        else if ( ( value & 0x1FFFFF ) == value )
        {
            dataSize = 3;
            wireType = WIRE_TYPE_VAR_LENGTH_A;
        }
        else
        {
            dataSize = 4;
            wireType = WIRE_TYPE_4_BYTES;
        }
        appendFieldHeader ( toStream, fieldId, wireType, dataSize );
        if ( wireType == WIRE_TYPE_VAR_LENGTH_A || wireType == WIRE_TYPE_VAR_LENGTH_B )
        {
            while ( value != 0 )
            {
                byte byteVal = ( byte ) ( value & 0x7F );
                value = ( value >> 7 ) & 0x1FFFFFF;
                if ( value != 0 )
                {
                    byteVal |= 0x80;
                }
                toStream.write ( byteVal );
            }
        }
        else
        {
            for ( int i = 0; i < dataSize; ++i )
            {
                toStream.write ( ( byte ) ( value & 0xFF ) );
                value = value >> 8;
            }
        }
    }
public static void appendField ( OutputStream toStream , Integer value , int fieldId ) throws IOException , CodecException { if ( fieldId < 0 ) { throw new CodecException ( ErrorCode . InvalidParameter ) ; } if ( value == null || value == 0 ) { appendFieldHeader ( toStream , fieldId , WIRE_TYPE_ZERO , 0 ) ; return ; } byte wireType ; int dataSize = 0 ; if ( value < 0 ) { value = - value ; wireType = WIRE_TYPE_VAR_LENGTH_B ; } else if ( ( value & 0xFF ) == value ) { dataSize = 1 ; wireType = WIRE_TYPE_1_BYTE ; } else if ( ( value & 0xFFFF ) == value ) { dataSize = 2 ; wireType = WIRE_TYPE_2_BYTES ; } else if ( ( value & 0x1FFFFF ) == value ) { dataSize = 3 ; wireType = WIRE_TYPE_VAR_LENGTH_A ; } else { dataSize = 4 ; wireType = WIRE_TYPE_4_BYTES ; } appendFieldHeader ( toStream , fieldId , wireType , dataSize ) ; if ( wireType == WIRE_TYPE_VAR_LENGTH_A || wireType == WIRE_TYPE_VAR_LENGTH_B ) { while ( value != 0 ) { byte byteVal = ( byte ) ( value & 0x7F ) ; value = ( value >> 7 ) & 0x1FFFFFF ; if ( value != 0 ) { byteVal |= 0x80 ; } toStream . write ( byteVal ) ; } } else { for ( int i = 0 ; i < dataSize ; ++ i ) { toStream . write ( ( byte ) ( value & 0xFF ) ) ; value = value >> 8 ; } } }
Writes a single value to the stream
 @param toStream The stream to append the data to
 @param value The value to append to the stream
 @param fieldId ID of the field to store in the header
Writes a single value to the stream @param toStream The stream to append the data to @param value The value to append to the stream @param fieldId ID of the field to store in the header
// We can fit the value in 3 bytes using variable length encoding // & part is to get rid of negative bit being shifted and replicated