JAVA NIO多线程服务器1.2版 (二)

2014-11-24 02:29:14 · 作者: · 浏览: 6
Exception e)
{
log.info(e);
}
}
}


public class Writer implements Reactor
{
private static final Log log = LogFactory.getLog(Writer.class);

private ByteBuffer output;

public Writer(ByteBuffer output)
{
this.output=output;
}

public void execute(SelectionKey key)
{
SocketChannel sc = (SocketChannel) key.channel();
try
{
while(sc.isConnected() && output.hasRemaining())
{
int len=sc.write(output);

if(len<0)
{
throw new EOFException();
}
if(len==0)
{
key.interestOps(SelectionKey.OP_WRITE);
key.selector().wakeup();
break;
}
}
if(!output.hasRemaining())
{
output.clear();
key.cancel();
sc.close();
}
}
catch(IOException e)
{
log.info(e);
}
}
}