If I'm holding a binary blob from a db query result, is clojure.java.io/input-stream the right way to present it as a stream?
I'm doing (:require [org.clojars.smee.binary.core :as binary]) (:require [clojure.java.io :as io]) ... (defn score-decode [blob] (binary/decode codec-length (io/input-stream blob))) which seems to work. On Tuesday, October 9, 2012 12:09:18 AM UTC-7, Steffen wrote: > > Hi, > > I wrote a binary parser combinator(ish) library that uses java's > input/outputstreams (https://github.com/smee/binary) that may help (add > [org.clojars.smee/binary "0.2.0"] to your project's leiningen dependencies). > > As I use streams, I have no way of knowing the size of a binary blob > beforehand, so in principal you have three options: Using a length prefix, > using an arbitrary prefix or iteratively read floats until you run out of > bytes: > > >> (use 'org.clojars.smee.binary.core)) >> >> >>> (def codec-prefix (repeated :float :prefix :int)) >> >> (def codec-length (repeated :float :length 50)) >> >> (def codec-simple (compile-codec :float)) >> >> >>> ;; call with either >> >> (defn by-prefix [^java.io.InputStream in] >> >> (decode codec-prefix in)) >> >> >>> (defn by-length [^java.io.InputStream in] >> >> (decode codec-length in)) >> >> >>> (defn by-greedy [^java.io.InputStream in] >> >> (take-while (complement nil?) >> >> (repeatedly #(try >> >> (decode codec-simple in) >> >> (catch java.io.EOFException e nil))))) >> >> >>> > Each call would return a sequence of floats. You can write a sequence of > floats back with "encode", of course. > Currently the documentation is nothing to be proud of, but it is in the > pipeline. Instead please refer to the beginning of an mp3 idv2 parser at > https://github.com/smee/binary/blob/master/src/org/clojars/smee/binary/demo/mp3.clj > or > the tests of the project. > > Steffen > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
