public PooledByteBufferImpl( int size ) { buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_EXTERNAL, size ); }
public static DirectByteBuffer getBuffer( byte allocator, int length ) { return( impl.getBufferSupport( allocator, length )); }
/** * 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 */ public void returnToPoolIfNotFree() { 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.returnBufferSupport( this ); buffer = null; //was_returned_to_pool = true; } } } } }
@Override public ByteBuffer allocateDirectByteBuffer( int size ) { return( DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_EXTERNAL,size ).getBuffer(DirectByteBuffer.SS_EXTERNAL)); }
public void returnToPool() { 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.returnBufferSupport( this ); buffer = null; //was_returned_to_pool = true; } } } }
public PooledByteBufferImpl( byte[] data ) { buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_EXTERNAL, data.length ); buffer.put( DirectByteBuffer.AL_EXTERNAL, data ); buffer.position( DirectByteBuffer.AL_EXTERNAL, 0 ); }
@Override public DirectByteBuffer[] getData() { if( buffer == null ) { buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_BT_ALLOWED_FAST, 4 ); buffer.putInt( DirectByteBuffer.SS_MSG, piece_number ); buffer.flip( DirectByteBuffer.SS_MSG ); } return new DirectByteBuffer[]{ buffer }; }
@Override public DirectByteBuffer[] getData() { if( buffer == null ) { buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_BT_HAVE, 4 ); buffer.putInt( DirectByteBuffer.SS_MSG, piece_number ); buffer.flip( DirectByteBuffer.SS_MSG ); } return new DirectByteBuffer[]{ buffer }; }
@Override public DirectByteBuffer[] getData() { if( buffer == null ) { buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_BT_SUGGEST_PIECE, 4 ); buffer.putInt( DirectByteBuffer.SS_MSG, piece_number ); buffer.flip( DirectByteBuffer.SS_MSG ); } return new DirectByteBuffer[]{ buffer }; }
@Override public DirectByteBuffer[] getData() { if ( buffer == null) { buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_LT_HANDSHAKE, 1 ); buffer.put(DirectByteBuffer.SS_MSG, (byte)(upload_only?1:0)); buffer.flip(DirectByteBuffer.SS_MSG); } return( new DirectByteBuffer[]{ buffer }); }
@Override public DirectByteBuffer[] getRawData() { if( buffer == null ) { DirectByteBuffer dbb = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_BT_KEEPALIVE, 4 ); dbb.putInt( DirectByteBuffer.SS_BT, 0 ); dbb.flip( DirectByteBuffer.SS_BT ); buffer = new DirectByteBuffer[]{ dbb }; } return buffer; }
public PooledByteBufferImpl( byte[] data, int offset, int length ) { buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_EXTERNAL, length ); buffer.put( DirectByteBuffer.AL_EXTERNAL, data, offset, length ); buffer.position( DirectByteBuffer.AL_EXTERNAL, 0 ); }
@Override public DirectByteBuffer[] getData() { DirectByteBuffer[] orig_data = this.base_message.getData(); DirectByteBuffer[] new_data = new DirectByteBuffer[orig_data.length + 1]; if (buffer_header == null ) { buffer_header = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_LT_EXT_MESSAGE, 1); buffer_header.put(DirectByteBuffer.SS_MSG, this.extension_id); buffer_header.flip(DirectByteBuffer.SS_MSG); } new_data[0] = buffer_header; System.arraycopy(orig_data, 0, new_data, 1, orig_data.length); return new_data; }
@Override public DirectByteBuffer read( long offset, int length ) throws IOException { DirectByteBuffer buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_DM_READ, length ); try{ cache_file.read( buffer, offset, CacheFile.CP_READ_CACHE ); }catch( Throwable e ){ buffer.returnToPool(); Debug.printStackTrace(e); throw( new IOException( e.getMessage())); } return( buffer ); }
@Override public DirectByteBuffer[] getData() { if( buffer[0] == null ) { buffer[0] = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_BT_PIECE, 8 ); buffer[0].putInt( DirectByteBuffer.SS_MSG, piece_number ); buffer[0].putInt( DirectByteBuffer.SS_MSG, piece_offset ); buffer[0].flip( DirectByteBuffer.SS_MSG ); } return buffer; }
@Override public DirectByteBuffer[] getData() { if (buffer == null) { buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_BT_DHT_PORT, 2); short s_port = (short)port; buffer.put(DirectByteBuffer.SS_MSG, (byte)(s_port >> 8)); buffer.put(DirectByteBuffer.SS_MSG, (byte)(s_port & 0xff)); buffer.flip(DirectByteBuffer.SS_MSG); } return new DirectByteBuffer[] {buffer}; }
/** * Convert the given message payload map to a bencoded byte stream. * @param payload to convert * @return bencoded serialization */ public static DirectByteBuffer convertPayloadToBencodedByteStream( Map payload, byte alloc_id ) { byte[] raw_payload; try { raw_payload = BEncoder.encode( payload ); if ( raw_payload == null || raw_payload.length == 0 ){ throw( new Exception( "Encoding failed" )); } } catch( Throwable t ) { System.err.println( "Payload encoding failed: " + payload ); 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; }
private void constructBuffer() { buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_BT_HAND, 68 ); buffer.put( DirectByteBuffer.SS_MSG, (byte)PROTOCOL.length() ); buffer.put( DirectByteBuffer.SS_MSG, PROTOCOL.getBytes() ); buffer.put( DirectByteBuffer.SS_MSG, reserved_bytes ); buffer.put( DirectByteBuffer.SS_MSG, datahash_bytes ); buffer.put( DirectByteBuffer.SS_MSG, peer_id_bytes ); buffer.flip( DirectByteBuffer.SS_MSG ); }
protected proxyStateRelayData() throws IOException { super( socks_connection ); source_buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_PROXY_RELAY, 1024 ); target_buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_PROXY_RELAY, 1024 ); relay_data_state = this; if ( connection.isClosed()){ destroy(); throw( new IOException( "connection closed" )); } connection.setReadState( this ); connection.setWriteState( this ); connection.requestReadSelect( source_channel ); connection.requestReadSelect( target_channel ); connection.setConnected(); }
@Override public DirectByteBuffer[] getData() { if( buffer == null ) { buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_BT_REJECT_REQUEST, 12 ); buffer.putInt( DirectByteBuffer.SS_MSG, piece_number ); buffer.putInt( DirectByteBuffer.SS_MSG, piece_offset ); buffer.putInt( DirectByteBuffer.SS_MSG, length ); buffer.flip( DirectByteBuffer.SS_MSG ); } return new DirectByteBuffer[]{ buffer }; }