java/proto/src/main/java/com/pravala/protocol/Codec.java
public static FieldHeader readFieldHeader ( ReadBuffer readBuffer )
    throws CodecException
    {
        if ( readBuffer == null || readBuffer.getBuffer() == null )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }

        // Not enough data in the buffer
        if ( readBuffer.getSize() <= readBuffer.getOffset() )
        {
            throw new CodecException ( ErrorCode.IncompleteData );
        }

        byte byteVal = readBuffer.readByte();

        // We read wire type - first 3 bits
        final byte wireType = ( byte ) ( byteVal & 0x7 );
        int fieldSize = 0;
        int fieldId = ( byteVal >> 3 ) & 0x0F;

        int shift = 4;

        // Overflow bit is set
        while ( ( byteVal & 0x80 ) != 0 )
        {
            // Not enough data in the buffer
            if ( readBuffer.getSize() <= readBuffer.getOffset() )
            {
                throw new CodecException ( ErrorCode.IncompleteData );
            }

            // shifts:
            // 4, 11, 18, 25, 32
            if ( shift > 25 )
            {
                throw new CodecException ( ErrorCode.ProtocolError );
            }

            byteVal = readBuffer.readByte();

            // We use current byte, take 7 bits from it (the eight one is
            // used as overflow bit), and shift it to add it to already read bits
            fieldId |= ( byteVal & 0x7F ) << shift;

            shift += 7;
        }

        // Here byteVal should be at the LAST byte of field "header".
        assert ( ( byteVal & 0x80 ) == 0 );

        // Offset points at the first byte of the payload (or the length field)

        switch ( wireType )
        {
            case WIRE_TYPE_ZERO:
                fieldSize = 0;
                break;

            case WIRE_TYPE_1_BYTE:
                fieldSize = 1;
                break;

            case WIRE_TYPE_2_BYTES:
                fieldSize = 2;
                break;

            case WIRE_TYPE_4_BYTES:
                fieldSize = 4;
                break;

            case WIRE_TYPE_8_BYTES:
                fieldSize = 8;
                break;

            case WIRE_TYPE_LENGTH_DELIM:
                {
                    fieldSize = 0;
                    shift = 0;

                    do
                    {
                        if ( readBuffer.getSize() <= readBuffer.getOffset() )
                        {
                            throw new CodecException ( ErrorCode.IncompleteData );
                        }

                        // shifts:
                        // 0, 7, 14, 21, 28, 37
                        if ( shift > 28 )
                        {
                            throw new CodecException ( ErrorCode.ProtocolError );
                        }

                        byteVal = readBuffer.readByte();

                        // The last byte can have only up to 4 bits set!
                        if ( shift == 28 && ( byteVal & 0x0F ) != byteVal )
                        {
                            throw new CodecException ( ErrorCode.ProtocolError );
                        }

                        // We use current byte, take 7 bits from it (the eight one is
                        // used as overflow bit), and shift it to add it to already read bits
                        fieldSize |= ( byteVal & 0x7F ) << shift;

                        // We just read 7 bits, next read will be in front of them
                        shift += 7;
                    }
                    while ( ( byteVal & 0x80 ) != 0 );
                }
                break;

            case WIRE_TYPE_VAR_LENGTH_A:
            case WIRE_TYPE_VAR_LENGTH_B:
                {
                    fieldSize = 0;

                    // We can't modify the original offset, since we are not consuming the header anymore.
                    // This is the actual payload.

                    int tmpOffset = readBuffer.getOffset();

                    do
                    {
                        // Not enough data in the buffer
                        if ( readBuffer.getSize() <= tmpOffset )
                        {
                            throw new CodecException ( ErrorCode.IncompleteData );
                        }

                        ++fieldSize;

                        byteVal = readBuffer.getBuffer()[ tmpOffset++ ];
                    }
                    while ( ( byteVal & 0x80 ) != 0 );
                }
                break;

            default:
                throw new CodecException ( ErrorCode.ProtocolError );
        }

        assert fieldSize >= 0;

        if ( readBuffer.getOffset() + fieldSize > readBuffer.getSize() )
        {
            throw new CodecException ( ErrorCode.IncompleteData );
        }

        assert readBuffer.getOffset() + fieldSize <= readBuffer.getSize();

        return new FieldHeader ( fieldId, fieldSize, wireType );
    }
