Skip to content

Commit dcf2483

Browse files
committed
Fix incorrect padded buffer length method calculation.
Motivation: Buffer#length() returns an incorrect value when dealing with padded ByteBuf. Changes: Buffer#length() should use readableBytes() instead of writerIndex().
1 parent 997bc3e commit dcf2483

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

vertx-core/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public BufferImpl setString(int pos, String str, String enc) {
528528
}
529529

530530
public int length() {
531-
return buffer.writerIndex();
531+
return buffer.readableBytes();
532532
}
533533

534534
public BufferImpl copy() {

vertx-core/src/test/java/io/vertx/tests/buffer/BufferTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ public void testLength() throws Exception {
11401140
byte[] bytes = TestUtils.randomByteArray(100);
11411141
BufferInternal buffer = BufferInternal.buffer(bytes);
11421142
assertEquals(100, BufferInternal.buffer(buffer.getByteBuf()).length());
1143+
assertEquals(100, TestUtils.leftPad(1, BufferInternal.buffer(buffer.getByteBuf())).length());
11431144
}
11441145

11451146
@Test

0 commit comments

Comments
 (0)