Dump packet with ByteBufInPacket toString
This commit is contained in:
@@ -78,4 +78,23 @@ public class ByteBufInPacket implements InPacket {
|
|||||||
public int getPosition() {
|
public int getPosition() {
|
||||||
return byteBuf.readerIndex();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.packet;
|
|||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
|
import net.opcodes.SendOpcode;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -189,4 +190,18 @@ class ByteBufInPacketTest {
|
|||||||
|
|
||||||
assertArrayEquals(bytes, sameBytes);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user