/*
* 如果原缓冲区的剩余容量不够,就创建一个新的缓冲区,容量为原来的两倍,
* 把原来缓冲区的数据拷贝到新缓冲区
*/
protected void resizeRequestBuffer(int remaining) {
if (requestBuffer.remaining() < remaining) {
// 把容量增大到原来的两倍
ByteBuffer bb = ByteBuffer.allocate(requestBuffer.capacity() * 2);
requestBuffer.flip();
bb.put(requestBuffer); //把原来缓冲区中的数据拷贝到新的缓冲区
requestBuffer = bb;
}
}
/*
* 接收数据,把它们存放到requestBuffer中,如果requsetBuffer的剩余容量不足5%,
* 就通过resizeRequestBuffer()方法扩充容量
*/
public int read() throws IOException {
resizeRequestBuffer(requestBufferSize/20);
return socketChannel.read(requestBuffer);
}
/* 返回requestBuffer,它存放了请求数据 */
public ByteBuffer getReadBuf() {
return requestBuffer;
}
/* 发送参数指定的ByteBuffer中的数据 */
public int write(ByteBuffer src) throws IOException {
return socketChannel.write(src);
}
/* 把FileChannel中的数据写到SocketChannel中 */
public long transferTo(FileChannel fc, long pos, long len) throws IOException {
return fc.transferTo(pos, len, socketChannel);
}
/* 关闭SocketChannel */
public void close() throws IOException {
socketChannel.close();
}
}
3.负责处理各种事件的Handler接口
Handler接口负责处理各种事件,它的定义如下:
import java.io.*;
import java.nio.channels.*;
public interface Handler {
public void handle(SelectionKey key) throws IOException;
}
Handler接口有两个实现类:AcceptHandler和RequestHandler。AcceptHandler负责处理接收连结就绪事件,RequestHandler负责处理读就绪和写就绪事件。更确切地说,RequestHandler负责接收客户的HTTP请求,以及发送HTTP响应。
|