GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid UnsupportedOperationException in CompositeeByteBuf when noUnsaf… · netty/netty@db33c48

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • ByteBufUtil.java
    • test/java/io/netty/buffer
      • ByteBufUtilTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/ByteBufUtil.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,7 @@ static int writeUtf8(AbstractByteBuf buffer, int writerIndex, int reservedBytes,
923923
if (buffer.hasArray()) {
924924
return safeArrayWriteUtf8(buffer.array(), buffer.arrayOffset() + writerIndex, seq, start, end);
925925
}
926-
if (buffer.isDirect()) {
927-
assert buffer.nioBufferCount() == 1;
926+
if (buffer.isDirect() && buffer.nioBufferCount() == 1) {
928927
final ByteBuffer internalDirectBuffer = buffer.internalNioBuffer(writerIndex, reservedBytes);
929928
final int bufferPosition = internalDirectBuffer.position();
930929
return safeDirectWriteUtf8(internalDirectBuffer, bufferPosition, seq, start, end);
Collapse file

‎buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java
+34-7Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545

4646
public class ByteBufUtilTest {
4747
private static final String PARAMETERIZED_NAME = "bufferType = {0}";
48+
private final AdaptiveByteBufAllocator adaptiveByteBufAllocator = new AdaptiveByteBufAllocator();
4849

4950
private enum BufferType {
50-
DIRECT_UNPOOLED, DIRECT_POOLED, HEAP_POOLED, HEAP_UNPOOLED
51+
DIRECT_UNPOOLED, DIRECT_POOLED, DIRECT_ADAPTIVE, HEAP_POOLED, HEAP_UNPOOLED, HEAP_ADAPTIVE
5152
}
5253

5354
private ByteBuf buffer(BufferType bufferType, int capacity) {
@@ -61,17 +62,43 @@ private ByteBuf buffer(BufferType bufferType, int capacity) {
6162
return PooledByteBufAllocator.DEFAULT.directBuffer(capacity);
6263
case HEAP_POOLED:
6364
return PooledByteBufAllocator.DEFAULT.buffer(capacity);
65+
case DIRECT_ADAPTIVE:
66+
return adaptiveByteBufAllocator.directBuffer(capacity);
67+
case HEAP_ADAPTIVE:
68+
return adaptiveByteBufAllocator.heapBuffer(capacity);
6469
default:
6570
throw new AssertionError("unexpected buffer type: " + bufferType);
6671
}
6772
}
6873

74+
private CompositeByteBuf compositeByteBuf(BufferType bufferType) {
75+
switch (bufferType) {
76+
77+
case DIRECT_UNPOOLED:
78+
return UnpooledByteBufAllocator.DEFAULT.compositeDirectBuffer();
79+
case HEAP_UNPOOLED:
80+
return UnpooledByteBufAllocator.DEFAULT.compositeHeapBuffer();
81+
case DIRECT_POOLED:
82+
return PooledByteBufAllocator.DEFAULT.compositeDirectBuffer();
83+
case HEAP_POOLED:
84+
return PooledByteBufAllocator.DEFAULT.compositeHeapBuffer();
85+
case DIRECT_ADAPTIVE:
86+
return adaptiveByteBufAllocator.compositeDirectBuffer();
87+
case HEAP_ADAPTIVE:
88+
return adaptiveByteBufAllocator.compositeHeapBuffer();
89+
default:
90+
throw new AssertionError("unexpected buffer type: " + bufferType);
91+
}
92+
}
93+
6994
public static Collection<Object[]> noUnsafe() {
7095
return Arrays.asList(new Object[][] {
7196
{ BufferType.DIRECT_POOLED },
7297
{ BufferType.DIRECT_UNPOOLED },
7398
{ BufferType.HEAP_POOLED },
74-
{ BufferType.HEAP_UNPOOLED }
99+
{ BufferType.HEAP_UNPOOLED },
100+
{ BufferType.DIRECT_ADAPTIVE },
101+
{ BufferType.HEAP_ADAPTIVE }
75102
});
76103
}
77104

@@ -403,7 +430,7 @@ public void testWriteUsAsciiComposite(BufferType bufferType) {
403430
String usAscii = "NettyRocks";
404431
ByteBuf buf = buffer(bufferType, 16);
405432
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
406-
ByteBuf buf2 = Unpooled.compositeBuffer().addComponent(
433+
ByteBuf buf2 = compositeByteBuf(bufferType).addComponent(
407434
buffer(bufferType, 8)).addComponent(buffer(bufferType, 24));
408435
// write some byte so we start writing with an offset.
409436
buf2.writeByte(1);
@@ -422,7 +449,7 @@ public void testWriteUsAsciiCompositeWrapped(BufferType bufferType) {
422449
String usAscii = "NettyRocks";
423450
ByteBuf buf = buffer(bufferType, 16);
424451
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
425-
ByteBuf buf2 = new WrappedCompositeByteBuf(Unpooled.compositeBuffer().addComponent(
452+
ByteBuf buf2 = new WrappedCompositeByteBuf(compositeByteBuf(bufferType).addComponent(
426453
buffer(bufferType, 8)).addComponent(buffer(bufferType, 24)));
427454
// write some byte so we start writing with an offset.
428455
buf2.writeByte(1);
@@ -456,7 +483,7 @@ public void testWriteUtf8Composite(BufferType bufferType) {
456483
String utf8 = "Some UTF-8 like äÄ∏ŒŒ";
457484
ByteBuf buf = buffer(bufferType, 16);
458485
buf.writeBytes(utf8.getBytes(CharsetUtil.UTF_8));
459-
ByteBuf buf2 = Unpooled.compositeBuffer().addComponent(
486+
ByteBuf buf2 = compositeByteBuf(bufferType).addComponent(
460487
buffer(bufferType, 8)).addComponent(buffer(bufferType, 24));
461488
// write some byte so we start writing with an offset.
462489
buf2.writeByte(1);
@@ -475,7 +502,7 @@ public void testWriteUtf8CompositeWrapped(BufferType bufferType) {
475502
String utf8 = "Some UTF-8 like äÄ∏ŒŒ";
476503
ByteBuf buf = buffer(bufferType, 16);
477504
buf.writeBytes(utf8.getBytes(CharsetUtil.UTF_8));
478-
ByteBuf buf2 = new WrappedCompositeByteBuf(Unpooled.compositeBuffer().addComponent(
505+
ByteBuf buf2 = new WrappedCompositeByteBuf(compositeByteBuf(bufferType).addComponent(
479506
buffer(bufferType, 8)).addComponent(buffer(bufferType, 24)));
480507
// write some byte so we start writing with an offset.
481508
buf2.writeByte(1);
@@ -816,7 +843,7 @@ private static void testDecodeString(String text, Charset charset) {
816843
@ParameterizedTest(name = PARAMETERIZED_NAME)
817844
@MethodSource("noUnsafe")
818845
public void testToStringDoesNotThrowIndexOutOfBounds(BufferType bufferType) {
819-
CompositeByteBuf buffer = Unpooled.compositeBuffer();
846+
CompositeByteBuf buffer = compositeByteBuf(bufferType);
820847
try {
821848
byte[] bytes = "1234".getBytes(CharsetUtil.UTF_8);
822849
buffer.addComponent(buffer(bufferType, bytes.length).writeBytes(bytes));

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.