java/proto/src/main/java/com/pravala/protocol/SerializableBase.java
public int deserializeWithLength ( byte[] buffer, final int offset, final int length ) throws CodecException
    {
        orgDataBuf = null;

        if ( length < 0 )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }

        // Min length - 2 bytes, one for the field header, one for the 'length' field
        if ( length < 2 )
        {
            return -1 * ( 2 - length );
        }

        if ( buffer == null || offset < 0 || length < 0 || offset + length > buffer.length )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }

        assert length >= 0;

        int payloadSize = 0;
        ReadBuffer readBuffer = new ReadBuffer ( buffer, offset, offset + length );

        try
        {
            // This DOES modify the offset
            Codec.FieldHeader hdr = Codec.readFieldHeader ( readBuffer );

            // This is not the code we expected!
            if ( hdr.fieldId != 0 )
            {
                throw new CodecException ( ErrorCode.ProtocolError );
            }

            // This DOES modify the offset
            payloadSize = Codec.readInteger ( hdr, readBuffer );

            assert readBuffer.getOffset() > offset;

            if ( payloadSize < 0 )
            {
                throw new CodecException ( ErrorCode.ProtocolError );
            }
        }
        catch ( CodecException e )
        {
            if ( e.getErrorCode() == ErrorCode.IncompleteData )
            {
                // We still are missing some data. We don't know how much, so
                // lets just say there is one byte missing!
                return -1;
            }

            // Otherwise just pass this error up!
            throw e;
        }

        // We don't have the entire message yet!
        // Report the number of missing bytes.
        if ( readBuffer.getOffset() + payloadSize > offset + length )
        {
            return -1 * ( readBuffer.getOffset() + payloadSize - ( offset + length ) );
        }

        assert offset < readBuffer.getOffset();
        assert readBuffer.getOffset() + payloadSize <= offset + length;
        assert readBuffer.getOffset() + payloadSize <= buffer.length;
        assert readBuffer.getOffset() + payloadSize <= readBuffer.getSize();

        readBuffer.setSize ( readBuffer.getOffset() + payloadSize );

        deserializeBase ( readBuffer );

        // deserializeBase should NOT modify the buffer...
        return payloadSize + ( readBuffer.getOffset() - offset );
    }
/**
     * Deserializes the entire message from the buffer
     *
     * It calls deserializeData from inheriting classes and also decodes the total message's length.
     * @param buffer The buffer to deserialize the data from.
     * @param offset The offset in the buffer to start deserializing from.
     * @param length The number of bytes (starting from the offset) that can be deserialized.
     * @return Negative value means that more bytes are needed. The number of missing bytes is the absolute
     *         value of the number returned. Positive value means that the entire message was deserialized,
     *         and that the returned number of bytes were consumed
     * @throws CodecException
     */
