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 — ReadOnlyByteBufferBuf | ReadOnlyUnsafeDirectByteBuf get*, copy*, dupl… · netty/netty@600697d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • ReadOnlyByteBufferBuf.java
      • ReadOnlyUnsafeDirectByteBuf.java
    • test/java/io/netty/buffer
      • AbstractByteBufTest.java
      • ReadOnlyDirectByteBufferBufTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java
+85-7Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,25 @@ protected long _getLongLE(int index) {
176176

177177
@Override
178178
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
179+
return getBytes(index, dst, dstIndex, length, false);
180+
}
181+
182+
@Override
183+
public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {
184+
checkReadableBytes(length);
185+
getBytes(readerIndex, dst, dstIndex, length, true);
186+
readerIndex += length;
187+
return this;
188+
}
189+
190+
protected ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length, boolean internal) {
179191
checkDstIndex(index, length, dstIndex, dst.capacity());
180192
if (dst.hasArray()) {
181193
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
182194
} else if (dst.nioBufferCount() > 0) {
183195
for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) {
184196
int bbLen = bb.remaining();
185-
getBytes(index, bb);
197+
getBytes(index, bb, internal);
186198
index += bbLen;
187199
}
188200
} else {
@@ -193,19 +205,44 @@ public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
193205

194206
@Override
195207
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
208+
return getBytes(index, dst, dstIndex, length, false);
209+
}
210+
211+
@Override
212+
public ByteBuf readBytes(byte[] dst, int dstIndex, int length) {
213+
checkReadableBytes(length);
214+
getBytes(readerIndex, dst, dstIndex, length, true);
215+
readerIndex += length;
216+
return this;
217+
}
218+
219+
protected ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal) {
196220
checkDstIndex(index, length, dstIndex, dst.length);
197221

198-
ByteBuffer tmpBuf = internalNioBuffer();
222+
final ByteBuffer tmpBuf = nioBuffer(internal);
199223
tmpBuf.clear().position(index).limit(index + length);
200224
tmpBuf.get(dst, dstIndex, length);
201225
return this;
202226
}
203227

204228
@Override
205229
public ByteBuf getBytes(int index, ByteBuffer dst) {
230+
return getBytes(index, dst, false);
231+
}
232+
233+
@Override
234+
public ByteBuf readBytes(ByteBuffer dst) {
235+
int length = dst.remaining();
236+
checkReadableBytes(length);
237+
getBytes(readerIndex, dst, true);
238+
readerIndex += length;
239+
return this;
240+
}
241+
242+
private ByteBuf getBytes(int index, ByteBuffer dst, boolean internal) {
206243
checkIndex(index, dst.remaining());
207244

208-
ByteBuffer tmpBuf = internalNioBuffer();
245+
final ByteBuffer tmpBuf = nioBuffer(internal);
209246
tmpBuf.clear().position(index).limit(index + dst.remaining());
210247
dst.put(tmpBuf);
211248
return this;
@@ -338,6 +375,18 @@ public boolean isDirect() {
338375

339376
@Override
340377
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
378+
return getBytes(index, out, length, false);
379+
}
380+
381+
@Override
382+
public ByteBuf readBytes(OutputStream out, int length) throws IOException {
383+
checkReadableBytes(length);
384+
getBytes(readerIndex, out, length, true);
385+
readerIndex += length;
386+
return this;
387+
}
388+
389+
private ByteBuf getBytes(int index, OutputStream out, int length, boolean internal) throws IOException {
341390
ensureAccessible();
342391
if (length == 0) {
343392
return this;
@@ -347,7 +396,7 @@ public ByteBuf getBytes(int index, OutputStream out, int length) throws IOExcept
347396
out.write(buffer.array(), index + buffer.arrayOffset(), length);
348397
} else {
349398
byte[] tmp = ByteBufUtil.threadLocalTempArray(length);
350-
ByteBuffer tmpBuf = internalNioBuffer();
399+
ByteBuffer tmpBuf = nioBuffer(internal);
351400
tmpBuf.clear().position(index);
352401
tmpBuf.get(tmp, 0, length);
353402
out.write(tmp, 0, length);
@@ -357,24 +406,48 @@ public ByteBuf getBytes(int index, OutputStream out, int length) throws IOExcept
357406

358407
@Override
359408
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
409+
return getBytes(index, out, length, false);
410+
}
411+
412+
@Override
413+
public int readBytes(GatheringByteChannel out, int length) throws IOException {
414+
checkReadableBytes(length);
415+
int readBytes = getBytes(readerIndex, out, length, true);
416+
readerIndex += readBytes;
417+
return readBytes;
418+
}
419+
420+
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
360421
ensureAccessible();
361422
if (length == 0) {
362423
return 0;
363424
}
364425

365-
ByteBuffer tmpBuf = internalNioBuffer();
426+
ByteBuffer tmpBuf = nioBuffer(internal);
366427
tmpBuf.clear().position(index).limit(index + length);
367428
return out.write(tmpBuf);
368429
}
369430

370431
@Override
371432
public int getBytes(int index, FileChannel out, long position, int length) throws IOException {
433+
return getBytes(index, out, position, length, false);
434+
}
435+
436+
@Override
437+
public int readBytes(FileChannel out, long position, int length) throws IOException {
438+
checkReadableBytes(length);
439+
int readBytes = getBytes(readerIndex, out, position, length, true);
440+
readerIndex += readBytes;
441+
return readBytes;
442+
}
443+
444+
private int getBytes(int index, FileChannel out, long position, int length, boolean internal) throws IOException {
372445
ensureAccessible();
373446
if (length == 0) {
374447
return 0;
375448
}
376449

377-
ByteBuffer tmpBuf = internalNioBuffer();
450+
ByteBuffer tmpBuf = nioBuffer(internal);
378451
tmpBuf.clear().position(index).limit(index + length);
379452
return out.write(tmpBuf, position);
380453
}
@@ -422,7 +495,8 @@ public ByteBuf copy(int index, int length) {
422495
ensureAccessible();
423496
ByteBuffer src;
424497
try {
425-
src = (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
498+
// Always duplicate the buffer so it's safe to call copy from multiple threads.
499+
src = (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + length);
426500
} catch (IllegalArgumentException ignored) {
427501
throw new IndexOutOfBoundsException("Too many bytes to read - Need " + (index + length));
428502
}
@@ -483,4 +557,8 @@ public boolean hasMemoryAddress() {
483557
public long memoryAddress() {
484558
throw new UnsupportedOperationException();
485559
}
560+
561+
private ByteBuffer nioBuffer(boolean internal) {
562+
return internal ? internalNioBuffer() : buffer.duplicate();
563+
}
486564
}
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected long _getLong(int index) {
6161
}
6262

6363
@Override
64-
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
64+
protected ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length, boolean internal) {
6565
checkIndex(index, length);
6666
ObjectUtil.checkNotNull(dst, "dst");
6767
if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
@@ -79,7 +79,7 @@ public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
7979
}
8080

8181
@Override
82-
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
82+
protected ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal) {
8383
checkIndex(index, length);
8484
ObjectUtil.checkNotNull(dst, "dst");
8585
if (dstIndex < 0 || dstIndex > dst.length - length) {

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.