Created
August 10, 2015 22:53
-
-
Save searler/d2c9b65901c2ad6a856b to your computer and use it in GitHub Desktop.
This file contains 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 scodec.Decoder | |
import scodec.bits.BitVector | |
import scodec.Err | |
import scodec.Attempt | |
import scodec.DecodeResult | |
class FixedSizeDecoder[A](sizeBytes: Long, decoder: Decoder[A]) extends Decoder[A] { | |
val size = sizeBytes * 8 | |
override def decode(buffer: BitVector): Attempt[DecodeResult[A]] = { | |
if (buffer.sizeGreaterThanOrEqual(size)) { | |
decoder.decode(buffer.take(size)) map { res => | |
DecodeResult(res.value, buffer.drop(size)) | |
} | |
} else { | |
Attempt.failure(Err.insufficientBits(size, buffer.size)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment