Skip to content

Commit 895c777

Browse files
committed
Refactor Buf classes to use BufConfiguration for enum handling and add KBufSerializer for custom ByteBuf support
1 parent f0c89b7 commit 895c777

File tree

13 files changed

+510
-46
lines changed

13 files changed

+510
-46
lines changed

build.gradle.kts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ dependencies {
1414
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.8.0")
1515
implementation("io.netty:netty-all:4.1.119.Final")
1616

17-
1817
testImplementation(kotlin("test"))
19-
2018
}
2119

22-
tasks.test {
23-
useJUnitPlatform()
20+
tasks {
21+
test {
22+
useJUnitPlatform()
23+
}
2424
}
25+
26+
2527
kotlin {
2628
jvmToolchain(21)
2729
}

gradle/wrapper/gradle-wrapper.jar

-16.8 KB
Binary file not shown.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#Sun Mar 02 14:03:09 CET 2025
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
43
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
56
zipStoreBase=GRADLE_USER_HOME
67
zipStorePath=wrapper/dists

gradlew

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
1820

1921
##############################################################################
2022
#
@@ -55,7 +57,7 @@
5557
# Darwin, MinGW, and NonStop.
5658
#
5759
# (3) This script is generated from the Groovy template
58-
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
60+
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
5961
# within the Gradle project.
6062
#
6163
# You can find Gradle at https://github.com/gradle/gradle/.
@@ -80,13 +82,12 @@ do
8082
esac
8183
done
8284

83-
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84-
85-
APP_NAME="Gradle"
85+
# This is normally unused
86+
# shellcheck disable=SC2034
8687
APP_BASE_NAME=${0##*/}
87-
88-
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
89-
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
88+
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
89+
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
90+
' "$PWD" ) || exit
9091

9192
# Use the maximum available, or set MAX_FD != -1 to use that value.
9293
MAX_FD=maximum
@@ -133,22 +134,29 @@ location of your Java installation."
133134
fi
134135
else
135136
JAVACMD=java
136-
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137+
if ! command -v java >/dev/null 2>&1
138+
then
139+
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137140
138141
Please set the JAVA_HOME variable in your environment to match the
139142
location of your Java installation."
143+
fi
140144
fi
141145

142146
# Increase the maximum file descriptors if we can.
143147
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144148
case $MAX_FD in #(
145149
max*)
150+
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
151+
# shellcheck disable=SC2039,SC3045
146152
MAX_FD=$( ulimit -H -n ) ||
147153
warn "Could not query maximum file descriptor limit"
148154
esac
149155
case $MAX_FD in #(
150156
'' | soft) :;; #(
151157
*)
158+
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
159+
# shellcheck disable=SC2039,SC3045
152160
ulimit -n "$MAX_FD" ||
153161
warn "Could not set maximum file descriptor limit to $MAX_FD"
154162
esac
@@ -193,18 +201,28 @@ if "$cygwin" || "$msys" ; then
193201
done
194202
fi
195203

196-
# Collect all arguments for the java command;
197-
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198-
# shell script including quotes and variable substitutions, so put them in
199-
# double quotes to make sure that they get re-expanded; and
200-
# * put everything else in single quotes, so that it's not re-expanded.
204+
205+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
206+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
207+
208+
# Collect all arguments for the java command:
209+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
210+
# and any embedded shellness will be escaped.
211+
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
212+
# treated as '${Hostname}' itself on the command line.
201213

202214
set -- \
203215
"-Dorg.gradle.appname=$APP_BASE_NAME" \
204216
-classpath "$CLASSPATH" \
205217
org.gradle.wrapper.GradleWrapperMain \
206218
"$@"
207219

220+
# Stop when "xargs" is not available.
221+
if ! command -v xargs >/dev/null 2>&1
222+
then
223+
die "xargs is not available"
224+
fi
225+
208226
# Use "xargs" to parse quoted args.
209227
#
210228
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.

gradlew.bat

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
@rem See the License for the specific language governing permissions and
1414
@rem limitations under the License.
1515
@rem
16+
@rem SPDX-License-Identifier: Apache-2.0
17+
@rem
1618

17-
@if "%DEBUG%" == "" @echo off
19+
@if "%DEBUG%"=="" @echo off
1820
@rem ##########################################################################
1921
@rem
2022
@rem Gradle startup script for Windows
@@ -25,7 +27,8 @@
2527
if "%OS%"=="Windows_NT" setlocal
2628