/**
     * Extracts the field size from the buffer
     *
     *        It reads data from the buffer, starting at given offset, extracts
     *        the wire type and data length (either based on the wire type, or
     *        on the length field in case wire type says it's length-delimited
     *        field). This function modifies offset to point at the first byte of data
     *        payload (so after field id, wire type and field length if it's
     *        present. After this is called offset can point beyond buffer's
     *        size!!! (but in that case an error is thrown).
     *
     * @param readBuffer The description of the buffer to read the field from.
     *        The offset field should be pointing to the beginning of the field
     *        when this function is called. This function modifies this offset
     *        to point at the first byte of payload (or past the field when
     *        there is no payload)
     * @return The size of the field's payload
     * @throws CodecException
     */
Extracts the field size from the buffer
It reads data from the buffer, starting at given offset, extracts
the wire type and data length (either based on the wire type, or
on the length field in case wire type says it's length-delimited
field). This function modifies offset to point at the first byte of data
payload (so after field id, wire type and field length if it's
present. After this is called offset can point beyond buffer's
size!!. (but in that case an error is thrown).

@param readBuffer The description of the buffer to read the field from.
The offset field should be pointing to the beginning of the field
when this function is called. This function modifies this offset
to point at the first byte of payload (or past the field when
there is no payload)
@return The size of the field's payload
@throws CodecException
Extracts the field size from the buffer It reads data from the buffer starting at given offset extracts the wire type and data length ( either based on the wire type or on the length field in case wire type says it ' s length - delimited field ) . This function modifies offset to point at the first byte of data payload ( so after field id wire type and field length if it ' s present . After this is called offset can point beyond buffer ' s size!! . ( but in that case an error is thrown ) . @param readBuffer The description of the buffer to read the field from . The offset field should be pointing to the beginning of the field when this function is called . This function modifies this offset to point at the first byte of payload ( or past the field when there is no payload ) @return The size of the field ' s payload @throws CodecException
public static FieldHeader readFieldHeader ( ReadBuffer readBuffer )
    throws CodecException
    {
        if ( readBuffer == null || readBuffer.getBuffer() == null )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }
        if ( readBuffer.getSize() <= readBuffer.getOffset() )
        {
            throw new CodecException ( ErrorCode.IncompleteData );
        }
        byte byteVal = readBuffer.readByte();
        final byte wireType = ( byte ) ( byteVal & 0x7 );
        int fieldSize = 0;
        int fieldId = ( byteVal >> 3 ) & 0x0F;
        int shift = 4;
        while ( ( byteVal & 0x80 ) != 0 )
        {
            if ( readBuffer.getSize() <= readBuffer.getOffset() )
            {
                throw new CodecException ( ErrorCode.IncompleteData );
            }
            if ( shift > 25 )
            {
                throw new CodecException ( ErrorCode.ProtocolError );
            }
            byteVal = readBuffer.readByte();
            fieldId |= ( byteVal & 0x7F ) << shift;
            shift += 7;
        }
        assert ( ( byteVal & 0x80 ) == 0 );
        switch ( wireType )
        {
            case WIRE_TYPE_ZERO:
                fieldSize = 0;
                break;
            case WIRE_TYPE_1_BYTE:
                fieldSize = 1;
                break;
            case WIRE_TYPE_2_BYTES:
                fieldSize = 2;
                break;
            case WIRE_TYPE_4_BYTES:
                fieldSize = 4;
                break;
            case WIRE_TYPE_8_BYTES:
                fieldSize = 8;
                break;
            case WIRE_TYPE_LENGTH_DELIM:
                {
                    fieldSize = 0;
                    shift = 0;
                    do
                    {
                        if ( readBuffer.getSize() <= readBuffer.getOffset() )
                        {
                            throw new CodecException ( ErrorCode.IncompleteData );
                        }
                        if ( shift > 28 )
                        {
                            throw new CodecException ( ErrorCode.ProtocolError );
                        }
                        byteVal = readBuffer.readByte();
                        if ( shift == 28 && ( byteVal & 0x0F ) != byteVal )
                        {
                            throw new CodecException ( ErrorCode.ProtocolError );
                        }
                        fieldSize |= ( byteVal & 0x7F ) << shift;
                        shift += 7;
                    }
                    while ( ( byteVal & 0x80 ) != 0 );
                }
                break;
            case WIRE_TYPE_VAR_LENGTH_A:
            case WIRE_TYPE_VAR_LENGTH_B:
                {
                    fieldSize = 0;
                    int tmpOffset = readBuffer.getOffset();
                    do
                    {
                        if ( readBuffer.getSize() <= tmpOffset )
                        {
                            throw new CodecException ( ErrorCode.IncompleteData );
                        }
                        ++fieldSize;
                        byteVal = readBuffer.getBuffer()[ tmpOffset++ ];
                    }
                    while ( ( byteVal & 0x80 ) != 0 );
                }
                break;
            default:
                throw new CodecException ( ErrorCode.ProtocolError );
        }
        assert fieldSize >= 0;
        if ( readBuffer.getOffset() + fieldSize > readBuffer.getSize() )
        {
            throw new CodecException ( ErrorCode.IncompleteData );
        }
        assert readBuffer.getOffset() + fieldSize <= readBuffer.getSize();
        return new FieldHeader ( fieldId, fieldSize, wireType );
    }
public static FieldHeader readFieldHeader ( ReadBuffer readBuffer ) throws CodecException { if ( readBuffer == null || readBuffer . getBuffer ( ) == null ) { throw new CodecException ( ErrorCode . InvalidParameter ) ; } if ( readBuffer . getSize ( ) <= readBuffer . getOffset ( ) ) { throw new CodecException ( ErrorCode . IncompleteData ) ; } byte byteVal = readBuffer . readByte ( ) ; final byte wireType = ( byte ) ( byteVal & 0x7 ) ; int fieldSize = 0 ; int fieldId = ( byteVal >> 3 ) & 0x0F ; int shift = 4 ; while ( ( byteVal & 0x80 ) != 0 ) { if ( readBuffer . getSize ( ) <= readBuffer . getOffset ( ) ) { throw new CodecException ( ErrorCode . IncompleteData ) ; } if ( shift > 25 ) { throw new CodecException ( ErrorCode . ProtocolError ) ; } byteVal = readBuffer . readByte ( ) ; fieldId |= ( byteVal & 0x7F ) << shift ; shift += 7 ; } assert ( ( byteVal & 0x80 ) == 0 ) ; switch ( wireType ) { case WIRE_TYPE_ZERO : fieldSize = 0 ; break ; case WIRE_TYPE_1_BYTE : fieldSize = 1 ; break ; case WIRE_TYPE_2_BYTES : fieldSize = 2 ; break ; case WIRE_TYPE_4_BYTES : fieldSize = 4 ; break ; case WIRE_TYPE_8_BYTES : fieldSize = 8 ; break ; case WIRE_TYPE_LENGTH_DELIM : { fieldSize = 0 ; shift = 0 ; do { if ( readBuffer . getSize ( ) <= readBuffer . getOffset ( ) ) { throw new CodecException ( ErrorCode . IncompleteData ) ; } if ( shift > 28 ) { throw new CodecException ( ErrorCode . ProtocolError ) ; } byteVal = readBuffer . readByte ( ) ; if ( shift == 28 && ( byteVal & 0x0F ) != byteVal ) { throw new CodecException ( ErrorCode . ProtocolError ) ; } fieldSize |= ( byteVal & 0x7F ) << shift ; shift += 7 ; } while ( ( byteVal & 0x80 ) != 0 ) ; } break ; case WIRE_TYPE_VAR_LENGTH_A : case WIRE_TYPE_VAR_LENGTH_B : { fieldSize = 0 ; int tmpOffset = readBuffer . getOffset ( ) ; do { if ( readBuffer . getSize ( ) <= tmpOffset ) { throw new CodecException ( ErrorCode . IncompleteData ) ; } ++ fieldSize ; byteVal = readBuffer . getBuffer ( ) [ tmpOffset ++ ] ; } while ( ( byteVal & 0x80 ) != 0 ) ; } break ; default : throw new CodecException ( ErrorCode . ProtocolError ) ; } assert fieldSize >= 0 ; if ( readBuffer . getOffset ( ) + fieldSize > readBuffer . getSize ( ) ) { throw new CodecException ( ErrorCode . IncompleteData ) ; } assert readBuffer . getOffset ( ) + fieldSize <= readBuffer . getSize ( ) ; return new FieldHeader ( fieldId , fieldSize , wireType ) ; }
Extracts the field size from the buffer
        It reads data from the buffer, starting at given offset, extracts
        the wire type and data length (either based on the wire type, or
        on the length field in case wire type says it's length-delimited
        field).
Extracts the field size from the buffer It reads data from the buffer starting at given offset extracts the wire type and data length ( either based on the wire type or on the length field in case wire type says it ' s length - delimited field ) .
// Not enough data in the buffer // We read wire type - first 3 bits // Overflow bit is set // Not enough data in the buffer // shifts: // 4, 11, 18, 25, 32 // We use current byte, take 7 bits from it (the eight one is // used as overflow bit), and shift it to add it to already read bits // Here byteVal should be at the LAST byte of field "header". // Offset points at the first byte of the payload (or the length field) // shifts: // 0, 7, 14, 21, 28, 37 // The last byte can have only up to 4 bits set! // We use current byte, take 7 bits from it (the eight one is // used as overflow bit), and shift it to add it to already read bits // We just read 7 bits, next read will be in front of them // We can't modify the original offset, since we are not consuming the header anymore. // This is the actual payload. // Not enough data in the buffer