FileDocCategorySizeDatePackage
GenericMessageDecoder.javaAPI DocAzureus 3.0.3.44053Thu Aug 10 11:23:00 BST 2006org.gudy.azureus2.pluginsimpl.local.messaging

GenericMessageDecoder

public class GenericMessageDecoder extends Object implements com.aelitis.azureus.core.peermanager.messaging.MessageStreamDecoder

Fields Summary
public static final int
MAX_MESSAGE_LENGTH
private final ByteBuffer
length_buffer
private final ByteBuffer[]
buffers
private final String
msg_type
private final String
msg_desc
private List
messages
private int
protocol_bytes_last_read
private int
data_bytes_last_read
private volatile boolean
destroyed
Constructors Summary
protected GenericMessageDecoder(String _msg_type, String _msg_desc)

	
	 
	
				
				 
	
		msg_type	= _msg_type;
		msg_desc	= _msg_desc;
	
Methods Summary
public java.nio.ByteBufferdestroy()

		destroyed	= true;
		
		return( null );
	
public intgetDataBytesDecoded()

		return( data_bytes_last_read );
	
public intgetPercentDoneOfCurrentMessage()

		return( 0 );
	
public intgetProtocolBytesDecoded()

		return( protocol_bytes_last_read );
	
public voidpauseDecoding()

	
	
public intperformStreamDecode(com.aelitis.azureus.core.networkmanager.Transport transport, int max_bytes)

		protocol_bytes_last_read 	= 0;
	    data_bytes_last_read 		= 0;
	    
	    long	total_read = 0;
	    
		while( total_read < max_bytes ){
			
			long	bytes_read;
			
			ByteBuffer	payload_buffer = buffers[1];
			
			if ( payload_buffer == null ){
				
				bytes_read = transport.read( buffers, 0, 1 );
				
				protocol_bytes_last_read += bytes_read;
				
				if ( length_buffer.hasRemaining()){
					
					break;
					
				}else{
					
					length_buffer.flip();
					
					int	size = length_buffer.getInt();
					
					if ( size > MAX_MESSAGE_LENGTH ){
						
						Debug.out( "Message too large for generic payload" );
						
						throw( new IOException( "message too large" ));
					}
					
					buffers[1] = ByteBuffer.allocate( size );
					
					length_buffer.flip();
				}
			}else{
				
				bytes_read = transport.read( buffers, 1, 1 );
				
				data_bytes_last_read += bytes_read;
				
				if ( payload_buffer.hasRemaining()){
					
					break;
				}
				
				payload_buffer.flip();
				
				messages.add( new GenericMessage( msg_type, msg_desc, new DirectByteBuffer( payload_buffer ), false ));
				
				buffers[1]	= null;
			}
			
			total_read += bytes_read;
		}
		
		if ( destroyed ){
			
			throw( new IOException( "decoder has been destroyed" ));
		}
		
		return((int) total_read );
	
public com.aelitis.azureus.core.peermanager.messaging.Message[]removeDecodedMessages()

		if( messages.isEmpty() )  return null;
	      
		Message[] msgs = (Message[])messages.toArray( new Message[messages.size()] );
		
		messages.clear();
		    
		return( msgs );
	
public voidresumeDecoding()