2729
set DIRNAME=%~dp0
28-
if "%DIRNAME%" == "" set DIRNAME=.
30+
if "%DIRNAME%"=="" set DIRNAME=.
31+
@rem This is normally unused
2932
set APP_BASE_NAME=%~n0
3033
set APP_HOME=%DIRNAME%
3134

@@ -40,13 +43,13 @@ if defined JAVA_HOME goto findJavaFromJavaHome
4043

4144
set JAVA_EXE=java.exe
4245
%JAVA_EXE% -version >NUL 2>&1
43-
if "%ERRORLEVEL%" == "0" goto execute
46+
if %ERRORLEVEL% equ 0 goto execute
4447

45-
echo.
46-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47-
echo.
48-
echo Please set the JAVA_HOME variable in your environment to match the
49-
echo location of your Java installation.
48+
echo. 1>&2
49+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
50+
echo. 1>&2
51+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
52+
echo location of your Java installation. 1>&2
5053

5154
goto fail
5255

@@ -56,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
5659

5760
if exist "%JAVA_EXE%" goto execute
5861

59-
echo.
60-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61-
echo.
62-
echo Please set the JAVA_HOME variable in your environment to match the
63-
echo location of your Java installation.
62+
echo. 1>&2
63+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
64+
echo. 1>&2
65+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
66+
echo location of your Java installation. 1>&2
6467

6568
goto fail
6669

@@ -75,13 +78,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
7578

7679
:end
7780
@rem End local scope for the variables with windows NT shell
78-
if "%ERRORLEVEL%"=="0" goto mainEnd
81+
if %ERRORLEVEL% equ 0 goto mainEnd
7982

8083
:fail
8184
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
8285
rem the _cmd.exe /c_ return code!
83-
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84-
exit /b 1
86+
set EXIT_CODE=%ERRORLEVEL%
87+
if %EXIT_CODE% equ 0 set EXIT_CODE=1
88+
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
89+
exit /b %EXIT_CODE%
8590

8691
:mainEnd
8792
if "%OS%"=="Windows_NT" endlocal

src/main/kotlin/dev/slne/surf/bytebufserializer/Buf.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlinx.serialization.modules.SerializersModule
1010

