提交 090bfa63 编写于 作者: A Arjen Poutsma

Add DataBuffer::toString(Charset)

Closes gh-23317
上级 c8704ce4
......@@ -355,4 +355,28 @@ public interface DataBuffer {
*/
OutputStream asOutputStream();
/**
* Return this buffer's data a String using the specified charset. Default implementation
* delegates to {@code toString(readPosition(), readableByteCount(), charset)}.
*
* @param charset the character set to use
* @return a string representation of all this buffers data
* @since 5.2
*/
default String toString(Charset charset) {
Assert.notNull(charset, "Charset must not be null");
return toString(readPosition(), readableByteCount(), charset);
}
/**
* Return a part of this buffer's data as a String using the specified charset.
*
* @param index the index at which to start the string
* @param length the number of bytes to use for the string
* @param charset the charset to use
* @return a string representation of a part of this buffers data
* @since 5.2
*/
String toString(int index, int length, Charset charset);
}
......@@ -21,6 +21,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.function.IntPredicate;
......@@ -381,6 +382,28 @@ public class DefaultDataBuffer implements DataBuffer {
}
@Override
public String toString(int index, int length, Charset charset) {
checkIndex(index, length);
Assert.notNull(charset, "Charset must not be null");
byte[] bytes;
int offset;
if (this.byteBuffer.hasArray()) {
bytes = this.byteBuffer.array();
offset = this.byteBuffer.arrayOffset() + index;
}
else {
bytes = new byte[length];
offset = 0;
ByteBuffer duplicate = this.byteBuffer.duplicate();
duplicate.clear().position(index).limit(index + length);
duplicate.get(bytes, 0, length);
}
return new String(bytes, offset, length, charset);
}
/**
* Calculate the capacity of the buffer.
* @see io.netty.buffer.AbstractByteBufAllocator#calculateNewCapacity(int, int)
......
......@@ -293,6 +293,18 @@ public class NettyDataBuffer implements PooledDataBuffer {
return new ByteBufOutputStream(this.byteBuf);
}
@Override
public String toString(Charset charset) {
Assert.notNull(charset, "Charset must not be null");
return this.byteBuf.toString(charset);
}
@Override
public String toString(int index, int length, Charset charset) {
Assert.notNull(charset, "Charset must not be null");
return this.byteBuf.toString(index, length, charset);
}
@Override
public boolean isAllocated() {
return this.byteBuf.refCnt() > 0;
......
......@@ -235,6 +235,44 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void toStringNullCharset() {
DataBuffer buffer = createDataBuffer(1);
try {
assertThatIllegalArgumentException().isThrownBy(() ->
buffer.toString(null));
}
finally {
release(buffer);
}
}
@Test
public void toStringUtf8() {
String spring = "Spring";
byte[] bytes = spring.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = createDataBuffer(bytes.length);
buffer.write(bytes);
String result = buffer.toString(StandardCharsets.UTF_8);
assertThat(result).isEqualTo(spring);
release(buffer);
}
@Test
public void toStringSection() {
String spring = "Spring";
byte[] bytes = spring.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = createDataBuffer(bytes.length);
buffer.write(bytes);
String result = buffer.toString(1, 3, StandardCharsets.UTF_8);
assertThat(result).isEqualTo("pri");
release(buffer);
}
@Test
public void inputStream() throws IOException {
DataBuffer buffer = createDataBuffer(4);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册