Skip to content

Commit

Permalink
Add setVarInt method instead of fiddling with writerIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
Janmm14 committed Dec 20, 2024
1 parent 220a69c commit 01d8304
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
24 changes: 24 additions & 0 deletions protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,30 @@ public static void writeVarInt(int value, ByteBuf output)
}
}
}

public static void setVarInt(int value, ByteBuf output, int pos, int len) {
switch ( len )
{
case 1:
output.setByte( pos, value );
break;
case 2:
output.setShort( pos, ( value & 0x7F | 0x80 ) << 8 | ( value >>> 7 & 0x7F ) );
break;
case 3:
output.setMedium( pos, ( value & 0x7F | 0x80 ) << 16 | ( value >>> 7 & 0x7F | 0x80 ) << 8 | ( value >>> 14 & 0x7F ) );
break;
case 4:
output.setInt( pos, ( value & 0x7F | 0x80 ) << 24 | ( value >>> 7 & 0x7F | 0x80 ) << 16 | ( value >>> 14 & 0x7F | 0x80 ) << 8 | ( value >>> 21 & 0x7F ) );
break;
case 5:
output.setInt( pos, ( value & 0x7F | 0x80 ) << 24 | ( value >>> 7 & 0x7F | 0x80 ) << 16 | ( value >>> 14 & 0x7F | 0x80 ) << 8 | ( value >>> 21 & 0x7F | 0x80 ) );
output.setByte( pos + 4, value >>> 28 );
break;
default:
throw new IllegalArgumentException( "Invalid varint len: " + len );
}
}

public static int readVarShort(ByteBuf buf)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
int compressedLen = writerIndex - MAX_SUPPORTED_VARINT_LENGTH_LEN;
byte lengthLen = varintSize( compressedLen );
int lengthStart = MAX_SUPPORTED_VARINT_LENGTH_LEN - lengthLen;
buf.writerIndex( lengthStart );
DefinedPacket.writeVarInt( compressedLen, buf );
DefinedPacket.setVarInt( compressedLen, buf, lengthStart, lengthLen );

buf.readerIndex( lengthStart ); // set start of buffer to ignore potential unused bytes before length
buf.writerIndex( writerIndex ); // set end of buffer back to end of compressed data
out.add( buf );
}
} else
Expand Down Expand Up @@ -164,27 +162,27 @@ public void setTwoBuffers(boolean twoBuffers)
}
}

private static byte varintSize(int paramInt)
private static byte varintSize(int value)
{
if ( ( paramInt & 0xFFFFFF80 ) == 0 )
if ( ( value & 0xFFFFFF80 ) == 0 )
{
return 1;
}
if ( ( paramInt & 0xFFFFC000 ) == 0 )
if ( ( value & 0xFFFFC000 ) == 0 )
{
return 2;
}
if ( ( paramInt & 0xFFE00000 ) == 0 )
if ( ( value & 0xFFE00000 ) == 0 )
{
return 3;
}
if ( ( paramInt & 0xF0000000 ) == 0 )
if ( ( value & 0xF0000000 ) == 0 )
{
return 4;
}
if ( MAX_SUPPORTED_VARINT_LENGTH_LEN < 5 )
{
throw new IllegalArgumentException( "Packet length " + paramInt + " longer than supported (max. 268435455 for 4 byte varint)" );
throw new IllegalArgumentException( "Packet length " + value + " longer than supported (max. 268435455 for 4 byte varint)" );
}
return 5;
}
Expand Down

0 comments on commit 01d8304

Please sign in to comment.