我现在使用的代码:
Pooled<ByteBuffer> pooledByteBuffer = exchange.getConnection().getBufferPool().allocate();
ByteBuffer byteBuffer = pooledByteBuffer.getResource();
int limit = byteBuffer.limit();
byteBuffer.clear();
exchange.getRequestChannel().read(byteBuffer);
int pos = byteBuffer.position();
byteBuffer.rewind();
byte[] bytes = new byte[pos];
byteBuffer.get(bytes);
String requestBody = new String(bytes, Charset.forName("UTF-8") );
byteBuffer.clear();
pooledByteBuffer.free();
它似乎工作正常,但我不确定clear()
在将其返回到池之前是否需要 ByteBuffer。我什至不确定使用exchange.getConnection().getBufferPool().allocate();
. 文档中没有太多关于它的内容。