All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zink.scala.fly.stub.BufferedSerializingFieldCodec.scala Maven / Gradle / Ivy

package com.zink.scala.fly.stub

import com.zink.scala.fly.{FieldCodec, FlyPrime}
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.nio.ByteBuffer

class BufferedSerializingFieldCodec extends FieldCodec {

    val baos:ByteArrayOutputStream = new ByteArrayOutputStream(FlyPrime.DEFAULT_BUFFER_SIZE)
    val oos:ObjectOutputStream = new ObjectOutputStream(baos)
    oos.flush()
    val bytes = baos.toByteArray
    baos.reset()
    oos.reset()

    val bbis:ByteBufferInputStream  = new ByteBufferInputStream(ByteBuffer.wrap(bytes))
    val ois:ObjectInputStream  = new ObjectInputStream(bbis)

    override def writeField(field:AnyRef) = {
        baos.reset()
        oos.writeObject(field)
        oos.flush()
        oos.reset()
        baos.toByteArray
    }

    override def readField(fieldBytes:Array[Byte]):Option[AnyRef] = {
        bbis.switchBuffer(ByteBuffer.wrap(fieldBytes))
        Option(ois.readObject)
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy