FileDocCategorySizeDatePackage
DirectByteBuffer.javaAPI DocAzureus 3.0.3.414070Sat Sep 01 06:58:38 BST 2007org.gudy.azureus2.core3.util

DirectByteBuffer

public class DirectByteBuffer extends Object
Virtual direct byte buffer given out and tracker by the buffer pool.

Fields Summary
public static final byte
AL_NONE
public static final byte
AL_EXTERNAL
public static final byte
AL_OTHER
public static final byte
AL_PT_READ
public static final byte
AL_PT_LENGTH
public static final byte
AL_CACHE_READ
public static final byte
AL_DM_READ
public static final byte
AL_DM_ZERO
public static final byte
AL_DM_CHECK
public static final byte
AL_BT_PIECE
public static final byte
AL_CACHE_WRITE
public static final byte
AL_PROXY_RELAY
public static final byte
AL_MSG
public static final byte
AL_MSG_AZ_HAND
public static final byte
AL_MSG_AZ_PEX
public static final byte
AL_MSG_BT_CANCEL
public static final byte
AL_MSG_BT_HAND
public static final byte
AL_MSG_BT_HAVE
public static final byte
AL_MSG_BT_PIECE
public static final byte
AL_MSG_BT_REQUEST
public static final byte
AL_MSG_BT_KEEPALIVE
public static final byte
AL_MSG_BT_HEADER
public static final byte
AL_MSG_AZ_HEADER
public static final byte
AL_MSG_BT_PAYLOAD
public static final byte
AL_MSG_AZ_PAYLOAD
public static final byte
AL_FILE
public static final byte
AL_NET_CRYPT
public static final byte
AL_MSG_LT_EXT_HANDSHAKE
public static final String[]
AL_DESCS
public static final byte
SS_NONE
public static final byte
SS_EXTERNAL
public static final byte
SS_OTHER
public static final byte
SS_CACHE
public static final byte
SS_FILE
public static final byte
SS_NET
public static final byte
SS_BT
public static final byte
SS_DR
public static final byte
SS_DW
public static final byte
SS_PEER
public static final byte
SS_PROXY
public static final byte
SS_MSG
public static final String[]
SS_DESCS
public static final byte
OP_LIMIT
public static final byte
OP_LIMIT_INT
public static final byte
OP_POSITION
public static final byte
OP_POSITION_INT
public static final byte
OP_CLEAR
public static final byte
OP_FLIP
public static final byte
OP_REMANING
public static final byte
OP_CAPACITY
public static final byte
OP_PUT_BYTEARRAY
public static final byte
OP_PUT_DBB
public static final byte
OP_PUT_BB
public static final byte
OP_PUTINT
public static final byte
OP_PUT_BYTE
public static final byte
OP_GET
public static final byte
OP_GET_INT
public static final byte
OP_GET_BYTEARRAY
public static final byte
OP_GETINT
public static final byte
OP_GETINT_INT
public static final byte
OP_HASREMAINING
public static final byte
OP_READ_FC
public static final byte
OP_WRITE_FC
public static final byte
OP_READ_SC
public static final byte
OP_WRITE_SC
public static final byte
OP_GETBUFFER
public static final byte
OP_GETSHORT
public static final byte
OP_PUTSHORT
public static final String[]
OP_DESCS
protected static final boolean
TRACE
protected static final int
TRACE_BUFFER_SIZE
private ByteBuffer
buffer
private DirectByteBufferPool
pool
private byte
allocator
Constructors Summary
public DirectByteBuffer(ByteBuffer _buffer)

  
	
	//private byte[]					trace_buffer;
	//private int						trace_buffer_pos;
	
	 
	 
		 	  
	
		this( AL_NONE, _buffer, null );
	
public DirectByteBuffer(byte _allocator, ByteBuffer _buffer, DirectByteBufferPool _pool)

		allocator	= _allocator;
		buffer 		= _buffer;
		pool		= _pool;
		
		if ( TRACE ){
			/*
			trace_buffer	= new byte[TRACE_BUFFER_SIZE];
			
			Arrays.fill(trace_buffer,(byte)0);
			*/
		}
	
protected DirectByteBuffer(DirectByteBuffer basis)
constructor for reference counted version

param
basis

		allocator	= basis.allocator;
		buffer 		= basis.buffer;
		pool		= null;
	
Methods Summary
public intcapacity(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_CAPACITY );
		}
		
		return( buffer.capacity());
	
public voidclear(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_CLEAR );
		}
		
		buffer.clear();
	
protected voiddumpTrace(java.lang.Throwable e)

		if ( TRACE ){
			
			System.out.println( getTraceString());
			
			Ignore.ignore(e);
		}
	
public voidflip(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_FLIP );
		}
		
		buffer.flip();
	
public byteget(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_GET);
		}
		
		return( buffer.get());
	
public byteget(byte subsystem, int x)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_GET_INT);
		}
		
		return( buffer.get(x));
	
public voidget(byte subsystem, byte[] data)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_GET_BYTEARRAY);
		}
		
		buffer.get(data);
	
protected bytegetAllocator()

		return( allocator );
	
public java.nio.ByteBuffergetBuffer(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_GETBUFFER );
		}
		
		return( buffer );
	
protected java.nio.ByteBuffergetBufferInternal()

		return( buffer );
	
public intgetInt(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_GETINT);
		}
		
		return( buffer.getInt());
	
public intgetInt(byte subsystem, int x)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_GETINT_INT );
		}
		
		return( buffer.getInt(x));
	
