Skip to content

Instantly share code, notes, and snippets.

@searler
Created July 20, 2015 01:32
Show Gist options
  • Save searler/919559baa4fdba2dfc0c to your computer and use it in GitHub Desktop.
Save searler/919559baa4fdba2dfc0c to your computer and use it in GitHub Desktop.
Self contained ByteString scodec decoder mechanism
import akka.util.ByteString
import scodec.Attempt.Successful
import scodec.DecodeResult
import scodec.Decoder
import scodec.bits.BitVector
class DecoderGather[R](decoder: Decoder[R]) {
private var bitBuffer = BitVector.empty
def apply(chunk: ByteString): Seq[R] = {
chunk.asByteBuffers.map(BitVector.apply).foreach(bb => bitBuffer = bitBuffer ++ bb)
doParsing(Vector.empty)
}
@tailrec
private def doParsing(parsedSoFar: Vector[R]): Vector[R] =
decoder.decode(bitBuffer) match {
case Successful(DecodeResult(value, remainder)) =>
bitBuffer = remainder
doParsing(parsedSoFar :+ value)
case _ => parsedSoFar
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment