@Override public Message deserialize(DirectByteBuffer data, byte version ) throws MessageException { if( data == null ) { throw new MessageException( "[" +getID() +"] decode error: data == null" ); } if( data.remaining( DirectByteBuffer.SS_MSG ) != 12 ) { throw new MessageException( "[" +getID() + "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] != 12" ); } int num = data.getInt( DirectByteBuffer.SS_MSG ); if( num < 0 ) { throw new MessageException( "[" +getID() + "] decode error: num < 0" ); } int offset = data.getInt( DirectByteBuffer.SS_MSG ); if( offset < 0 ) { throw new MessageException( "[" +getID()+ "] decode error: offset < 0" ); } int length = data.getInt( DirectByteBuffer.SS_MSG ); if( length < 0 ) { throw new MessageException( "[" +getID() + "] decode error: lngth < 0" ); } data.returnToPool(); return new BTCancel( num, offset, length, version ); }
@Override public Message deserialize(DirectByteBuffer data, byte version ) throws MessageException { if( data == null ) { throw new MessageException( "[" +getID() + "] decode error: data == null" ); } if( data.remaining( DirectByteBuffer.SS_MSG ) != 12 ) { throw new MessageException( "[" +getID() + "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] != 12" ); } int num = data.getInt( DirectByteBuffer.SS_MSG ); if( num < 0 ) { throw new MessageException( "[" +getID() + "] decode error: num < 0" ); } int offset = data.getInt( DirectByteBuffer.SS_MSG ); if( offset < 0 ) { throw new MessageException( "[" +getID() + "] decode error: offset < 0" ); } int length = data.getInt( DirectByteBuffer.SS_MSG ); if( length < 0 ) { throw new MessageException( "[" +getID() + "] decode error: length < 0" ); } data.returnToPool(); return new BTRequest( num, offset, length, version ); }
@Override public Message deserialize(DirectByteBuffer data, byte version ) throws MessageException { if( data == null ) { throw new MessageException( "[" +getID() + "] decode error: data == null" ); } if( data.remaining( DirectByteBuffer.SS_MSG ) != 12 ) { throw new MessageException( "[" +getID() + "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] != 12" ); } int num = data.getInt( DirectByteBuffer.SS_MSG ); if( num < 0 ) { throw new MessageException( "[" +getID() + "] decode error: num < 0" ); } int offset = data.getInt( DirectByteBuffer.SS_MSG ); if( offset < 0 ) { throw new MessageException( "[" +getID() + "] decode error: offset < 0" ); } int length = data.getInt( DirectByteBuffer.SS_MSG ); if( length < 0 ) { throw new MessageException( "[" +getID() + "] decode error: length < 0" ); } data.returnToPool(); return new BTRejectRequest( num, offset, length, version ); }
@Override public Message deserialize(DirectByteBuffer data, byte version ) throws MessageException { if( data == null ) { throw new MessageException( "[" +getID() + "] decode error: data == null" ); } if( data.remaining( DirectByteBuffer.SS_MSG ) < 8 ) { throw new MessageException( "[" +getID()+ "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] < 8" ); } int number = data.getInt( DirectByteBuffer.SS_MSG ); if( number < 0 ) { throw new MessageException( "[" +getID() +"] decode error: number < 0" ); } int offset = data.getInt( DirectByteBuffer.SS_MSG ); if( offset < 0 ) { throw new MessageException( "[" +getID() + "] decode error: offset < 0" ); } return new BTPiece( number, offset, data, version ); }
@Override public Message deserialize(DirectByteBuffer data, byte version ) throws MessageException { if( data == null ) { throw new MessageException( "[" +getID() +"] decode error: data == null" ); } if( data.remaining( DirectByteBuffer.SS_MSG ) != 4 ) { throw new MessageException( "[" +getID() + "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] != 12" ); } int num = data.getInt( DirectByteBuffer.SS_MSG ); if( num < 0 ) { throw new MessageException( "[" +getID() + "] decode error: num < 0" ); } data.returnToPool(); return new BTAllowedFast( num, version ); }
@Override public Message deserialize(DirectByteBuffer data, byte version ) throws MessageException { if( data == null ) { throw new MessageException( "[" +getID() + "] decode error: data == null" ); } if( data.remaining( DirectByteBuffer.SS_MSG ) != 4 ) { throw new MessageException( "[" +getID() + "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] != 4" ); } int number = data.getInt( DirectByteBuffer.SS_MSG ); if( number < 0 ) { throw new MessageException( "[" +getID() + "] decode error: number < 0" ); } data.returnToPool(); return new BTHave( number, version ); }
@Override public Message deserialize(DirectByteBuffer data, byte version ) throws MessageException { if( data == null ) { throw new MessageException( "[" +getID() +"] decode error: data == null" ); } if( data.remaining( DirectByteBuffer.SS_MSG ) != 4 ) { throw new MessageException( "[" +getID() + "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] != 12" ); } int num = data.getInt( DirectByteBuffer.SS_MSG ); if( num < 0 ) { throw new MessageException( "[" +getID() + "] decode error: num < 0" ); } data.returnToPool(); return new BTSuggestPiece( num, version ); }
/** * Construct a new AZ message instance from the given message raw byte stream. * @param stream_payload data * @return decoded/deserialized AZ message * @throws MessageException if message creation failed. * NOTE: Does not auto-return to buffer pool the given direct buffer on thrown exception. */ public static Message createAZMessage( DirectByteBuffer stream_payload ) throws MessageException { int id_length = stream_payload.getInt( bss ); if( id_length < 1 || id_length > 1024 || id_length > stream_payload.remaining( bss ) - 1 ) { byte bt_id = stream_payload.get( (byte)0, 0 ); throw new MessageException( "invalid AZ id length given: " +id_length+ ", stream_payload.remaining(): " +stream_payload.remaining( bss )+ ", BT id?=" +bt_id ); } byte[] id_bytes = new byte[ id_length ]; stream_payload.get( bss, id_bytes ); // if only the version came first we could save a lot of space by changing the id length + id.... // in the meantime we overload the version byte to have a version number and flags // flags = top 4 bits, version = bottom 4 bits byte version_and_flags = stream_payload.get( bss ); byte version = (byte)( version_and_flags & 0x0f ); if ( version >= MESSAGE_VERSION_SUPPORTS_PADDING ){ byte flags = (byte)(( version_and_flags >> 4 ) & 0x0f ); if ( ( flags & 0x01 ) != 0 ){ short padding_length = stream_payload.getShort( bss ); byte[] padding = new byte[padding_length]; stream_payload.get( bss, padding ); } } return MessageManager.getSingleton().createMessage( id_bytes, stream_payload, version ); }
message_length = length_buffer.getInt( SS );