Created
July 20, 2015 01:32
-
-
Save searler/919559baa4fdba2dfc0c to your computer and use it in GitHub Desktop.
Self contained ByteString scodec decoder mechanism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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