not fast, but it works:) any ideas on better code? ;;;;;code from http://www.mail-archive.com/clojure@googlegroups.com/msg19378.html ;;saving objects to strings and going back -slow, but it works (import (java.io ByteArrayOutputStream ObjectOutputStream ByteArrayInputStream ObjectInputStream))
(use 'clojure.contrib.duck-streams) (defn str-to-bytes [s] (.getBytes s)) (defn str-from-bytes [b] (new String b)) ; encode a raw array of bytes as a base64 array of bytes (defn encode64 [b] (. (new sun.misc.BASE64Encoder) encode b)) ; decode a string encoded in base 64, result as array of bytes (defn decode64 [s] (let [decoder (new sun.misc.BASE64Decoder)] (. decoder decodeBuffer s))) (defn obj-to-bytes "convert object to string" [obj] (with-open [bos (ByteArrayOutputStream.) stream (ObjectOutputStream. bos)] (.writeObject stream obj) (.flush stream) (.toByteArray bos))) (defn bytes-to-obj "convert string to object" [s] (.readObject (ObjectInputStream. (ByteArrayInputStream. s)))) ; compress human readable string and return it as base64 encoded (defn encode [s] (let [b (obj-to-bytes s) output (new java.io.ByteArrayOutputStream) deflater (new java.util.zip.DeflaterOutputStream output (new java.util.zip.Deflater) 1024)] (. deflater write b) (. deflater close) (str-from-bytes (encode64 (. output toByteArray))))) ; take an object that was compressed & base64 encoded.. and undo allthat (defn decode [s] (let [b (decode64 s) input (new java.io.ByteArrayInputStream b) inflater (new java.util.zip.InflaterInputStream input (new java.util.zip.Inflater) 1024) result (to-byte-array inflater)] (. inflater close) (bytes-to-obj result))) (defmethod print-dup :default [o w] (.write w (str "#=(forex.module.ea/decode \"" (encode o) "\")"))) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en