Dump packet with ByteBufInPacket toString

This commit is contained in:
P0nk
2021-08-23 20:59:51 +02:00
parent da2d8abc56
commit 4fd7adf547
2 changed files with 34 additions and 0 deletions

View File

@@ -78,4 +78,23 @@ public class ByteBufInPacket implements InPacket {
public int getPosition() {
return byteBuf.readerIndex();
}
@Override
public String toString() {
final int readerIndex = byteBuf.readerIndex();
byteBuf.markReaderIndex();
byteBuf.readerIndex(0);
String hexDumpWithPosition = insertReaderPosition(ByteBufUtil.hexDump(byteBuf).toUpperCase(), readerIndex);
String toString = String.format("ByteBufInPacket[%s]", hexDumpWithPosition);
byteBuf.resetReaderIndex();
return toString;
}
private static String insertReaderPosition(String hexDump, int index) {
StringBuilder sb = new StringBuilder(hexDump);
sb.insert(2 * index, '_');
return sb.toString();
}
}

View File

@@ -2,6 +2,7 @@ package net.packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.opcodes.SendOpcode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -189,4 +190,18 @@ class ByteBufInPacketTest {
assertArrayEquals(bytes, sameBytes);
}
@Test
void toString_shouldIncludeEntirePacket() {
OutPacket outPacket = OutPacket.create(SendOpcode.COCONUT_HIT);
outPacket.writeByte(111);
ByteBuf byteBuf = Unpooled.wrappedBuffer(outPacket.getBytes());
ByteBufInPacket inPacket = new ByteBufInPacket(byteBuf);
String initial = inPacket.toString();
inPacket.readShort();
String afterReadingOpcode = inPacket.toString();
assertEquals(initial.length(), afterReadingOpcode.length());
}
}