|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2023 the original author or authors. |
|
|
|
* Copyright 2002-2024 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -678,6 +678,38 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { |
|
|
|
void readableByteBuffers(DataBufferFactory bufferFactory) throws IOException { |
|
|
|
void readableByteBuffers(DataBufferFactory bufferFactory) throws IOException { |
|
|
|
super.bufferFactory = bufferFactory; |
|
|
|
super.bufferFactory = bufferFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(3); |
|
|
|
|
|
|
|
dataBuffer.write("abc".getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
dataBuffer.readPosition(1); |
|
|
|
|
|
|
|
dataBuffer.writePosition(2); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte[] result = new byte[1]; |
|
|
|
|
|
|
|
try (var iterator = dataBuffer.readableByteBuffers()) { |
|
|
|
|
|
|
|
assertThat(iterator).hasNext(); |
|
|
|
|
|
|
|
int i = 0; |
|
|
|
|
|
|
|
while (iterator.hasNext()) { |
|
|
|
|
|
|
|
ByteBuffer byteBuffer = iterator.next(); |
|
|
|
|
|
|
|
assertThat(byteBuffer.position()).isEqualTo(0); |
|
|
|
|
|
|
|
assertThat(byteBuffer.limit()).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(byteBuffer.capacity()).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(byteBuffer.remaining()).isEqualTo(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byteBuffer.get(result, i, 1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(iterator).isExhausted(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(result).containsExactly('b'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
release(dataBuffer); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedDataBufferAllocatingTest |
|
|
|
|
|
|
|
void readableByteBuffersJoined(DataBufferFactory bufferFactory) { |
|
|
|
|
|
|
|
super.bufferFactory = bufferFactory; |
|
|
|
|
|
|
|
|
|
|
|
DataBuffer dataBuffer = this.bufferFactory.join(Arrays.asList(stringBuffer("a"), |
|
|
|
DataBuffer dataBuffer = this.bufferFactory.join(Arrays.asList(stringBuffer("a"), |
|
|
|
stringBuffer("b"), stringBuffer("c"))); |
|
|
|
stringBuffer("b"), stringBuffer("c"))); |
|
|
|
|
|
|
|
|
|
|
|
@ -703,17 +735,26 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { |
|
|
|
void writableByteBuffers(DataBufferFactory bufferFactory) { |
|
|
|
void writableByteBuffers(DataBufferFactory bufferFactory) { |
|
|
|
super.bufferFactory = bufferFactory; |
|
|
|
super.bufferFactory = bufferFactory; |
|
|
|
|
|
|
|
|
|
|
|
DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(1); |
|
|
|
DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(3); |
|
|
|
|
|
|
|
dataBuffer.write("ab".getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
dataBuffer.readPosition(1); |
|
|
|
|
|
|
|
|
|
|
|
try (DataBuffer.ByteBufferIterator iterator = dataBuffer.writableByteBuffers()) { |
|
|
|
try (DataBuffer.ByteBufferIterator iterator = dataBuffer.writableByteBuffers()) { |
|
|
|
assertThat(iterator).hasNext(); |
|
|
|
assertThat(iterator).hasNext(); |
|
|
|
ByteBuffer byteBuffer = iterator.next(); |
|
|
|
ByteBuffer byteBuffer = iterator.next(); |
|
|
|
byteBuffer.put((byte) 'a'); |
|
|
|
assertThat(byteBuffer.position()).isEqualTo(0); |
|
|
|
dataBuffer.writePosition(1); |
|
|
|
assertThat(byteBuffer.limit()).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(byteBuffer.capacity()).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(byteBuffer.remaining()).isEqualTo(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byteBuffer.put((byte) 'c'); |
|
|
|
|
|
|
|
dataBuffer.writePosition(3); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(iterator).isExhausted(); |
|
|
|
assertThat(iterator).isExhausted(); |
|
|
|
} |
|
|
|
} |
|
|
|
assertThat(dataBuffer.read()).isEqualTo((byte) 'a'); |
|
|
|
byte[] result = new byte[2]; |
|
|
|
|
|
|
|
dataBuffer.read(result); |
|
|
|
|
|
|
|
assertThat(result).containsExactly('b', 'c'); |
|
|
|
|
|
|
|
|
|
|
|
release(dataBuffer); |
|
|
|
release(dataBuffer); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -945,4 +986,21 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { |
|
|
|
assertThat(StandardCharsets.UTF_8.decode(byteBuffer).toString()).isEqualTo("b"); |
|
|
|
assertThat(StandardCharsets.UTF_8.decode(byteBuffer).toString()).isEqualTo("b"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedDataBufferAllocatingTest // gh-31873
|
|
|
|
|
|
|
|
void repeatedWrites(DataBufferFactory bufferFactory) { |
|
|
|
|
|
|
|
super.bufferFactory = bufferFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataBuffer buffer = bufferFactory.allocateBuffer(256); |
|
|
|
|
|
|
|
String name = "Müller"; |
|
|
|
|
|
|
|
int repeatCount = 19; |
|
|
|
|
|
|
|
for (int i = 0; i < repeatCount; i++) { |
|
|
|
|
|
|
|
buffer.write(name, StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
String result = buffer.toString(StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
String expected = name.repeat(repeatCount); |
|
|
|
|
|
|
|
assertThat(result).isEqualTo(expected); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
release(buffer); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|