Scodec

The fs2-scodec library provides streaming encoding and decoding of binary data, based on the scodec library. Initially, this library was called scodec-stream and evolved independently for a long time before being integrated into FS2.

What is Scodec?

Scodec is a library for working with binary data in Scala, allowing you to describe binary protocols and data formats in a composable, type-safe, and purely functional style. Instead of manually managing offsets in byte buffers, you describe the data structure using encoding/decoding combinators, and the library handles all the low-level work.

Why use fs2-scodec?

Basic decoding example

Consider a program that reads a file and decodes a stream of frames from it. Each frame has the following format:

  1. The first 4 bytes are a 32-bit signed integer indicating the length of the frame payload.
  2. This is followed by the specified number of bytes — the actual frame payload.
import cats.effect.{IO, IOApp}
import scodec.bits.*
import scodec.codecs.*
import fs2.Stream
import fs2.interop.scodec.*
import fs2.io.file.{Files, Path}

object Decode extends IOApp.Simple:

  def run: IO[Unit] =
    // Create a decoder that reads a stream of frames
    val frames: StreamDecoder[ByteVector] =
      StreamDecoder.many(int32).flatMap { numBytes =>
        // For each frame: read length (int32), then read numBytes of payload
        StreamDecoder.once(bytes(numBytes))
      }

    // File path
    val filePath = Path("largefile.bin")

    // Read the file in a stream and decode it with our decoder
    val s: Stream[IO, ByteVector] =
      Files[IO].readAll(filePath).through(frames.toPipeByte)

    // Count the number of successfully decoded frames
    s.compile.count.flatMap(cnt => IO.println(s"Read $cnt frames."))

When executed, this program will incrementally read data from the "largefile.bin" file and decode a stream of frames according to the described format.

Detailed example breakdown

Creating a decoder

val frames: StreamDecoder[ByteVector] =
  StreamDecoder.many(int32).flatMap { numBytes =>
    StreamDecoder.once(bytes(numBytes))
  }

Here, decomposition into two parts is used:

  1. StreamDecoder.many(int32) — creates a decoder that repeatedly applies the int32 codec to the input stream, extracting 32-bit integers one after another. Each extracted value represents the length of the next frame.
  2. .flatMap { numBytes => StreamDecoder.once(bytes(numBytes)) } — for each extracted numBytes value, a new decoder is created that applies the bytes(numBytes) codec exactly once. This codec reads exactly numBytes bytes from the input stream and returns them as a ByteVector. The process then repeats: we read int32 again (using StreamDecoder.many), then bytes again, and so on until the input stream is exhausted.
flatMap at the StreamDecoder level works such that each time the decoder extracts a value, it can use that value to dynamically choose the next decoder. This allows describing protocols with variable field lengths, where the size of the next field depends on the value of the previous one.

An alternative way to write the same decoder using the decodeMany method:

val frames: StreamDecoder[ByteVector] =
  StreamDecoder.decodeMany(int32.flatMap { numBytes =>
    bytes(numBytes)
  })

Reading a file and applying the decoder

val s: Stream[IO, ByteVector] =
  Files[IO].readAll(filePath).through(frames.toPipeByte)

Counting results

s.compile.count.flatMap(cnt => IO.println(s"Read $cnt frames."))

Core types: StreamDecoder and StreamEncoder

The fs2-scodec library provides two core types:

StreamDecoder[A]

StreamDecoder[A] represents a decoder that transforms a byte stream into a stream of values of type A. It is a purely functional description of how to extract structured data from a byte stream.

Constructors and combinators of StreamDecoder:

StreamEncoder[A]

StreamEncoder[A] represents an encoder that transforms a stream of values of type A into a byte stream. It is the mirror image of StreamDecoder.

Main constructors:

Error handling and incomplete data

Scodec distinguishes two types of errors:

  1. Decoding error (Err) — occurs when the data structure does not match expectations (e.g., invalid format, buffer overread).
  2. Insufficient data — occurs when there are not enough bytes in the buffer to complete decoding.

Both of these situations are handled transparently in StreamDecoder:


References: