Skip to content

Instantly share code, notes, and snippets.

@searler
Created August 10, 2015 22:53
Show Gist options
  • Save searler/d2c9b65901c2ad6a856b to your computer and use it in GitHub Desktop.
Save searler/d2c9b65901c2ad6a856b to your computer and use it in GitHub Desktop.
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