On Mon, Aug 10, 2009 at 10:42 PM, Kyle R. Burton<[email protected]> wrote:
>> Is there a way to do binary serialization of Clojure/Java values?
>> ASCII (read) and (write) are nice, but they are wasting space,
>> truncating floats and are probably slow compared to binary
>> serialization.
>
> The following utility functions have worked in many cases for me:
>
> (defn object->file [obj file]
> (with-open [outp (java.io.ObjectOutputStream.
> (java.io.FileOutputStream. file))]
> (.writeObject outp obj)))
>
>
> (defn file->object [file]
> (with-open [inp (java.io.ObjectInputStream. (java.io.FileInputStream. file))]
> (.readObject inp)))
>
> (defn freeze
> ([obj]
> (with-open [baos (java.io.ByteArrayOutputStream. 1024)
> oos (java.io.ObjectOutputStream. baos)]
> (.writeObject oos obj)
> (.toByteArray baos)))
> ([obj & objs]
> (freeze (vec (cons obj objs)))))
Sorry, forgot to offer up the inverse of freeze, thaw:
(defn thaw [bytes]
(with-open [bais (java.io.ByteArrayInputStream. bytes)
ois (java.io.ObjectInputStream. bais)]
(.readObject ois)))
Regards,
Kyle
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---