public ReferenceCountedDirectByteBuffergetReferenceCountedBuffer()

		ReferenceCountedDirectByteBuffer res = new ReferenceCountedDirectByteBuffer( this );
				
		return( res );
	
public shortgetShort(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_GETSHORT);
		}
		
		return( buffer.getShort());
	
protected java.lang.StringgetTraceString()

		if ( TRACE ){
			/*
			StringBuffer	sb = new StringBuffer();
			
			sb.append( AL_DESCS[allocator]);
			sb.append( ":" );
			
			boolean	wrapped	= false;
			
			int	start	= 0;
			int	end		= trace_buffer_pos;
			
			if ( trace_buffer[trace_buffer_pos] != 0 ){
				
				start	= trace_buffer_pos;
				
				wrapped	= true;
			}
			
			if ( end == 0 && !wrapped ){
				
				sb.append( "not used");
				
			}else{
							
				if ( wrapped ){
					
					sb.append( "*" );
				}
				
				int	num = 0;
				
				do{
					
					if ( num++ > 0 ){
						sb.append(",");
					}
					
					sb.append( SS_DESCS[trace_buffer[start++]]);
					sb.append( "/" );
					sb.append( OP_DESCS[trace_buffer[start++]]);
					
					if ( start == TRACE_BUFFER_SIZE ){
						
						start	= 0;
					}
				}while( start != end );
			}
			
			return( sb.toString());
			*/
		}
			
		return( null );
	
public booleanhasRemaining(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_HASREMAINING );
		}
		
		return( buffer.hasRemaining());
	
public intlimit(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_LIMIT );
		}
		
		return( buffer.limit());
	
public voidlimit(byte subsystem, int l)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_LIMIT_INT );
		}
		
		buffer.limit(l);
	
public intposition(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_POSITION);
		}
		
	  	return( buffer.position());
	
public voidposition(byte subsystem, int l)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_POSITION_INT);
		}
		
		buffer.position(l);
	
public voidput(byte subsystem, byte[] data)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_PUT_BYTEARRAY );
		}
		
		buffer.put( data );
	
public voidput(byte subsystem, org.gudy.azureus2.core3.util.DirectByteBuffer data)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_PUT_DBB);
		}
		
		buffer.put( data.buffer );
	
public voidput(byte subsystem, java.nio.ByteBuffer data)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_PUT_BB);
		}
		
		buffer.put( data );
	
public voidput(byte subsystem, byte data)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_PUT_BYTE );
		}
		
		buffer.put( data );
	
public voidputInt(byte subsystem, int data)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_PUTINT);
		}
		
		buffer.putInt( data );
	
public voidputShort(byte subsystem, short x)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_PUTSHORT);
		}
		
		buffer.putShort( x );
	
public intread(byte subsystem, java.nio.channels.FileChannel chan)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_READ_FC);
		}
		
		try{
			return( chan.read(buffer ));
			
		}catch( IllegalArgumentException e ){
			
			dumpTrace(e);
			
			throw( e );
		}
	
public intread(byte subsystem, java.nio.channels.SocketChannel chan)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_READ_SC );
		}
		
		try{
			return( chan.read(buffer ));
			
		}catch( IllegalArgumentException e ){
			
			dumpTrace(e);
			
			throw( e );
		}
	
public intremaining(byte subsystem)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_REMANING );
		}
		
		return( buffer.remaining());
	
public voidreturnToPool()

				
		if ( pool != null ){
				
				// we can't afford to return a buffer more than once to the pool as it'll get
				// handed out twice in parallel and cause weird problems. We haven't been able
				// to totally eliminiate duplicate returnToPool calls....
			
			synchronized( this ){
				
				if ( buffer == null ){
					
					Debug.out( "Buffer already returned to pool");
					
				}else{
	    	
					pool.returnBuffer( this );
					
					buffer	= null;
				}
			}
		}
	
public voidreturnToPoolIfNotFree()
Normally you should know when a buffer is/isn't free and NOT CALL THIS METHOD However, there are some error situations where the existing code doesn't correctly manage things - we know this and don't want spurious logs occuring as per the above normal method

				
		if ( pool != null ){
				
				// we can't afford to return a buffer more than once to the pool as it'll get
				// handed out twice in parallel and cause weird problems. We haven't been able
				// to totally eliminiate duplicate returnToPool calls....
			
			synchronized( this ){
				
				if ( buffer != null ){
	    	
					pool.returnBuffer( this );
					
					buffer	= null;
				}
			}
		}
	
protected voidtraceUsage(byte subsystem, byte operation)

		if ( TRACE ){
			/*
			trace_buffer[trace_buffer_pos++]	= subsystem;
			trace_buffer[trace_buffer_pos++]	= operation;	
		
			if ( trace_buffer_pos == TRACE_BUFFER_SIZE ){
				
				trace_buffer_pos	= 0;
			}
			*/
		}
	
public intwrite(byte subsystem, java.nio.channels.FileChannel chan)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_WRITE_FC );
		}
		
		try{
			return( chan.write(buffer ));
			
		}catch( IllegalArgumentException e ){
			
			dumpTrace(e);
			
			throw( e );
		}
	
public intwrite(byte subsystem, java.nio.channels.SocketChannel chan)

		if ( TRACE ){
			
			traceUsage( subsystem, OP_WRITE_SC );
		}
		
		try{
			return( chan.write(buffer ));
			
		}catch( IllegalArgumentException e ){
			
			dumpTrace(e);
			
			throw( e );
		}