Merge pull request #62 from P0nk/fix-packet-reuse
Fix issue with packet bytes reuse
This commit is contained in:
@@ -12,7 +12,6 @@ import java.awt.*;
|
|||||||
@NotThreadSafe
|
@NotThreadSafe
|
||||||
public class ByteBufOutPacket implements OutPacket {
|
public class ByteBufOutPacket implements OutPacket {
|
||||||
private final ByteBuf byteBuf;
|
private final ByteBuf byteBuf;
|
||||||
private byte[] bytes;
|
|
||||||
|
|
||||||
public ByteBufOutPacket() {
|
public ByteBufOutPacket() {
|
||||||
this.byteBuf = Unpooled.buffer();
|
this.byteBuf = Unpooled.buffer();
|
||||||
@@ -32,12 +31,7 @@ public class ByteBufOutPacket implements OutPacket {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] getBytes() {
|
public byte[] getBytes() {
|
||||||
if (bytes == null) {
|
return ByteBufUtil.getBytes(byteBuf);
|
||||||
// Avoid creating new byte arrays when the packet is broadcast
|
|
||||||
bytes = ByteBufUtil.getBytes(byteBuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -203,15 +203,4 @@ class ByteBufOutPacketTest {
|
|||||||
assertEquals(0, wrapped.readByte());
|
assertEquals(0, wrapped.readByte());
|
||||||
assertEquals(secondWrittenByte, wrapped.readByte());
|
assertEquals(secondWrittenByte, wrapped.readByte());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void whenGettingBytesRepeatedly_bytesShouldBeLockedInPlace() {
|
|
||||||
outPacket.writeByte(1);
|
|
||||||
byte[] initialWrite = outPacket.getBytes();
|
|
||||||
|
|
||||||
outPacket.writeByte(2);
|
|
||||||
byte[] afterWritingAgain = outPacket.getBytes();
|
|
||||||
|
|
||||||
assertArrayEquals(initialWrite, afterWritingAgain);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user