* @see de.netseeker.ejoe.io.DataChannel#writeHeader(de.netseeker.ejoe.ConnectionHeader, java.nio.ByteBuffer, long)
*/
public void writeHeader( ConnectionHeader header, ByteBuffer buffer, long timeout ) throws IOException
{
ByteBuffer headerBuf = null;
SocketChannel channel = header.getChannel();
boolean noBuffer = (buffer == null);
int length = !noBuffer ? buffer.limit() : 0;
if ( !noBuffer ) buffer.mark();
if ( header.isClient() )
{
HttpRequest request = new HttpRequest( header, (header.getAttachementInfo() != null) ? header
.getAttachementInfo().toString() : HttpRequest.HTTP_POST );
if ( !noBuffer ) request.addData( buffer );
headerBuf = request.toByteBuffer();
if ( logger.isLoggable( Level.FINEST ) )
{
logger.log( Level.FINEST, "Preparing to write client request with " + headerBuf.limit() + " bytes:\n"
+ IOUtil.decodeToString( headerBuf ) );
}
}
else
{
HttpResponse response = new HttpResponse( header, (header.getAttachementInfo() != null) ? header
.getAttachementInfo().toString() : HttpResponse.HTTP_OK );
if ( !noBuffer ) response.addData( buffer );
headerBuf = response.toByteBuffer();
if ( logger.isLoggable( Level.FINEST ) )
{
logger.log( Level.FINEST, "Preparing to write server response with " + headerBuf.limit() + " bytes:\n"
+ IOUtil.decodeToString( headerBuf ) );
}
}
try
{
semiBlockingWrite( channel, headerBuf, timeout );
IOUtil.setSendBufferSize( channel.socket(), length );
}
catch ( IncompleteIOException ioe )
{
logger.log( Level.FINEST, "Incomplete header write detected, skip this request." );
throw new IncompleteIOException( null, SelectionKey.OP_WRITE );