我正在使用Java 7和AsynchronousSocketChannel
。我想阅读一个请求(例如HTTP POST
),但如果它大于我使用的ByteBuffer
的大小,我很难想出一个很好的解决方案来阅读完整的请求。例如。如果ByteBuffer
是4048字节,并且HTTP POST包含大于4kB的图像。如何使用小于请求的CompletionHandlers和ByteBuffer读取请求?
有没有什么好的递归解决方案或循环呢?
这是我读请求的代码:
public void readRequest(final AsynchronousSocketChannel ch) {
final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
final StringBuilder strBuilder = new StringBuilder();
final CharsetDecoder decoder = Charset.forName("US-ASCII").newDecoder();
ch.read(buffer, null, new CompletionHandler<Integer, Void>() {
public void completed(Integer bytes, Void att) {
buffer.flip();
try {
decoder.reset();
strBuilder.append(decoder.decode(buffer).toString());
} catch (CharacterCodingException e) {
e.printStackTrace();
}
buffer.clear();
// More data to read or send response
if(bytes != -1) {
// More data to read
ch.read(...);
} else {
// Create and send a response
}
}
public void failed(Throwable exc, Void att) {
exc.printStackTrace();
}
});
}
在哪里我已经写:
// More data to read
ch.read(...);
它看起来像代码重用的好地方,但我不能想出一个好的解决方案有什么方法可以在这里重用CompletionHandler吗?任何建议阅读有限的ByteBuffer
完整的请求?
我想以非阻塞和异步的方式解决这个问题。
所以下面这个建议的文件,这将是一个坏主意,叫AsynchronousServerSocketChannel的从完成处理程序接受的方法? – sloven