greglook / clj-cbor

Native Clojure CBOR codec implementation.
The Unlicense
70 stars 7 forks source link

Buffering behavior in decode is non-obvious #13

Closed greglook closed 5 years ago

greglook commented 5 years ago

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.