Merge pull request #62 from P0nk/fix-packet-reuse

Fix issue with packet bytes reuse
This commit is contained in:
Ponk
2021-11-17 22:05:46 +01:00
committed by GitHub
2 changed files with 1 additions and 18 deletions

View File

@@ -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

View File

@@ -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);
}
} }