10000 fixes array copy in Tuple3ByteBuf (#669) · FTTF-git/rsocket-java@9fa00a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fa00a6

Browse files
authored
fixes array copy in Tuple3ByteBuf (rsocket#669)
* fixes array copying offsets for tuple3 bytebuf Signed-off-by: Robert Roeser <rroeserr@gmail.com> * fixes array copying offsets for tuple3 bytebuf Signed-off-by: Robert Roeser <rroeserr@gmail.com> * update array indexes in copy Signed-off-by: Robert Roeser <rroeserr@gmail.com>
1 parent 44f911b commit 9fa00a6

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

rsocket-core/src/main/java/io/rsocket/buffer/Tuple3ByteBuf.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ public ByteBuffer[] _nioBuffers(int index, int length) {
144144
new ByteBuffer[oneBuffer.length + twoBuffer.length + threeBuffer.length];
145145
System.arraycopy(oneBuffer, 0, results, 0, oneBuffer.length);
146146
System.arraycopy(twoBuffer, 0, results, oneBuffer.length, twoBuffer.length);
147-
System.arraycopy(threeBuffer, 0, results, twoBuffer.length, threeBuffer.length);
147+
System.arraycopy(
148+
threeBuffer, 0, results, oneBuffer.length + twoBuffer.length, threeBuffer.length);
148149
return results;
149150
} else {
150151
ByteBuffer[] results = new ByteBuffer[oneBuffer.length + twoBuffer.length];
@@ -167,7 +168,7 @@ public ByteBuffer[] _nioBuffers(int index, int length) {
167168
threeBuffer = three.nioBuffers(threeReadIndex, length);
168169
ByteBuffer[] results = new ByteBuffer[twoBuffer.length + threeBuffer.length];
169170
System.arraycopy(twoBuffer, 0, results, 0, twoBuffer.length);
170-
System.arraycopy(threeBuffer, 0, results, threeBuffer.length, twoBuffer.length);
171+
System.arraycopy(threeBuffer, 0, results, twoBuffer.length, threeBuffer.length);
171172
return results;
172173
} else {
173174
return twoBuffer;

rsocket-core/src/test/java/io/rsocket/buffer/Tuple3ByteBufTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import io.netty.buffer.ByteBuf;
44
import io.netty.buffer.ByteBufAllocator;
5+
import io.netty.buffer.ByteBufUtil;
56
import io.netty.buffer.Unpooled;
7+
import java.nio.ByteBuffer;
8+
import java.nio.charset.Charset;
69
import java.util.concurrent.ThreadLocalRandom;
10+
import org.junit.Assert;
711
import org.junit.jupiter.api.Test;
812

913
class Tuple3ByteBufTest {
@@ -34,4 +38,61 @@ void testTupleBufferGet() {
3438

3539
int medium = tuple.getMedium(8);
3640
}
41+
42+
@Test
43+
void testTuple3BufferSlicing() {
44+
ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
45+
ByteBuf one = allocator.directBuffer();
46+
ByteBufUtil.writeUtf8(one, "foo");
47+
48+
ByteBuf two = allocator.directBuffer();
49+
ByteBufUtil.writeUtf8(two, "bar");
50+
51+
ByteBuf three = allocator.directBuffer();
52+
ByteBufUtil.writeUtf8(three, "bar");
53+
54+
ByteBuf buf = TupleByteBuf.of(one, two, three);
55+
56+
String s = buf.slice(0, 6).toString(Charset.defaultCharset());
57+
Assert.assertEquals("foobar", s);
58+
59+
String s1 = buf.slice(3, 6).toString(Charset.defaultCharset());
60+
Assert.assertEquals("barbar", s1);
61+
62+
String s2 = buf.slice(4, 4).toString(Charset.defaultCharset());
63+
Assert.assertEquals("arba", s2);
64+
}
65+
66+
@Test
67+
void testTuple3ToNioBuffers() throws Exception {
68+
ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
69+
ByteBuf one = allocator.directBuffer();
70+
ByteBufUtil.writeUtf8(one, "one");
71+
72+
ByteBuf two = allocator.directBuffer();
73+
ByteBufUtil.writeUtf8(two, "two");
74+
75+
ByteBuf three = allocator.directBuffer();
76+
ByteBufUtil.writeUtf8(three, "three");
77+
78+
ByteBuf buf = TupleByteBuf.of(one, two, three);
79+
ByteBuffer[] byteBuffers = buf.nioBuffers();
80+
81+
Assert.assertEquals(3, byteBuffers.length);
82+
83+
ByteBuffer bb = byteBuffers[0];
84+
byte[] dst = new byte[bb.remaining()];
85+
bb.get(dst);
86+
Assert.assertEquals("one", new String(dst, "UTF-8"));
87+
88+
bb = byteBuffers[1];
89+
dst = new byte[bb.remaining()];
90+
bb.get(dst);
91+
Assert.assertEquals("two", new String(dst, "UTF-8"));
92+
93+
bb = byteBuffers[2];
94+
dst = new byte[bb.remaining()];
95+
bb.get(dst);
96+
Assert.assertEquals("three", new String(dst, "UTF-8"));
97+
}
3798
}

0 commit comments

Comments
 (0)
0