ByteBufferOutputStream out = null;
ByteBuffer dataBuf = null;
SerializeAdapter adapter = this._receiverInfo.getAdapterName() != null ? AdapterFactory
.createAdapter( this._receiverInfo.getAdapterName() ) : null;
WritableByteChannel channel = this._receiverInfo.getChannel();
DataChannel dataChannel = DataChannel.getInstance( this._receiverInfo );
try
{
if ( this._receiverInfo.hasAttachment() )
{
// usual way: convert the serialized object to a ByteBuffer
if ( !this._receiverInfo.isDirect() )
{
out = new ByteBufferOutputStream();
serialize( adapter, out );
dataBuf = out.getBackingBuffer();
}
// direct mode: just use the attachement
else
{
ByteBuffer tmp = (ByteBuffer) this._receiverInfo.getAttachment();
if ( tmp.position() > 0 )
{
tmp.flip();
}
dataBuf = tmp.duplicate();
ByteBufferAllocator.collect( tmp );
}
if ( dataBuf.position() > 0 )
{
dataBuf.flip();
}
this._receiverInfo.releaseAttachment();
dataChannel.writeHeader( this._receiverInfo, dataBuf, EJConstants.EJOE_CONNECTION_TIMEOUT );
if ( log.isLoggable( Level.FINE ) )
{
byte[] tmp = new byte[dataBuf.remaining()];
dataBuf.get( tmp );
dataBuf.position( 0 );
log.log( Level.FINE, "Going to send server response:\n"
+ new String( tmp, EJConstants.EJOE_DEFAULT_CHARSET ) );
}
dataChannel.nonBlockingWrite( channel, dataBuf );
}
else if ( this._receiverInfo.hasWaitingBuffer() )
{
dataBuf = this._receiverInfo.getWaitingBuffer();
dataChannel.nonBlockingWrite( channel, dataBuf );
}
// seems that we have no answer for the client
else
{
dataChannel.writeHeader( this._receiverInfo, null, EJConstants.EJOE_CONNECTION_TIMEOUT );
}
log.log( Level.FINE, "Server response sent." );
}
finally