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

        assert ( wireType & 0x7 ) == wireType;

        if ( ( wireType & 0x7 ) != wireType )
        {
            throw new CodecException ( ErrorCode.InternalError );
        }

        // First 3 bits carry the wire type
        byte byteVal = ( byte ) ( wireType & 0x7 );

        // Next 4 bits carry the first 4 bits of the field ID:
        byteVal |= ( byte ) ( ( fieldId & 0xF ) << 3 );

        // Remove those 4 bits from the field ID:
        fieldId >>= 4;

        while ( fieldId > 0 )
        {
            // Field ID is longer - set the 'byteVal' value AND the overflow bit
            toStream.write ( byteVal | 0x80 );

            // Next byte will contain next 7 bits of the field ID:
            byteVal = ( byte ) ( fieldId & 0x7F );

            // Remove those 7 bits of the field ID:
            fieldId >>= 7;
        }

        // Last byte - no overflow bit set!
        toStream.write ( byteVal );

        if ( wireType == WIRE_TYPE_LENGTH_DELIM )
        {
            // In a single byte we can store 7 bits of the data length
            byteVal = ( byte ) ( dataSize & 0x7F );

            // Remove those 7 bits
            int remainingLen = dataSize >> 7;

            assert remainingLen >= 0;

            while ( remainingLen > 0 )
            {
                // data length is longer - set the 'byteVal' value AND the
                // overflow bit
                toStream.write ( byteVal | 0x80 );

                // Next byte will contain next 7 bits of the data length:
                byteVal = ( byte ) ( remainingLen & 0x7F );

                // Remove those 7 bits:
                remainingLen >>= 7;
            }

            // Last byte - no overflow bit set!
            toStream.write ( byteVal );
        }
    }
/**
     * Writes the header of the field (and appends it to the stream)
     *
     * @param toStream The output stream to encode the data to
     * @param fieldId The ID of the field to store in the header
     * @param wireType The wire type to use
     * @param dataSize The size of the data that will be stored in that field. Only used
     *               with WIRE_TYPE_LENGTH_DELIM wire type, it is ignored for all other wire types
     * @throws IOException Thrown by OutputStream.write, CodecException
     */
Writes the header of the field (and appends it to the stream)
@param toStream The output stream to encode the data to
@param fieldId The ID of the field to store in the header
@param wireType The wire type to use
@param dataSize The size of the data that will be stored in that field. Only used
with WIRE_TYPE_LENGTH_DELIM wire type, it is ignored for all other wire types
@throws IOException Thrown by OutputStream.write, CodecException
Writes the header of the field ( and appends it to the stream ) @param toStream The output stream to encode the data to @param fieldId The ID of the field to store in the header @param wireType The wire type to use @param dataSize The size of the data that will be stored in that field . Only used with WIRE_TYPE_LENGTH_DELIM wire type it is ignored for all other wire types @throws IOException Thrown by OutputStream . write CodecException
public static void appendFieldHeader ( OutputStream toStream, int fieldId, byte wireType, int dataSize )
    throws IOException, CodecException
    {
        if ( dataSize < 0 || fieldId < 0 )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }
        assert ( wireType & 0x7 ) == wireType;
        if ( ( wireType & 0x7 ) != wireType )
        {
            throw new CodecException ( ErrorCode.InternalError );
        }
        byte byteVal = ( byte ) ( wireType & 0x7 );
        byteVal |= ( byte ) ( ( fieldId & 0xF ) << 3 );
        fieldId >>= 4;
        while ( fieldId > 0 )
        {
            toStream.write ( byteVal | 0x80 );
            byteVal = ( byte ) ( fieldId & 0x7F );
            fieldId >>= 7;
        }
        toStream.write ( byteVal );
        if ( wireType == WIRE_TYPE_LENGTH_DELIM )
        {
            byteVal = ( byte ) ( dataSize & 0x7F );
            int remainingLen = dataSize >> 7;
            assert remainingLen >= 0;
            while ( remainingLen > 0 )
            {
                toStream.write ( byteVal | 0x80 );
                byteVal = ( byte ) ( remainingLen & 0x7F );
                :
                remainingLen >>= 7;
            }
            toStream.write ( byteVal );
        }
    }
public static void appendFieldHeader ( OutputStream toStream , int fieldId , byte wireType , int dataSize ) throws IOException , CodecException { if ( dataSize < 0 || fieldId < 0 ) { throw new CodecException ( ErrorCode . InvalidParameter ) ; } assert ( wireType & 0x7 ) == wireType ; if ( ( wireType & 0x7 ) != wireType ) { throw new CodecException ( ErrorCode . InternalError ) ; } byte byteVal = ( byte ) ( wireType & 0x7 ) ; byteVal |= ( byte ) ( ( fieldId & 0xF ) << 3 ) ; fieldId >>= 4 ; while ( fieldId > 0 ) { toStream . write ( byteVal | 0x80 ) ; byteVal = ( byte ) ( fieldId & 0x7F ) ; fieldId >>= 7 ; } toStream . write ( byteVal ) ; if ( wireType == WIRE_TYPE_LENGTH_DELIM ) { byteVal = ( byte ) ( dataSize & 0x7F ) ; int remainingLen = dataSize >> 7 ; assert remainingLen >= 0 ; while ( remainingLen > 0 ) { toStream . write ( byteVal | 0x80 ) ; byteVal = ( byte ) ( remainingLen & 0x7F ) ; remainingLen >>= 7 ; } toStream . write ( byteVal ) ; } }
Writes the header of the field (and appends it to the stream)
 @param toStream The output stream to encode the data to
 @param fieldId The ID of the field to store in the header
 @param wireType The wire type to use
 @param dataSize The size of the data that will be stored in that field.
Writes the header of the field ( and appends it to the stream ) @param toStream The output stream to encode the data to @param fieldId The ID of the field to store in the header @param wireType The wire type to use @param dataSize The size of the data that will be stored in that field .
// First 3 bits carry the wire type // Next 4 bits carry the first 4 bits of the field ID: // Remove those 4 bits from the field ID: // Field ID is longer - set the 'byteVal' value AND the overflow bit // Next byte will contain next 7 bits of the field ID: // Remove those 7 bits of the field ID: // Last byte - no overflow bit set! // In a single byte we can store 7 bits of the data length // Remove those 7 bits // data length is longer - set the 'byteVal' value AND the // overflow bit // Next byte will contain next 7 bits of the data length: // Remove those 7 bits: // Last byte - no overflow bit set!