|
2 | 2 |
|
3 | 3 | import io.netty.buffer.ByteBuf;
|
4 | 4 | import io.netty.buffer.ByteBufAllocator;
|
| 5 | +import io.netty.buffer.ByteBufUtil; |
5 | 6 | import io.netty.buffer.Unpooled;
|
| 7 | +import java.nio.ByteBuffer; |
| 8 | +import java.nio.charset.Charset; |
6 | 9 | import java.util.concurrent.ThreadLocalRandom;
|
| 10 | +import org.junit.Assert; |
7 | 11 | import org.junit.jupiter.api.Test;
|
8 | 12 |
|
9 | 13 | class Tuple3ByteBufTest {
|
@@ -34,4 +38,61 @@ void testTupleBufferGet() {
|
34 | 38 |
|
35 | 39 | int medium = tuple.getMedium(8);
|
36 | 40 | }
|
| 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 | + } |
37 | 98 | }
|
0 commit comments