Skip to content

Commit

Permalink
Added CompressingSerde
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed May 5, 2024
1 parent 1c45b96 commit b2f3871
Showing 1 changed file with 18 additions and 0 deletions.
@@ -1,9 +1,27 @@
package com.sksamuel.centurion.avro.io

import org.apache.avro.file.Codec
import java.nio.ByteBuffer

/**
* A [Serde] provides an easy way to convert between a data class [T] and avro encoded bytes.
*/
interface Serde<T : Any> {
fun serialize(obj: T): ByteArray
fun deserialize(bytes: ByteArray): T
}

class CompressingSerde<T : Any>(private val codec: Codec, private val serde: Serde<T>) : Serde<T> {

override fun serialize(obj: T): ByteArray {
val bytes = serde.serialize(obj)
val compressed = codec.compress(ByteBuffer.wrap(bytes))
val b = ByteArray(compressed.remaining())
compressed.get(b)
return b
}

override fun deserialize(bytes: ByteArray): T {
return serde.deserialize(codec.decompress(ByteBuffer.wrap(bytes)).array())
}
}

0 comments on commit b2f3871

Please sign in to comment.