Introduce DataBufferWrapper

This commit introduces the DataBufferWrapper, a wrapper for DataBuffers,
and uses it in applicable use cases.
This commit is contained in:
Arjen Poutsma
2019-07-19 11:36:52 +02:00
parent 24e96b6c79
commit c8704ce473
5 changed files with 237 additions and 484 deletions

View File

@@ -17,15 +17,11 @@
package org.springframework.http.server.reactive;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntPredicate;
import javax.net.ssl.SSLSession;
import io.undertow.connector.ByteBufferPool;
@@ -38,6 +34,7 @@ import reactor.core.publisher.Flux;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DataBufferWrapper;
import org.springframework.core.io.buffer.PooledDataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
@@ -206,24 +203,23 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
}
private static class UndertowDataBuffer implements PooledDataBuffer {
private final DataBuffer dataBuffer;
private static class UndertowDataBuffer extends DataBufferWrapper implements PooledDataBuffer {
private final PooledByteBuffer pooledByteBuffer;
private final AtomicInteger refCount;
public UndertowDataBuffer(DataBuffer dataBuffer, PooledByteBuffer pooledByteBuffer) {
this.dataBuffer = dataBuffer;
super(dataBuffer);
this.pooledByteBuffer = pooledByteBuffer;
this.refCount = new AtomicInteger(1);
}
private UndertowDataBuffer(DataBuffer dataBuffer, PooledByteBuffer pooledByteBuffer,
AtomicInteger refCount) {
super(dataBuffer);
this.refCount = refCount;
this.dataBuffer = dataBuffer;
this.pooledByteBuffer = pooledByteBuffer;
}
@@ -235,7 +231,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
@Override
public PooledDataBuffer retain() {
this.refCount.incrementAndGet();
DataBufferUtils.retain(this.dataBuffer);
DataBufferUtils.retain(dataBuffer());
return this;
}
@@ -244,7 +240,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
int refCount = this.refCount.decrementAndGet();
if (refCount == 0) {
try {
return DataBufferUtils.release(this.dataBuffer);
return DataBufferUtils.release(dataBuffer());
}
finally {
this.pooledByteBuffer.close();
@@ -253,157 +249,12 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
return false;
}
@Override
public DataBufferFactory factory() {
return this.dataBuffer.factory();
}
@Override
public int indexOf(IntPredicate predicate, int fromIndex) {
return this.dataBuffer.indexOf(predicate, fromIndex);
}
@Override
public int lastIndexOf(IntPredicate predicate, int fromIndex) {
return this.dataBuffer.lastIndexOf(predicate, fromIndex);
}
@Override
public int readableByteCount() {
return this.dataBuffer.readableByteCount();
}
@Override
public int writableByteCount() {
return this.dataBuffer.writableByteCount();
}
@Override
public int readPosition() {
return this.dataBuffer.readPosition();
}
@Override
public DataBuffer readPosition(int readPosition) {
return this.dataBuffer.readPosition(readPosition);
}
@Override
public int writePosition() {
return this.dataBuffer.writePosition();
}
@Override
public DataBuffer writePosition(int writePosition) {
this.dataBuffer.writePosition(writePosition);
return this;
}
@Override
public int capacity() {
return this.dataBuffer.capacity();
}
@Override
public DataBuffer capacity(int newCapacity) {
this.dataBuffer.capacity(newCapacity);
return this;
}
@Override
public DataBuffer ensureCapacity(int capacity) {
this.dataBuffer.ensureCapacity(capacity);
return this;
}
@Override
public byte getByte(int index) {
return this.dataBuffer.getByte(index);
}
@Override
public byte read() {
return this.dataBuffer.read();
}
@Override
public DataBuffer read(byte[] destination) {
this.dataBuffer.read(destination);
return this;
}
@Override
public DataBuffer read(byte[] destination, int offset, int length) {
this.dataBuffer.read(destination, offset, length);
return this;
}
@Override
public DataBuffer write(byte b) {
this.dataBuffer.write(b);
return this;
}
@Override
public DataBuffer write(byte[] source) {
this.dataBuffer.write(source);
return this;
}
@Override
public DataBuffer write(byte[] source, int offset, int length) {
this.dataBuffer.write(source, offset, length);
return this;
}
@Override
public DataBuffer write(DataBuffer... buffers) {
this.dataBuffer.write(buffers);
return this;
}
@Override
public DataBuffer write(ByteBuffer... byteBuffers) {
this.dataBuffer.write(byteBuffers);
return this;
}
@Override
public DataBuffer write(CharSequence charSequence, Charset charset) {
this.dataBuffer.write(charSequence, charset);
return this;
}
@Override
public DataBuffer slice(int index, int length) {
DataBuffer slice = this.dataBuffer.slice(index, length);
DataBuffer slice = dataBuffer().slice(index, length);
return new UndertowDataBuffer(slice, this.pooledByteBuffer, this.refCount);
}
@Override
public ByteBuffer asByteBuffer() {
return this.dataBuffer.asByteBuffer();
}
@Override
public ByteBuffer asByteBuffer(int index, int length) {
return this.dataBuffer.asByteBuffer(index, length);
}
@Override
public InputStream asInputStream() {
return this.dataBuffer.asInputStream();
}
@Override
public InputStream asInputStream(boolean releaseOnClose) {
return this.dataBuffer.asInputStream(releaseOnClose);
}
@Override
public OutputStream asOutputStream() {
return this.dataBuffer.asOutputStream();
}
}
}