1111
sealed class Buf(
1212
override val serializersModule: SerializersModule = EmptySerializersModule(),
13-
public val configuration: BufConfiguration = BufConfiguration()
13+
private val configuration: BufConfiguration = BufConfiguration()
1414
) : BinaryFormat {
1515

1616
companion object Default : Buf(EmptySerializersModule(), BufConfiguration()) {
@@ -23,12 +23,12 @@ sealed class Buf(
2323
}
2424

2525
@OptIn(ExperimentalSerializationApi::class)
26-
fun <T> encodeToBuf(
27-
buf: ByteBuf,
26+
fun <T, B: ByteBuf> encodeToBuf(
27+
buf: B,
2828
serializer: SerializationStrategy<T>,
2929
value: T
3030
) {
31-
val encoder = BufEncoder(buf, serializersModule, configuration.enumsWithOrdinal)
31+
val encoder = BufEncoder(buf, serializersModule, configuration)
3232
encoder.encodeSerializableValue(serializer, value)
3333
}
3434

@@ -37,7 +37,7 @@ sealed class Buf(
3737
buf: ByteBuf,
3838
deserializer: DeserializationStrategy<T>
3939
): T {
40-
val decoder = BufDecoder(buf, serializersModule, configuration.enumsWithOrdinal)
40+
val decoder = BufDecoder(buf, serializersModule, configuration)
4141
return decoder.decodeSerializableValue(deserializer)
4242
}
4343

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package dev.slne.surf.bytebufserializer
22

3-
class BufConfiguration(internal var enumsWithOrdinal: Boolean = true, )
3+
data class BufConfiguration(internal var enumsWithOrdinal: Boolean = true)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@file:Suppress("UNCHECKED_CAST")
2+
3+
package dev.slne.surf.bytebufserializer
4+
5+
import dev.slne.surf.bytebufserializer.internal.BufDecoder
6+
import dev.slne.surf.bytebufserializer.internal.BufEncoder
7+
import io.netty.buffer.ByteBuf
8+
import kotlinx.serialization.ExperimentalSerializationApi
9+
import kotlinx.serialization.KSerializer
10+
import kotlinx.serialization.encoding.Decoder
11+
import kotlinx.serialization.encoding.Encoder
12+
import kotlin.reflect.KClass
13+
14+
@OptIn(ExperimentalSerializationApi::class)
15+
interface KBufSerializer<T, B: ByteBuf>: KSerializer<T> {
16+
17+
val bufClass: KClass<B>
18+
19+
override fun serialize(encoder: Encoder, value: T) {
20+
require(encoder is BufEncoder<*>) { "This serializer can only be used with BufEncoder<$bufClass>" }
21+
22+
val buf = encoder.buf
23+
require(bufClass.isInstance(buf)) {
24+
"Expected ByteBuf of type ${bufClass.qualifiedName}, but got ${buf::class.qualifiedName}"
25+
}
26+
27+
serialize0(buf as B, value)
28+
}
29+
30+
fun serialize0(buf: B, value: T)
31+
32+
override fun deserialize(decoder: Decoder): T {
33+
require(decoder is BufDecoder<*>) { "This serializer can only be used with BufDecoder<$bufClass>" }
34+
35+
val buf = decoder.buf
36+
require(bufClass.isInstance(buf)) {
37+
"Expected ByteBuf of type ${bufClass.qualifiedName}, but got ${buf::class.qualifiedName}"
38+
}
39+
40+
return deserialize0(buf as B)
41+
}
42+
43+
fun deserialize0(buf: B): T
44+
}

src/main/kotlin/dev/slne/surf/bytebufserializer/internal/BufDecoder.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.slne.surf.bytebufserializer.internal
22

3+
import dev.slne.surf.bytebufserializer.BufConfiguration
34
import io.netty.buffer.ByteBuf
45
import kotlinx.serialization.ExperimentalSerializationApi
56
import kotlinx.serialization.descriptors.SerialDescriptor
@@ -11,7 +12,7 @@ import kotlinx.serialization.modules.SerializersModule
1112
class BufDecoder<B : ByteBuf>(
1213
val buf: B,
1314
override val serializersModule: SerializersModule,
14-
private val decodeEnumWithOrdinal: Boolean,
15+
private val configuration: BufConfiguration,
1516
private var elementsCount: Int = 0
1617
) : AbstractDecoder() {
1718
private var elementIndex = 0
@@ -62,7 +63,7 @@ class BufDecoder<B : ByteBuf>(
6263
}
6364

6465
override fun decodeEnum(enumDescriptor: SerialDescriptor): Int {
65-
return if (decodeEnumWithOrdinal) {
66+
return if (configuration.enumsWithOrdinal) {
6667
buf.readByte().toInt()
6768
} else {
6869
val name = decodeString()
@@ -71,7 +72,7 @@ class BufDecoder<B : ByteBuf>(
7172
}
7273

7374
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
74-
return BufDecoder(buf, serializersModule, decodeEnumWithOrdinal, descriptor.elementsCount)
75+
return BufDecoder(buf, serializersModule, configuration, descriptor.elementsCount)
7576
}
7677

7778
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int =

src/main/kotlin/dev/slne/surf/bytebufserializer/internal/BufEncoder.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.slne.surf.bytebufserializer.internal
22

3+
import dev.slne.surf.bytebufserializer.BufConfiguration
34
import io.netty.buffer.ByteBuf
45
import kotlinx.serialization.ExperimentalSerializationApi
56
import kotlinx.serialization.descriptors.SerialDescriptor
@@ -11,7 +12,7 @@ import kotlinx.serialization.modules.SerializersModule
1112
class BufEncoder<B : ByteBuf>(
1213
val buf: B,
1314
override val serializersModule: SerializersModule,
14-
private val encodeEnumWithOrdinal: Boolean,
15+
private val configuration: BufConfiguration,
1516
) : AbstractEncoder() {
1617
override fun encodeBoolean(value: Boolean) = useCustomOr(value) {
1718
buf.writeBoolean(value)
@@ -52,7 +53,7 @@ class BufEncoder<B : ByteBuf>(
5253
}
5354

5455
override fun encodeEnum(enumDescriptor: SerialDescriptor, index: Int) {
55-
if (encodeEnumWithOrdinal) {
56+
if (configuration.enumsWithOrdinal) {
5657
encodeInt(index)
5758
} else {
5859
encodeString(enumDescriptor.getElementName(index))

0 commit comments

Comments
 (0)