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 — Port: Fix `ByteBuf.setBytes(int, ByteBuffer)` when the source is the … · netty/netty@becd891

File tree

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

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
5858
import java.util.concurrent.atomic.AtomicLong;
5959
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
60-
import java.util.concurrent.atomic.LongAdder;
6160
import java.util.concurrent.locks.StampedLock;
6261

6362
/**
@@ -2478,8 +2477,13 @@ public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
24782477

24792478
@Override
24802479
public ByteBuf setBytes(int index, ByteBuffer src) {
2481-
checkIndex(index, src.remaining());
2482-
ByteBuffer tmp = (ByteBuffer) internalNioBuffer().clear().position(index);
2480+
int length = src.remaining();
2481+
checkIndex(index, length);
2482+
if (src == tmpNioBuf) {
2483+
src = src.duplicate();
2484+
}
2485+
ByteBuffer tmp = internalNioBuffer();
2486+
tmp.position(index);
24832487
tmp.put(src);
24842488
return this;
24852489
}
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
254254
public ByteBuf setBytes(int index, ByteBuffer src) {
255255
int length = src.remaining();
256256
checkIndex(index, length);
257-
ByteBuffer tmpBuf = internalNioBuffer();
258-
if (src == tmpBuf) {
257+
if (src == tmpNioBuf) {
259258
src = src.duplicate();
260259
}
260+
ByteBuffer tmpBuf = internalNioBuffer();
261261

262262
index = idx(index);
263263
tmpBuf.limit(index + length).position(index);
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.management.ManagementFactory;
2323
import java.lang.management.ThreadMXBean;
2424
import java.lang.reflect.Method;
25+
import java.nio.ByteBuffer;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
2728
import static org.assertj.core.api.Assumptions.assumeThat;
@@ -219,6 +220,30 @@ public void execute() throws Throwable {
219220
}
220221
}
221222

223+
@Test
224+
public void testSetBytesFromOwnInternalNioBuffer() {
225+
T allocator = newAllocator(true);
226+
int size = 8;
227+
ByteBuf directBuffer = allocator.directBuffer(size, size);
228+
try {
229+
for (int i = 0; i < size; ++i) {
230+
directBuffer.setByte(i, i);
231+
}
232+
int srcStartIndex = size / 2;
233+
// The source is this buffer's own internal NIO buffer.
234+
ByteBuffer srcByteBuf = directBuffer.internalNioBuffer(srcStartIndex, size - srcStartIndex);
235+
directBuffer.setBytes(0, srcByteBuf);
236+
for (int i = 0; i < srcStartIndex; i++) {
237+
assertEquals((byte) (srcStartIndex + i), directBuffer.getByte(i));
238+
}
239+
for (int i = srcStartIndex; i < size; i++) {
240+
assertEquals((byte) i, directBuffer.getByte(i));
241+
}
242+
} finally {
243+
directBuffer.release();
244+
}
245+
}
246+
222247
protected long expectedUsedMemory(T allocator, int capacity) {
223248
return capacity;
224249
}

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.