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

Commit 7c64467

Browse files
committed
feat: optimize binary tag codecs with buffered and compressed streams
- Replaced `ByteBufInputStream` and `ByteBufOutputStream` with `FastBufferedInputStream` and `FastBufferedOutputStream` for improved performance. - Added GZIP compression support for `BINARY_TAG_CODEC_COMPRESSED`.
1 parent 76ff78e commit 7c64467

File tree

1 file changed

+11
-4
lines changed
  • surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/network/codec

1 file changed

+11
-4
lines changed

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/network/codec/ByteBufCodecs.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import io.netty.buffer.ByteBufInputStream
99
import io.netty.buffer.ByteBufOutputStream
1010
import io.netty.handler.codec.DecoderException
1111
import io.netty.handler.codec.EncoderException
12+
import it.unimi.dsi.fastutil.io.FastBufferedInputStream
13+
import it.unimi.dsi.fastutil.io.FastBufferedOutputStream
1214
import it.unimi.dsi.fastutil.objects.ObjectArrayList
1315
import net.kyori.adventure.key.Key
1416
import net.kyori.adventure.nbt.BinaryTag
@@ -19,6 +21,8 @@ import net.kyori.adventure.sound.Sound
1921
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
2022
import java.io.ByteArrayInputStream
2123
import java.io.ByteArrayOutputStream
24+
import java.io.DataInputStream
25+
import java.io.DataOutputStream
2226
import java.math.BigDecimal
2327
import java.math.BigInteger
2428
import java.math.MathContext
@@ -29,6 +33,8 @@ import java.time.Instant
2933
import java.time.ZoneId
3034
import java.time.ZonedDateTime
3135
import java.util.*
36+
import java.util.zip.GZIPInputStream
37+
import java.util.zip.GZIPOutputStream
3238
import kotlin.math.min
3339
import kotlin.time.Duration
3440
import kotlin.time.Duration.Companion.milliseconds
@@ -172,27 +178,28 @@ object ByteBufCodecs {
172178
val BINARY_TAG_CODEC: StreamCodec<ByteBuf, BinaryTag> = streamCodec({ buf, tag ->
173179
val type = tag.type() as BinaryTagType<BinaryTag>
174180
buf.writeByte(type.id().toInt())
175-
ByteBufOutputStream(buf).use {
181+
DataOutputStream(FastBufferedOutputStream(ByteBufOutputStream(buf))).use {
176182
type.write(tag, it)
177183
}
178184
}, { bytes ->
179185
val typeId = bytes.readByte().toInt()
180186
val type = getTagType(typeId)
181-
ByteBufInputStream(bytes).use {
187+
DataInputStream(FastBufferedInputStream(ByteBufInputStream(bytes))).use {
182188
type.read(it)
183189
}
184190
})
185191

186192
val BINARY_TAG_CODEC_COMPRESSED: StreamCodec<ByteBuf, BinaryTag> = streamCodec({ buf, tag ->
187193
val type = tag.type() as BinaryTagType<BinaryTag>
188194
buf.writeByte(type.id().toInt())
189-
ByteBufOutputStream(buf).use {
195+
DataOutputStream(FastBufferedOutputStream(GZIPOutputStream(ByteBufOutputStream(buf)))).use {
190196
type.write(tag, it)
191197
}
192198
}, { bytes ->
193199
val typeId = bytes.readByte().toInt()
194200
val type = getTagType(typeId)
195-
ByteBufInputStream(bytes).use {
201+
202+
DataInputStream(FastBufferedInputStream(GZIPInputStream(ByteBufInputStream(bytes)))).use {
196203
type.read(it)
197204
}
198205
})

0 commit comments

Comments
 (0)