FileDocCategorySizeDatePackage
MessagingUtil.javaAPI DocAzureus 3.0.3.42966Mon Jun 25 16:48:32 BST 2007com.aelitis.azureus.core.peermanager.messaging

MessagingUtil

public class MessagingUtil extends Object

Fields Summary
Constructors Summary
Methods Summary
public static java.util.MapconvertBencodedByteStreamToPayload(DirectByteBuffer stream, int min_size, java.lang.String id)
Convert the given bencoded byte stream into a message map.

param
stream to convert
param
min_size of stream
param
id of message
return
mapped deserialization
throws
MessageException on convertion error NOTE: Does not auto-return given direct buffer on thrown exception.

    if( stream == null ) {
      throw new MessageException( "[" +id +"] decode error: stream == null" );
    }
    
    if( stream.remaining( DirectByteBuffer.SS_MSG ) < min_size ) {
      throw new MessageException( "[" +id +"] decode error: stream.remaining[" +stream.remaining( DirectByteBuffer.SS_MSG )+ "] < " +min_size );
    }

    byte[] raw = new byte[ stream.remaining( DirectByteBuffer.SS_MSG ) ];
    
    stream.get( DirectByteBuffer.SS_MSG, raw );
  
    try {
     Map result = BDecoder.decode( raw );
     
     stream.returnToPool();
     
     return( result );
     
    }catch( Throwable t ) {
      throw new MessageException( "[" +id+ "] payload stream b-decode error: " +t.getMessage() );
    } 
  
public static DirectByteBufferconvertPayloadToBencodedByteStream(java.util.Map payload, byte alloc_id)
Convert the given message payload map to a bencoded byte stream.

param
payload to convert
return
bencoded serialization

    byte[] raw_payload;
    
    try {
      raw_payload = BEncoder.encode( payload );
    }
    catch( Throwable t ) {
      Debug.out( t );
      raw_payload = new byte[0];
    }
    
    DirectByteBuffer buffer = DirectByteBufferPool.getBuffer( alloc_id, raw_payload.length );
    buffer.put( DirectByteBuffer.SS_MSG, raw_payload );
    buffer.flip( DirectByteBuffer.SS_MSG );
    
    return buffer;