Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 296a7df

Browse files
committed
refactor: improve VarLong and Utf8String handling for better readability and consistency
- Adjusted `VarLong` logic to use clear variable naming (`numBytes` instead of `shift`). - Replaced bit-shifting expressions with more readable parenthesized forms. - Refactored `Utf8String` to utilize `VarInt` methods for encoding and decoding operations.
1 parent 8686590 commit 296a7df

File tree

2 files changed

+8
-10
lines changed
  • surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/protocol/buffer/types

2 files changed

+8
-10
lines changed

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/protocol/buffer/types/Utf8String.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package dev.slne.surf.cloud.api.common.netty.protocol.buffer.types
33
import dev.slne.surf.cloud.api.common.netty.network.codec.streamCodec
44
import dev.slne.surf.cloud.api.common.netty.protocol.buffer.checkDecoded
55
import dev.slne.surf.cloud.api.common.netty.protocol.buffer.checkEncoded
6-
import dev.slne.surf.cloud.api.common.netty.protocol.buffer.readVarInt
7-
import dev.slne.surf.cloud.api.common.netty.protocol.buffer.writeVarInt
86
import io.netty.buffer.ByteBuf
97
import io.netty.buffer.ByteBufUtil
108
import java.nio.charset.StandardCharsets
@@ -38,7 +36,7 @@ object Utf8String {
3836
*/
3937
fun read(buf: ByteBuf, maxLength: Int): String {
4038
val utf8MaxLength = ByteBufUtil.utf8MaxBytes(maxLength)
41-
val utf8Length = buf.readVarInt()
39+
val utf8Length = VarInt.readVarInt(buf)
4240

4341
checkDecoded(utf8Length <= utf8MaxLength) { "String too long (max $maxLength): $utf8Length" }
4442
checkDecoded(utf8Length >= 0) { "String length is negative: $utf8Length" }
@@ -78,7 +76,7 @@ object Utf8String {
7876

7977
checkEncoded(bytesWritten <= utf8MaxLength) { "String too long (max $maxLength): $bytesWritten" }
8078

81-
buf.writeVarInt(bytesWritten)
79+
VarInt.writeVarInt(buf, bytesWritten)
8280
buf.writeBytes(utf8Buffer)
8381
} finally {
8482
utf8Buffer.release()

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/protocol/buffer/types/VarLong.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ object VarLong {
2323
*/
2424
fun getEncodedSize(value: Long): Int {
2525
for (i in 1 until MAX_VARLONG_SIZE) {
26-
if ((value and -1L shl i * DATA_BITS_PER_BYTE) == 0L) {
26+
if (value and (-1L shl (i * DATA_BITS_PER_BYTE)) == 0L) {
2727
return i
2828
}
2929
}
@@ -49,16 +49,16 @@ object VarLong {
4949
*/
5050
fun readVarLong(buf: ByteBuf): Long {
5151
var result = 0L
52-
var shift = 0
52+
var numBytes = 0
5353

5454
var currentByte: Byte
5555
do {
5656
currentByte = buf.readByte()
57-
result =
58-
result or ((currentByte.toLong() and DATA_BITS_MASK.toLong()) shl (shift++ * DATA_BITS_PER_BYTE))
59-
shift += DATA_BITS_PER_BYTE
57+
result = result or
58+
((currentByte.toLong() and DATA_BITS_MASK.toLong()) shl (numBytes * DATA_BITS_PER_BYTE))
59+
numBytes++
6060

61-
checkDecoded(shift <= MAX_VARLONG_SIZE) { "VarLong too big" }
61+
checkDecoded(numBytes <= MAX_VARLONG_SIZE) { "VarLong too big" }
6262
} while (currentByte.toInt() and CONTINUATION_BIT_MASK == CONTINUATION_BIT_MASK)
6363

6464
return result

0 commit comments

Comments
 (0)