java/proto/src/main/java/com/pravala/protocol/Codec.java
public static void appendField ( OutputStream toStream, Byte 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;
        }

        if ( value < 0 )
        {
            value = ( byte ) -value;

            appendFieldHeader ( toStream, fieldId, WIRE_TYPE_VAR_LENGTH_B, 0 );

            while ( value != 0 )
            {
                byte byteVal = ( byte ) ( value & 0x7F );

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

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

                toStream.write ( byteVal );
            }
        }
        else
        {
            appendFieldHeader ( toStream, fieldId, WIRE_TYPE_1_BYTE, 1 );
            toStream.write ( value );
        }
    }
/**
     *  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, 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, Byte 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;
        }
        if ( value < 0 )
        {
            value = ( byte ) -value;
            appendFieldHeader ( toStream, fieldId, WIRE_TYPE_VAR_LENGTH_B, 0 );
            while ( value != 0 )
            {
                byte byteVal = ( byte ) ( value & 0x7F );
                value = ( byte ) ( ( value >> 7 ) & 0x01 );
                if ( value != 0 )
                {
                    byteVal |= 0x80;
                }
                toStream.write ( byteVal );
            }
        }
        else
        {
            appendFieldHeader ( toStream, fieldId, WIRE_TYPE_1_BYTE, 1 );
            toStream.write ( value );
        }
    }
public static void appendField ( OutputStream toStream , Byte 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 ; } if ( value < 0 ) { value = ( byte ) - value ; appendFieldHeader ( toStream , fieldId , WIRE_TYPE_VAR_LENGTH_B , 0 ) ; while ( value != 0 ) { byte byteVal = ( byte ) ( value & 0x7F ) ; value = ( byte ) ( ( value >> 7 ) & 0x01 ) ; if ( value != 0 ) { byteVal |= 0x80 ; } toStream . write ( byteVal ) ; } } else { appendFieldHeader ( toStream , fieldId , WIRE_TYPE_1_BYTE , 1 ) ; toStream . write ( value ) ; } }
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
// & part is to get rid of negative bit being shifted and replicated