A question came up recently about using cbor/decode to lazily parse an input stream, where data from the underlying stream was getting lost.
This is due to the fact that internally, decode will automatically coerce its argument into a java.io.DataInputStream by first calling clojure.java.io/input-stream on it. The implementation of input-stream always wraps a java.io.BufferedInputStream around the result, which in turn means that the first read on a bare InputStream sucks more data than expected into the internal buffer, which is then discarded!
The immediate solution is to make sure you're always passing a DataInputStream to the decode methods, but a better approach would be to not try coercing things that are already InputStreams and to clearly document this behavior on the functions.
A question came up recently about using
cbor/decode
to lazily parse an input stream, where data from the underlying stream was getting lost.This is due to the fact that internally,
decode
will automatically coerce its argument into ajava.io.DataInputStream
by first callingclojure.java.io/input-stream
on it. The implementation ofinput-stream
always wraps ajava.io.BufferedInputStream
around the result, which in turn means that the first read on a bareInputStream
sucks more data than expected into the internal buffer, which is then discarded!The immediate solution is to make sure you're always passing a
DataInputStream
to thedecode
methods, but a better approach would be to not try coercing things that are alreadyInputStream
s and to clearly document this behavior on the functions.