GeezerVison is showing github.com — ByteBuf slice duplicates are not slices · Issue #12919 · netty/netty
ByteBuf slice duplicates are not slices #12919DescriptionExpected behavior
A ByteBuf duplicated slice remains a slice.
Actual behavior
A ByteBuf duplicated slice is a duplicate and not a slice anymore.
Steps to reproduce
Duplicating a sliced ByteBuf is enough.
Minimal yet complete reproducer code (or URL to code)
ByteBuf helloWorld = Unpooled.copiedBuffer("Hello World", StandardCharsets.UTF_8);
ByteBuf hello = helloWorld.slice(0, 5);
try {
hello.writeByte('_');
fail();
} catch (Exception ignore) {
}
try {
hello.duplicate().writeByte('_');
fail(); // Fails here
} catch (Exception ignore) {
}
Netty version
Most I'd say
JVM version (e.g. java -version)
N/A
OS version (e.g. uname -a)
N/A Activityfranz1981 commented on Oct 21, 2022 I would expect as well that It won't be expanded too chrisvest commented on Oct 21, 2022 It might be illogical or unexpected, but it is specified to behave this way. The javadocs for duplicate() say that it returns…
A buffer whose readable content is equivalent to the buffer returned by {@link #slice()}.
However this buffer will share the capacity of the underlying buffer,
and therefore allows access to all of the underlying content if necessary.
So duplicate() always gives you duplicates, and slice() always gives you slices. Reacted by gamecraft and Sergii Tkachenko vietj commented on Oct 24, 2022 ContributorAuthor More actions @chrisvest I hear that, however it is not possible from the user code perspective to determine whether the ByteBuf is a duplicate or a slice, if it needs to gets a duplicate of a slice with the same semantic, which makes the API not convenient for such use case (and I know that slices are going away in Netty 5 of course) lhotari commented on Jun 3, 2024 I made #14093 with the title "Duplicate of slice should have the same capacity as the original slice so that it's not writable" to address this issue. normanmaurer commented on Jun 3, 2024 @chrisvest I actually agree with @lhotari here... If you duplicate a slice it should share the same capacity as the buffer you called duplicate on, which in this case is a slice. It should not share the capacity of the unwrapped slice. So with this I would say #14093 is correct. Also this is inline with the javadoc you posted as the "underlying buffer" would be the sliced buffer and not the original buffer. sergiitk commented on Jun 14, 2024 For posterity: looks like this behavioral change caused grpc/grpc-java#11284, as we relied on "duplicate() always gives you duplicates, and slice() always gives you slices." behavior (#12919 (comment)) to get around the issue with component() of a double-wrapped CompositeByteBuf returning a slice with incorrect indexes, #12844. Reacted by Francesco Nigro and Julien Viet
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|