From 1a2b162353d7ceb6ddb38323e2819e4d4d17b2be Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 11 Jan 2019 18:32:03 -0500 Subject: [PATCH] Cleaner and more correct to use ByteBufs directly. --- .../netty/MinecraftVarintFrameDecoder.java | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java index dfe0e25b4..01dda3641 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java @@ -11,41 +11,42 @@ import java.util.List; public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { - private final byte[] lenBuf = new byte[3]; - @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { if (!in.isReadable()) { return; } - Arrays.fill(lenBuf, (byte) 0); + ByteBuf lenBuf = ctx.alloc().buffer(3); int origReaderIndex = in.readerIndex(); - - ByteBuf wrappedBuf = Unpooled.wrappedBuffer(lenBuf); - for (int i = 0; i < lenBuf.length; i++) { - if (!in.isReadable()) { - in.readerIndex(origReaderIndex); - return; - } - - lenBuf[i] = in.readByte(); - if (lenBuf[i] > 0) { - int packetLength = ProtocolUtils.readVarInt(wrappedBuf); - if (packetLength == 0) { - return; - } - - if (in.readableBytes() < packetLength) { + try { + for (int i = 0; i < 3; i++) { + if (!in.isReadable()) { in.readerIndex(origReaderIndex); return; } - out.add(in.readRetainedSlice(packetLength)); - return; - } - } + byte read = in.readByte(); + lenBuf.writeByte(read); + if (read > 0) { + int packetLength = ProtocolUtils.readVarInt(lenBuf.slice()); + if (packetLength == 0) { + return; + } - throw new CorruptedFrameException("VarInt too big"); + if (in.readableBytes() < packetLength) { + in.readerIndex(origReaderIndex); + return; + } + + out.add(in.readRetainedSlice(packetLength)); + return; + } + } + + throw new CorruptedFrameException("VarInt too big"); + } finally { + lenBuf.release(); + } } }