Various DataBuffer improvements

This commit introduces various improvements in DataBuffer:

- DataBuffer now exposes its read and write position, as well as its
capacity and writable byte count.
- Added DataBuffer.asByteBuffer(int, int)
- DataBufferUtils.read now reads directly into a DataBuffer, rather than
copying a ByteBuffer into a DataBuffer
- TomcatHttpHandler now reads directly into a DataBuffer

Issues: SPR-16068 SPR-16070
This commit is contained in:
Arjen Poutsma
2017-10-19 10:21:49 +02:00
parent d8a7b96b46
commit c7a15260d6
9 changed files with 701 additions and 232 deletions

View File

@@ -30,6 +30,7 @@ import org.apache.catalina.connector.CoyoteOutputStream;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
/**
* {@link ServletHttpHandlerAdapter} extension that uses Tomcat APIs for reading
@@ -67,21 +68,32 @@ public class TomcatHttpHandlerAdapter extends ServletHttpHandlerAdapter {
@Override
protected DataBuffer readFromInputStream() throws IOException {
DataBuffer buffer = getDataBufferFactory().allocateBuffer(getBufferSize());
ByteBuffer byteBuffer = buffer.asByteBuffer();
byteBuffer.limit(byteBuffer.capacity());
boolean release = true;
int capacity = getBufferSize();
DataBuffer dataBuffer = getDataBufferFactory().allocateBuffer(capacity);
try {
ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, capacity);
ServletRequest request = getNativeRequest();
int read = ((CoyoteInputStream) request.getInputStream()).read(byteBuffer);
if (logger.isTraceEnabled()) {
logger.trace("read:" + read);
ServletRequest request = getNativeRequest();
int read = ((CoyoteInputStream) request.getInputStream()).read(byteBuffer);
if (logger.isTraceEnabled()) {
logger.trace("read:" + read);
}
if (read > 0) {
dataBuffer.writePosition(read);
release = false;
return dataBuffer;
}
else {
return null;
}
}
if (read > 0) {
return getDataBufferFactory().wrap(byteBuffer);
finally {
if (release) {
DataBufferUtils.release(dataBuffer);
}
}
return null;
}
}