FileDocCategorySizeDatePackage
HTTPMessageDecoder.javaAPI DocAzureus 3.0.3.45381Tue Jul 17 18:03:12 BST 2007com.aelitis.azureus.core.networkmanager.impl.http

HTTPMessageDecoder

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

Fields Summary
private static final int
MAX_HEADER
private static final String
NL
private HTTPNetworkConnection
http_connection
private volatile boolean
paused
private volatile boolean
paused_internally
private volatile boolean
destroyed
private StringBuffer
header_so_far
private boolean
header_ready
private List
messages
private int
protocol_bytes_read
Constructors Summary
public HTTPMessageDecoder()

		
	 
	
	
	
public HTTPMessageDecoder(String pre_read_header)

		header_so_far.append( pre_read_header );
		
		header_ready	= true;
	
Methods Summary
protected voidaddMessage(com.aelitis.azureus.core.peermanager.messaging.Message message)

		synchronized( messages ){
			
			messages.add( message );
		}
		
		http_connection.readWakeup();
	
public java.nio.ByteBufferdestroy()

		paused		= true;
		destroyed	= true;
		
		if ( http_connection != null ){
		
			http_connection.destroy();
		}
		
	    try{

	    	for( int i=0; i<messages.size(); i++ ){
	    		
	    		Message msg = (Message)messages.get( i );
	    		
			    msg.destroy();
			}
		}catch( IndexOutOfBoundsException e ){
		    	// as access to messages_last_read isn't synchronized we can get this error if we destroy the
		    	// decoder in parallel with messages being removed. We don't really want to synchornized access
		    	// to this so we'll take the hit here
		}
		    
		messages.clear();
		  
		return( null );
	
public intgetDataBytesDecoded()

		return( 0 );
	
public intgetPercentDoneOfCurrentMessage()

		return( 0 );
	
public intgetProtocolBytesDecoded()

		return( protocol_bytes_read );
	
protected intgetQueueSize()

		return( messages.size());
	
public voidpauseDecoding()

		paused	= true;
	
protected voidpauseInternally()

		paused_internally = true;
	
public intperformStreamDecode(com.aelitis.azureus.core.networkmanager.Transport transport, int max_bytes)

			// before we start message processing we should have had the connection bound
		
		if ( http_connection == null ){
			
			Debug.out( "connection not yet assigned" );
			
			throw( new IOException( "Internal error - connection not yet assigned" ));
		}
		
		// System.out.println( "performStreamDecode" );
		
		protocol_bytes_read	= 0;
		
		if ( paused_internally ){
			
			return( 0 );
		}
		
		if ( header_ready ){
			
			header_ready = false;
			
			int	len = header_so_far.length();
			
			http_connection.decodeHeader( this, header_so_far.toString());
			
			header_so_far.setLength(0);

			return( len );
			
		}else{
			int	rem = max_bytes;
			
			byte[]	bytes = new byte[1];
			
			ByteBuffer		bb	= ByteBuffer.wrap( bytes );
			
			ByteBuffer[]	bbs = { bb };
			
			while( rem > 0 && !( paused || paused_internally )){
				
				if ( transport.read( bbs,0, 1 ) == 0 ){
					
					break;
				}
				
				rem--;
				
				protocol_bytes_read++;
				
				bb.flip();
				
				char	c = (char)(bytes[0]&0xff);
				
				header_so_far.append( c );
				
				if ( header_so_far.length() > MAX_HEADER ){
					
					throw( new IOException( "HTTP header exceeded maximum of " + MAX_HEADER ));
				}
				
				if ( c == '\n" ){
					
					String	header_str = header_so_far.toString();
					
					if ( header_str.endsWith( NL + NL )){
						
						http_connection.decodeHeader( this, header_str );
					
						header_so_far.setLength(0);
					}
				}
			}
			
			return( max_bytes - rem );
		}
	
public com.aelitis.azureus.core.peermanager.messaging.Message[]removeDecodedMessages()

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

		if ( !destroyed ){
			
			paused	= false;
		}
	
public voidsetConnection(HTTPNetworkConnection _http_connection)

		http_connection	= _http_connection;
		
		if ( destroyed ){
			
			http_connection.destroy();
		}