Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core-kotlin-modules/core-kotlin-11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Relevant Articles
65 changes: 65 additions & 0 deletions core-kotlin-modules/core-kotlin-11/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-kotlin-11</artifactId>
<name>core-kotlin-11</name>
<packaging>jar</packaging>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>core-kotlin-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-serialization</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-json</artifactId>
<version>${kotlinx.serialization.version}</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware.kryo</groupId>
<artifactId>kryo</artifactId>
<version>${kryo.version}</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>kotlinx-serialization</plugin>
</compilerPlugins>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<kotlinx.serialization.version>1.4.1</kotlinx.serialization.version>
<kryo.version>2.24.0</kryo.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.baeldung.dataclasstobytebuffer

import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer

data class User(val id: Int = 0, val name: String = "", val age: Int = 0)

fun User.manualToByteBuffer(): ByteBuffer {
val nameBytes = this.name.toByteArray(Charsets.UTF_8)
val buffer = ByteBuffer.allocate(Int.SIZE_BYTES + nameBytes.size + Int.SIZE_BYTES)

buffer.putInt(this.id)
buffer.put(nameBytes)
buffer.putInt(this.age)

buffer.flip() // Prepare the buffer for reading

return buffer
}

fun ByteBuffer.manualToUser(): User {
val id = this.int
val nameBytes = ByteArray(this.remaining() - Int.SIZE_BYTES)
this.get(nameBytes)
val name = String(nameBytes, Charsets.UTF_8)
val age = this.int

return User(id, name, age)
}

// Using Kryo

fun User.toByteBufferWithKryo(kryo: Kryo): ByteBuffer {
val byteArrayOutputStream = ByteArrayOutputStream()
val output = Output(byteArrayOutputStream)
kryo.writeObject(output, this)
output.close()

val byteArray = byteArrayOutputStream.toByteArray()
return ByteBuffer.wrap(byteArray)
}

fun ByteBuffer.toUserWithKryo(kryo: Kryo): User {
val byteArray = ByteArray(this.remaining())
this.get(byteArray)

val byteArrayInputStream = ByteArrayInputStream(byteArray)
val input = Input(byteArrayInputStream)
return kryo.readObject(input, User::class.java).also { input.close() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.dataclasstobytebuffer

import java.io.*
import java.nio.ByteBuffer

data class UserInputOutputStream(val id: Int, val name: String, val age: Int) : Serializable

fun UserInputOutputStream.outputStreamByteBuffer(): ByteBuffer {
val byteArrayOutputStream = ByteArrayOutputStream()
val objectOutputStream = ObjectOutputStream(byteArrayOutputStream)

objectOutputStream.writeObject(this)
objectOutputStream.flush()

val byteArray = byteArrayOutputStream.toByteArray()
return ByteBuffer.wrap(byteArray)
}

fun ByteBuffer.inputStreamToUser(): UserInputOutputStream {
val byteArray = ByteArray(this.remaining())
this.get(byteArray)

val byteArrayInputStream = ByteArrayInputStream(byteArray)
val objectInputStream = ObjectInputStream(byteArrayInputStream)

return objectInputStream.readObject() as UserInputOutputStream
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.dataclasstobytebuffer

import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets

@Serializable
data class UserSerialization(val id: Int, val name: String, val age: Int)

fun UserSerialization.userToJson(): ByteBuffer {
val jsonString = Json.encodeToString(this)
val byteArray = jsonString.toByteArray()
return ByteBuffer.wrap(byteArray)
}

fun ByteBuffer.jsonToUser(): UserSerialization {
val byteArray = ByteArray(this.remaining())
this.get(byteArray)
val jsonString = String(byteArray, StandardCharsets.UTF_8)
return Json.decodeFromString(jsonString)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.baeldung.dataclasstobytebuffer

import com.esotericsoftware.kryo.Kryo
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals

class DataClassToByteBufferUnitTest {

private val kryo = Kryo().apply {
register(User::class.java)
}

@Test
fun `Manual serialize and deserialize with fixed size`() {
val user = User(id = 1, name = "Alice", age = 30)

val serializedUser = user.manualToByteBuffer()

val deserializedUser = serializedUser.manualToUser()

assertEquals(user, deserializedUser)
}

@Test
fun `Serialize and deserialize using byte array output and input stream`() {
val user = UserInputOutputStream(id = 1, name = "Alice", age = 30)

val serializedUser = user.outputStreamByteBuffer()

val deserializedUser = serializedUser.inputStreamToUser()

assertEquals(user, deserializedUser)
}

@Test
fun `Serialize and deserialize using kotlin serialization library`() {
val user = UserSerialization(id = 1, name = "Alice", age = 30)

val serializedUser = user.userToJson()

val deserializedUser = serializedUser.jsonToUser()

assertEquals(user, deserializedUser)
}

@Test
fun `Serialize and deserialize using Kryo library`() {
val user = User(id = 1, name = "Alice", age = 30)

val serializedUser = user.toByteBufferWithKryo(kryo)

val deserializedUser = serializedUser.toUserWithKryo(kryo)

assertEquals(user, deserializedUser)
}
}
1 change: 1 addition & 0 deletions core-kotlin-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module>core-kotlin-8</module>
<module>core-kotlin-9</module>
<module>core-kotlin-10</module>
<module>core-kotlin-11</module>
<module>core-kotlin-advanced</module>
<module>core-kotlin-advanced-2</module>
<module>core-kotlin-advanced-3</module>
Expand Down