Skip to content

Commit

Permalink
Remove code that is not needed anymore due the changes of how extendW…
Browse files Browse the repository at this point in the history
…ith(...) works now (#12453)

Motivation:

We can remove some code-duplication now as CompositeBuffer.extendWith(...) now takes care of all the extra logic

Modifications:

Remove not needed code

Result:

Cleanup
  • Loading branch information
normanmaurer committed Jun 10, 2022
1 parent 0a895b0 commit 6af0085
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.CompositeBuffer;
import io.netty5.buffer.api.DefaultBufferAllocators;
import io.netty5.util.Resource;
import io.netty5.util.Send;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.channel.embedded.EmbeddedChannel;
Expand Down Expand Up @@ -141,18 +140,7 @@ private Buffer decompressContent(ChannelHandlerContext ctx, WebSocketFrame msg)
partUncompressedContent.close();
continue;
}
if (partUncompressedContent.readerOffset() != 0) {
// We can't compose buffers that will have reader-offset gaps.
partUncompressedContent.readSplit(0).close(); // Trim off already-read bytes at the beginning.
}
if (partUncompressedContent.writableBytes() > 0) {
// We also can't compose buffers that will have writer-offset gaps.
// Trim off the excess with split.
bufferList.add(partUncompressedContent.split().send());
partUncompressedContent.close();
} else {
bufferList.add(partUncompressedContent.send());
}
bufferList.add(partUncompressedContent.send());
}
CompositeBuffer compositeDecompressedContent = ctx.bufferAllocator().compose(bufferList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,7 @@ private Buffer compressContent(ChannelHandlerContext ctx, WebSocketFrame msg) {
partCompressedContent.close();
continue;
}
if (partCompressedContent.readerOffset() != 0) {
// We can't compose buffers that will have reader-offset gaps.
partCompressedContent.readSplit(0).close(); // Trim off already-read bytes at the beginning.
}
if (partCompressedContent.writableBytes() > 0) {
// We also can't compose buffers that will have writer-offset gaps.
// Trim off the excess with split.
bufferList.add(partCompressedContent.split().send());
partCompressedContent.close();
} else {
bufferList.add(partCompressedContent.send());
}
bufferList.add(partCompressedContent.send());
}

if (bufferList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
import io.netty5.channel.socket.ChannelInputShutdownEvent;
import io.netty5.util.Attribute;
import io.netty5.util.AttributeKey;
import io.netty5.util.Send;
import io.netty5.util.concurrent.EventExecutor;
import io.netty5.util.concurrent.Future;
import io.netty5.util.concurrent.Promise;
import io.netty5.util.internal.StringUtil;

import java.net.SocketAddress;
import java.util.Arrays;

import static io.netty5.util.internal.MathUtil.safeFindNextPositivePowerOfTwo;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -634,31 +636,19 @@ public Buffer cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in) {
cumulation.close();
cumulation = tmp;
}
CompositeBuffer composite;
if (CompositeBuffer.isComposite(cumulation)) {
composite = (CompositeBuffer) cumulation;
} else {
composite = alloc.compose(cumulation.send());
}
if (in.readOnly()) {
composite.extendWith(in.copy().send());
} else {
if (in.readerOffset() != 0) {
// We can't compose buffers that will have reader-offset gaps.
in.readSplit(0).close(); // Trim off already-read bytes at the beginning.
}
if (in.writableBytes() > 0) {
// We also can't compose buffers that will have writer-offset gaps.
// Trim off the excess with split.
composite.extendWith(in.split().send());
} else {
composite.extendWith(in.send());
}
CompositeBuffer composite = (CompositeBuffer) cumulation;
composite.extendWith(prepareInForCompose(in));
return composite;
}
return composite;
return alloc.compose(Arrays.asList(cumulation.send(), prepareInForCompose(in)));
}
}

private static Send<Buffer> prepareInForCompose(Buffer in) {
return in.readOnly() ? in.copy().send() : in.send();
}

@Override
public Buffer discardSomeReadBytes(Buffer cumulation) {
// Compact is slow on composite buffers, and we also need to avoid leaving any writable space at the end.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ static CompositeBuffer compose(BufferAllocator allocator, Supplier<Buffer> suppl
if (msg == null) {
break;
}
if (msg.readerOffset() != 0) {
// We can't compose buffers that will have reader-offset gaps.
msg.readSplit(0).close(); // Trim off already-read bytes at the beginning.
}
if (msg.writableBytes() > 0) {
// We also can't compose buffers that will have writer-offset gaps.
// Trim off the excess with split.
bufferList.add(msg.split().send());
} else {

// This is needed at the moment as otherwise we will see:
//
// java.lang.AssertionError: Method should not be used.
// at io.netty5.buffer.api.DefaultCompositeBuffer$TornBufferAccessor
// .readByte(DefaultCompositeBuffer.java:1575)
if (msg.readableBytes() != 0) {
bufferList.add(msg.send());
}
}
Expand Down

0 comments on commit 6af0085

Please sign in to comment.