   [codec ^bytes byte-data]
   (let [bais (ByteArrayInputStream. byte-data)]
     (decode! codec bais)))
+
+
+(defn decode-with-header!
+  "Reads a multicodec header from the input stream and checks it against the one
+  provided. Throws an exception if the header does not match, otherwise uses the
+  codec to read a value from the stream.
+
+  If a header is not given, the codec's header is used."
+  ([codec input]
+   (decode-with-header! codec input (:header codec)))
+  ([codec input header]
+   (let [header' (header/read-header! input)]
+     (when-not (= header header')
+       (throw (ex-info
+                (format "The stream header %s did not match expected header %s"
+                        (pr-str header') (pr-str header))
+                {:expected header, :actual header'})))
+     (decode! codec input))))

    (.toByteArray baos)))



;; ## Decoding


(ns multicodec.core
  "Core multicodec protocols and functions."
  (:require
    [multicodec.header :as header])
  (:import
    (java.io
      ByteArrayInputStream