Deserializes the entire message from the buffer
It calls deserializeData from inheriting classes and also decodes the total message's length.
@param buffer The buffer to deserialize the data from.
@param offset The offset in the buffer to start deserializing from.
@param length The number of bytes (starting from the offset) that can be deserialized.
@return Negative value means that more bytes are needed. The number of missing bytes is the absolute
value of the number returned. Positive value means that the entire message was deserialized,
and that the returned number of bytes were consumed
@throws CodecException
Deserializes the entire message from the buffer It calls deserializeData from inheriting classes and also decodes the total message ' s length . @param buffer The buffer to deserialize the data from . @param offset The offset in the buffer to start deserializing from . @param length The number of bytes ( starting from the offset ) that can be deserialized . @return Negative value means that more bytes are needed . The number of missing bytes is the absolute value of the number returned . Positive value means that the entire message was deserialized and that the returned number of bytes were consumed @throws CodecException
public int deserializeWithLength ( byte[] buffer, final int offset, final int length ) throws CodecException
    {
        orgDataBuf = null;
        if ( length < 0 )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }
        if ( length < 2 )
        {
            return -1 * ( 2 - length );
        }
        if ( buffer == null || offset < 0 || length < 0 || offset + length > buffer.length )
        {
            throw new CodecException ( ErrorCode.InvalidParameter );
        }
        assert length >= 0;
        int payloadSize = 0;
        ReadBuffer readBuffer = new ReadBuffer ( buffer, offset, offset + length );
        try
        {
            Codec.FieldHeader hdr = Codec.readFieldHeader ( readBuffer );
            if ( hdr.fieldId != 0 )
            {
                throw new CodecException ( ErrorCode.ProtocolError );
            }
            payloadSize = Codec.readInteger ( hdr, readBuffer );
            assert readBuffer.getOffset() > offset;
            if ( payloadSize < 0 )
            {
                throw new CodecException ( ErrorCode.ProtocolError );
            }
        }
        catch ( CodecException e )
        {
            if ( e.getErrorCode() == ErrorCode.IncompleteData )
            {
                return -1;
            }
            throw e;
        }
        if ( readBuffer.getOffset() + payloadSize > offset + length )
        {
            return -1 * ( readBuffer.getOffset() + payloadSize - ( offset + length ) );
        }
        assert offset < readBuffer.getOffset();
        assert readBuffer.getOffset() + payloadSize <= offset + length;
        assert readBuffer.getOffset() + payloadSize <= buffer.length;
        assert readBuffer.getOffset() + payloadSize <= readBuffer.getSize();
        readBuffer.setSize ( readBuffer.getOffset() + payloadSize );
        deserializeBase ( readBuffer );
        return payloadSize + ( readBuffer.getOffset() - offset );
    }
public int deserializeWithLength ( byte [ ] buffer , final int offset , final int length ) throws CodecException { orgDataBuf = null ; if ( length < 0 ) { throw new CodecException ( ErrorCode . InvalidParameter ) ; } if ( length < 2 ) { return - 1 * ( 2 - length ) ; } if ( buffer == null || offset < 0 || length < 0 || offset + length > buffer . length ) { throw new CodecException ( ErrorCode . InvalidParameter ) ; } assert length >= 0 ; int payloadSize = 0 ; ReadBuffer readBuffer = new ReadBuffer ( buffer , offset , offset + length ) ; try { Codec . FieldHeader hdr = Codec . readFieldHeader ( readBuffer ) ; if ( hdr . fieldId != 0 ) { throw new CodecException ( ErrorCode . ProtocolError ) ; } payloadSize = Codec . readInteger ( hdr , readBuffer ) ; assert readBuffer . getOffset ( ) > offset ; if ( payloadSize < 0 ) { throw new CodecException ( ErrorCode . ProtocolError ) ; } } catch ( CodecException e ) { if ( e . getErrorCode ( ) == ErrorCode . IncompleteData ) { return - 1 ; } throw e ; } if ( readBuffer . getOffset ( ) + payloadSize > offset + length ) { return - 1 * ( readBuffer . getOffset ( ) + payloadSize - ( offset + length ) ) ; } assert offset < readBuffer . getOffset ( ) ; assert readBuffer . getOffset ( ) + payloadSize <= offset + length ; assert readBuffer . getOffset ( ) + payloadSize <= buffer . length ; assert readBuffer . getOffset ( ) + payloadSize <= readBuffer . getSize ( ) ; readBuffer . setSize ( readBuffer . getOffset ( ) + payloadSize ) ; deserializeBase ( readBuffer ) ; return payloadSize + ( readBuffer . getOffset ( ) - offset ) ; }
Deserializes the entire message from the buffer
 It calls deserializeData from inheriting classes and also decodes the total message's length.
Deserializes the entire message from the buffer It calls deserializeData from inheriting classes and also decodes the total message ' s length .
// Min length - 2 bytes, one for the field header, one for the 'length' field // This DOES modify the offset // This is not the code we expected! // This DOES modify the offset // We still are missing some data. We don't know how much, so // lets just say there is one byte missing! // Otherwise just pass this error up! // We don't have the entire message yet! // Report the number of missing bytes. // deserializeBase should NOT modify the buffer...