@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) != 2) throw new MessageException("[" +getID() + "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] != 2"); short s_port = data.getShort(DirectByteBuffer.SS_MSG); data.returnToPool(); return new BTDHTPort(0xFFFF & s_port); }
/** * 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 ); }