Simplify conversion of DataBuffer to String

See gh-24786
This commit is contained in:
Сергей Цыпанов
2020-03-26 11:39:56 +02:00
committed by Rossen Stoyanchev
parent ac11acb532
commit f17b0125ff
10 changed files with 24 additions and 38 deletions

View File

@@ -191,8 +191,8 @@ public class DefaultDataBuffer implements DataBuffer {
if (newCapacity > oldCapacity) {
ByteBuffer oldBuffer = this.byteBuffer;
ByteBuffer newBuffer = allocate(newCapacity, oldBuffer.isDirect());
((Buffer) oldBuffer).position(0).limit(oldBuffer.capacity());
((Buffer) newBuffer).position(0).limit(oldBuffer.capacity());
oldBuffer.position(0).limit(oldBuffer.capacity());
newBuffer.position(0).limit(oldBuffer.capacity());
newBuffer.put(oldBuffer);
newBuffer.clear();
setNativeBuffer(newBuffer);
@@ -205,8 +205,8 @@ public class DefaultDataBuffer implements DataBuffer {
writePosition = newCapacity;
writePosition(writePosition);
}
((Buffer) oldBuffer).position(readPosition).limit(writePosition);
((Buffer) newBuffer).position(readPosition).limit(writePosition);
oldBuffer.position(readPosition).limit(writePosition);
newBuffer.position(readPosition).limit(writePosition);
newBuffer.put(oldBuffer);
newBuffer.clear();
}
@@ -265,7 +265,7 @@ public class DefaultDataBuffer implements DataBuffer {
ByteBuffer tmp = this.byteBuffer.duplicate();
int limit = this.readPosition + length;
((Buffer) tmp).clear().position(this.readPosition).limit(limit);
tmp.clear().position(this.readPosition).limit(limit);
tmp.get(destination, offset, length);
this.readPosition += length;
@@ -295,7 +295,7 @@ public class DefaultDataBuffer implements DataBuffer {
ByteBuffer tmp = this.byteBuffer.duplicate();
int limit = this.writePosition + length;
((Buffer) tmp).clear().position(this.writePosition).limit(limit);
tmp.clear().position(this.writePosition).limit(limit);
tmp.put(source, offset, length);
this.writePosition += length;
@@ -324,7 +324,7 @@ public class DefaultDataBuffer implements DataBuffer {
int length = source.remaining();
ByteBuffer tmp = this.byteBuffer.duplicate();
int limit = this.writePosition + source.remaining();
((Buffer) tmp).clear().position(this.writePosition).limit(limit);
tmp.clear().position(this.writePosition).limit(limit);
tmp.put(source);
this.writePosition += length;
}
@@ -340,7 +340,7 @@ public class DefaultDataBuffer implements DataBuffer {
buffer.position(index);
ByteBuffer slice = this.byteBuffer.slice();
// Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer
((Buffer) slice).limit(length);
slice.limit(length);
return new SlicedDefaultDataBuffer(slice, this.dataBufferFactory, length);
}
finally {

View File

@@ -249,10 +249,8 @@ public abstract class AbstractEncoderTests<E extends Encoder<?>> extends Abstrac
*/
protected Consumer<DataBuffer> expectString(String expected) {
return dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
String actual = dataBuffer.toString(UTF_8);
release(dataBuffer);
String actual = new String(resultBytes, UTF_8);
assertThat(actual).isEqualTo(expected);
};

View File

@@ -54,9 +54,9 @@ public abstract class DataBufferTestUtils {
* @return the string representation of the given data buffer
*/
public static String dumpString(DataBuffer buffer, Charset charset) {
Assert.notNull(buffer, "'buffer' must not be null");
Assert.notNull(charset, "'charset' must not be null");
byte[] bytes = dumpBytes(buffer);
return new String(bytes, charset);
return buffer.toString(charset);
}
}