Write CharSequence instances to DataBuffers

Prior to this commit, one could write a `CharSequence` to an existing
`DataBuffer` instance by turning it into a byte array or `ByteBuffer`
first. This had the following disadvantages:

1. Memory allocation was not efficient (not leveraging pooled memory
when available)
2. Dealing with `CharsetEncoder` is not always easy
3. `DataBuffer` implementations, like `NettyDataBuffer` can use
optimized implementations in some cases

This commit adds a new `DataBuffer#write(CharSequence, Charset)` method
for those cases and also an `ensureCapacity` method useful for checking
that the current buffer has enough capacity to write to it..

Issue: SPR-17558
This commit is contained in:
Brian Clozel
2018-12-20 21:33:33 +01:00
parent 5e4a8966ee
commit 6361b0cb23
7 changed files with 190 additions and 17 deletions

View File

@@ -23,6 +23,7 @@ import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.function.IntPredicate;
import javax.net.ssl.SSLSession;
@@ -294,6 +295,11 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
return this.dataBuffer.capacity(newCapacity);
}
@Override
public DataBuffer ensureCapacity(int capacity) {
return this.dataBuffer.ensureCapacity(capacity);
}
@Override
public byte getByte(int index) {
return this.dataBuffer.getByte(index);
@@ -343,6 +349,11 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
return this.dataBuffer.write(byteBuffers);
}
@Override
public DataBuffer write(CharSequence charSequence, Charset charset) {
return this.dataBuffer.write(charSequence, charset);
}
@Override
public DataBuffer slice(int index, int length) {
return this.dataBuffer.slice(index, length);