Simplify getBytes and add tests for it
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package net.packet;
|
package net.packet;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.ByteBufUtil;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
@@ -13,10 +14,7 @@ public class ByteBufInPacket implements InPacket {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] getBytes() {
|
public byte[] getBytes() {
|
||||||
byte[] bytes = new byte[byteBuf.readableBytes()];
|
return ByteBufUtil.getBytes(byteBuf);
|
||||||
int readerIndex = byteBuf.readerIndex();
|
|
||||||
byteBuf.getBytes(readerIndex, bytes);
|
|
||||||
return bytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.packet;
|
package net.packet;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.ByteBufUtil;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import net.jcip.annotations.NotThreadSafe;
|
import net.jcip.annotations.NotThreadSafe;
|
||||||
import net.opcodes.SendOpcode;
|
import net.opcodes.SendOpcode;
|
||||||
@@ -25,10 +26,7 @@ public class ByteBufOutPacket implements OutPacket {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] getBytes() {
|
public byte[] getBytes() {
|
||||||
byte[] bytes = new byte[byteBuf.readableBytes()];
|
return ByteBufUtil.getBytes(byteBuf);
|
||||||
int readerIndex = byteBuf.readerIndex();
|
|
||||||
byteBuf.getBytes(readerIndex, bytes);
|
|
||||||
return bytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
class ByteBufInPacketTest {
|
class ByteBufInPacketTest {
|
||||||
@@ -167,4 +168,26 @@ class ByteBufInPacketTest {
|
|||||||
inPacket.seek(5);
|
inPacket.seek(5);
|
||||||
assertEquals(5, inPacket.getPosition());
|
assertEquals(5, inPacket.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getBytes() {
|
||||||
|
givenWrittenBytes(20, 19, 21, 18, 22);
|
||||||
|
|
||||||
|
byte[] bytes = inPacket.getBytes();
|
||||||
|
|
||||||
|
assertArrayEquals(new byte[]{20, 19, 21, 18, 22}, bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGetBytes_shouldBeRepeatable() {
|
||||||
|
givenWrittenBytes(1, 2, 3, 4, 5);
|
||||||
|
|
||||||
|
byte[] bytes = inPacket.getBytes();
|
||||||
|
assertEquals(5, bytes.length);
|
||||||
|
|
||||||
|
byte[] sameBytes = inPacket.getBytes();
|
||||||
|
assertEquals(5, sameBytes.length);
|
||||||
|
|
||||||
|
assertArrayEquals(bytes, sameBytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user