Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit f775e2c

Browse files
committed
Get the last ByteBufAdaptor tests passing
1 parent 2dee6f8 commit f775e2c

File tree

2 files changed

+237
-48
lines changed

2 files changed

+237
-48
lines changed

src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.netty.buffer.ByteBufConvertible;
1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.buffer.ByteBufAllocator;
21+
import io.netty.buffer.ByteBufUtil;
2122
import io.netty.buffer.Unpooled;
2223
import io.netty.buffer.api.Buffer;
2324
import io.netty.buffer.api.BufferAllocator;
@@ -464,7 +465,8 @@ public ByteBuf getBytes(int index, byte[] dst) {
464465

465466
@Override
466467
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
467-
if (index < 0 || capacity() < index + length) {
468+
checkAccess();
469+
if (index < 0 || capacity() < index + length || dst.length < dstIndex + length) {
468470
throw new IndexOutOfBoundsException();
469471
}
470472
for (int i = 0; i < length; i++) {
@@ -475,6 +477,10 @@ public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
475477

476478
@Override
477479
public ByteBuf getBytes(int index, ByteBuffer dst) {
480+
checkAccess();
481+
if (index < 0 || capacity() < index + dst.remaining()) {
482+
throw new IndexOutOfBoundsException();
483+
}
478484
while (dst.hasRemaining()) {
479485
dst.put(getByte(index));
480486
index++;
@@ -492,13 +498,15 @@ public ByteBuf getBytes(int index, OutputStream out, int length) throws IOExcept
492498

493499
@Override
494500
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
501+
checkAccess();
495502
ByteBuffer transfer = ByteBuffer.allocate(length);
496503
buffer.copyInto(index, transfer, 0, length);
497504
return out.write(transfer);
498505
}
499506

500507
@Override
501508
public int getBytes(int index, FileChannel out, long position, int length) throws IOException {
509+
checkAccess();
502510
ByteBuffer transfer = ByteBuffer.allocate(length);
503511
buffer.copyInto(index, transfer, 0, length);
504512
return out.write(transfer, position);
@@ -940,8 +948,9 @@ public double readDouble() {
940948

941949
@Override
942950
public ByteBuf readBytes(int length) {
951+
checkAccess();
943952
Buffer copy = preferredBufferAllocator().allocate(length);
944-
buffer.copyInto(0, copy, readerIndex(), length);
953+
buffer.copyInto(readerIndex(), copy, 0, length);
945954
readerIndex(readerIndex() + length);
946955
return wrap(copy).writerIndex(length);
947956
}
@@ -1015,6 +1024,7 @@ public ByteBuf readBytes(OutputStream out, int length) throws IOException {
10151024

10161025
@Override
10171026
public int readBytes(GatheringByteChannel out, int length) throws IOException {
1027+
checkAccess();
10181028
ByteBuffer[] components = new ByteBuffer[buffer.countReadableComponents()];
10191029
buffer.forEachReadable(0, (i, component) -> {
10201030
components[i] = component.readableBuffer();
@@ -1301,31 +1311,30 @@ public int indexOf(int fromIndex, int toIndex, byte value) {
13011311
if (!buffer.isAccessible()) {
13021312
return -1;
13031313
}
1304-
int inc, start, end;
13051314
if (fromIndex <= toIndex) {
1306-
inc = 1;
1307-
start = fromIndex;
1308-
end = toIndex - 1;
1309-
if (start < 0) {
1310-
start = 0; // Required to pass regression tests.
1315+
if (fromIndex < 0) {
1316+
fromIndex = 0; // Required to pass regression tests.
13111317
}
1312-
if (capacity() <= end) {
1318+
if (capacity() < toIndex) {
13131319
throw new IndexOutOfBoundsException();
13141320
}
1321+
for (; fromIndex < toIndex; fromIndex++) {
1322+
if (getByte(fromIndex) == value) {
1323+
return fromIndex;
1324+
}
1325+
}
13151326
} else {
1316-
inc = -1;
1317-
start = fromIndex - 1;
1318-
end = toIndex;
1319-
if (capacity() <= start) {
1320-
start = capacity() - 1; // Required to pass regression tests.
1327+
if (capacity() < fromIndex) {
1328+
fromIndex = capacity(); // Required to pass regression tests.
13211329
}
1322-
if (end < 0) {
1330+
fromIndex--;
1331+
if (toIndex < 0) {
13231332
throw new IndexOutOfBoundsException();
13241333
}
1325-
}
1326-
for (int i = start; i != end; i += inc) {
1327-
if (getByte(i) == value) {
1328-
return i;
1334+
for (; fromIndex > toIndex; fromIndex--) {
1335+
if (getByte(fromIndex) == value) {
1336+
return fromIndex;
1337+
}
13291338
}
13301339
}
13311340
return -1;
@@ -1376,7 +1385,7 @@ public int forEachByteDesc(ByteProcessor processor) {
13761385
@Override
13771386
public int forEachByteDesc(int index, int length, ByteProcessor processor) {
13781387
checkAccess();
1379-
int bytes = buffer.openReverseCursor(index, length).process(processor);
1388+
int bytes = buffer.openReverseCursor(index + length - 1, length).process(processor);
13801389
return bytes == -1 ? -1 : index - bytes;
13811390
}
13821391

@@ -1559,41 +1568,29 @@ public String toString(int index, int length, Charset charset) {
15591568

15601569
@Override
15611570
public int hashCode() {
1562-
int hash = 4242;
1563-
int capacity = capacity();
1564-
for (int i = 0; i < capacity; i++) {
1565-
hash = 31 * hash + getByte(i);
1566-
}
1567-
return hash;
1571+
return ByteBufUtil.hashCode(this);
15681572
}
15691573

15701574
@Override
15711575
public boolean equals(Object obj) {
15721576
if (obj instanceof ByteBufConvertible) {
15731577
ByteBuf other = ((ByteBufConvertible) obj).asByteBuf();
1574-
boolean equal = true;
1575-
int capacity = capacity();
1576-
if (other.capacity() != capacity) {
1577-
return false;
1578-
}
1579-
for (int i = 0; i < capacity; i++) {
1580-
equal &= getByte(i) == other.getByte(i);
1581-
}
1582-
return equal;
1578+
return this == other || ByteBufUtil.equals(this, other);
15831579
}
15841580
return false;
15851581
}
15861582

15871583
@Override
15881584
public int compareTo(ByteBuf buffer) {
1589-
var cap = Math.min(capacity(), buffer.capacity());
1590-
for (int i = 0; i < cap; i++) {
1591-
int cmp = Byte.compare(getByte(i), buffer.getByte(i));
1592-
if (cmp != 0) {
1593-
return cmp;
1594-
}
1585+
ByteOrder orderThis = order();
1586+
ByteOrder orderThat = buffer.order();
1587+
try {
1588+
// Little-ending implementation of the compare seems to be broken.
1589+
return ByteBufUtil.compare(order(ByteOrder.BIG_ENDIAN), buffer.order(ByteOrder.BIG_ENDIAN));
1590+
} finally {
1591+
order(orderThis);
1592+
buffer.order(orderThat);
15951593
}
1596-
return Integer.compare(capacity(), buffer.capacity());
15971594
}
15981595

15991596
@Override

0 commit comments

Comments
 (0)