Chas,

I am sorry for my late response.

Thank you very much for your explanation and the code.
It was extremely helpful!

I had problems related to my classpath (I need to sleep more...),
so I ended up with a modified version that can load any file,
whether it is in the classpath or not.
I paste it here just in case somebody also wants it.

Thank you very much, Chas!

Paul.
------

(defn deser-from-file*
  "Returns object stored in file f. Alternative version based on Chas
Emerick's code."
  [f]
  (with-open [r (->     (java.io.FileInputStream. f)
                                (java.io.InputStreamReader. "UTF-8")
                                (java.io.BufferedReader.)
                                (java.io.PushbackReader.))]
    (read r) ))


On Aug 20, 9:43 pm, Chas Emerick <cemer...@snowtide.com> wrote:
> load-string evaluates the contents of the string, which brings in all  
> of the compilation machinery, which produces bytecode, classes, etc.  
> Classfiles have a 64K size limit in typical JVM implementations.
>
> You want to use the read fn (which requires a PushbackReader), as all  
> you're interested in is the data in the file -- you don't want or need  
> code generation. Here's the fn I use for doing exactly this for  
> resources in the classpath:
>
> (defn read-from-classpath
>    "Loads the readable content from the given path within the current  
> classpath using read."
>    [rsrc-path]
>    (with-open [r (java.io.PushbackReader.
>                    (java.io.InputStreamReader.
>                      (.getResourceAsStream java.lang.String rsrc-path)
>                      "UTF-8"))]
>      (read r)))
>
> Cheers,
>
> - Chas
>
> On Aug 20, 2009, at 4:02 AM, Paul GT wrote:
>
>
>
> > Dear list,
>
> > I am writing some functions to serialize and deserialize clojure data
> > structures,
> > but somehow they do not work and I am stuck.
>
> > The functions are as follows:
>
> > (use 'clojure.contrib.duck-streams)
>
> > (defn ser
> >  "Returns the string serialization of object o."
> >  [o]
> >  (binding [*print-dup* true] (pr-str o)))
>
> > (defn deser
> >  "Returns the deserialization (excecutes) of string s."
> >  [s]
> >  (load-string s))
>
> > (defn ser-to-file
> >  "Writes object o to file f."
> >  [o f]
> >  (spit f (ser o)))
>
> > (defn deser-from-file
> >  "Returns object stored in file f."
> >  [f]
> >  (deser (slurp f)))
>
> > With the above definitions, I can run the code below and it executes
> > correctly:
>
> > (ser-to-file [1 2 3] "my-file.txt") ;; Ok
> > (deser-from-file "my-file.txt") ;; Ok
>
> > But if instead of a small vector like [1 2 3]
> > I use a big one (say ten thousand entries),
> > I receive an error message:
>
> > (ser-to-file (into [] (range 10000)) "my-file.txt")      ;; Ok
> > (deser-from-file "my-file.txt")     ;; Error!
> > java.lang.ClassFormatError: Invalid method Code length 89867 in class
> > file user$eval__672 (NO_SOURCE_FILE:80)
>
> > Actually, I first tried with a vector of one million entries,
> > but it consumed a lot of memory.
> > I started making it smaller and smaller and, on my machine,
> > when the vector has a length of 7200, it works.
> > But when it is 7300, the error comes up.
>
> > I have tried it with clojure 1.0 and the latest 1.1 alpha in github,
> > both Vista and Ubuntu.
>
> > Looks like the problem is in the deserialization,
> > because if I look at "my-file.txt" the vector is written correctly.
>
> > I do not know what I am doing wrong.
> > If anybody could shed some light, I would be really grateful.
> > I am sorry if this seems like a simple issue or the solution is
> > obvious.
>
> > Thank you very much for your patience.
>
> > Paul.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to