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 — Auto-port 4.1: Fix the region copied by Unpooled.copiedBuffer(CharSeq… · lltopk/netty@815f37d

File tree

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

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/Unpooled.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ public static ByteBuf copiedBuffer(
645645
}
646646

647647
buf = buf.slice();
648-
buf.limit(length);
648+
buf.limit(offset + length);
649649
buf.position(offset);
650650
return copiedBuffer(buf, charset);
651651
}
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/UnpooledTest.java
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStream;
2424
import java.nio.ByteBuffer;
2525
import java.nio.ByteOrder;
26+
import java.nio.CharBuffer;
2627
import java.nio.channels.ScatteringByteChannel;
2728
import java.nio.charset.Charset;
2829
import java.util.ArrayList;
@@ -340,6 +341,36 @@ private static void testCopiedBufferCharSequence(CharSequence sequence, Charset
340341
}
341342
}
342343

344+
@Test
345+
public void testCopiedBufferCharBufferRegion() {
346+
// CharBuffer.wrap(CharSequence) and read-only buffers are not backed by an accessible array.
347+
testCopiedBufferCharBufferRegion(CharBuffer.wrap("hello world"));
348+
testCopiedBufferCharBufferRegion(CharBuffer.wrap("hello world".toCharArray()).asReadOnlyBuffer());
349+
// Array backed.
350+
testCopiedBufferCharBufferRegion(CharBuffer.wrap("hello world".toCharArray()));
351+
}
352+
353+
private static void testCopiedBufferCharBufferRegion(CharBuffer buffer) {
354+
assertCopiedBufferRegion("llo w", buffer, 2, 5);
355+
assertCopiedBufferRegion("world", buffer, 6, 5);
356+
357+
// The offset is relative to the position of the buffer.
358+
buffer.position(1);
359+
assertCopiedBufferRegion("lo wo", buffer, 2, 5);
360+
assertCopiedBufferRegion("world", buffer, 5, 5);
361+
assertEquals(1, buffer.position());
362+
assertEquals(11, buffer.limit());
363+
}
364+
365+
private static void assertCopiedBufferRegion(String expected, CharBuffer buffer, int offset, int length) {
366+
ByteBuf copied = copiedBuffer(buffer, offset, length, CharsetUtil.US_ASCII);
367+
try {
368+
assertEquals(expected, copied.toString(CharsetUtil.US_ASCII));
369+
} finally {
370+
copied.release();
371+
}
372+
}
373+
343374
@Test
344375
public void testCopiedBuffer() {
345376
ByteBuf copied = copiedBuffer(ByteBuffer.allocateDirect(16));

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.