Using a java nio ByteBuffer to simulate what you're doing, the
following works ok for me;

(defmulti t-str class)
(defmethod t-str String [s] (java.nio.ByteBuffer/wrap
  (.getBytes s "us-ascii")))

(t-str "abcde")
#<HeapByteBuffer java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]>

(defmethod t-str String [s] (java.nio.ByteBuffer/wrap
  (.getBytes s "utf-8")))

(t-str "abcde")
#<HeapByteBuffer java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]>

Maybe there's something about the particular [ s ] object that you're
passing in?

Rgds, Adrian.


On Tue, May 12, 2009 at 8:45 PM, tsuraan <tsur...@gmail.com> wrote:
>
> I'm trying to encode a java string into utf-8 for encapsulation within
> an OtpErlangBinary
> (http://erlang.org/doc/apps/jinterface/java/com/ericsson/otp/erlang/OtpErlangBinary.html).
>  When I try to construct an OtpErlangBinary from the results of
> String.getBytes(encoding), I get bad data.  A string (pure ascii) with
> 20 characters becomes a 47-byte OtpErlangBinary, and none of the bytes
> in that binary seem to correspond to the bytes of the string.  The
> simple function that I have looks like this:
>
> (defmethod to-otp String [ s ]
>  (new OtpErlangBinary (.getBytes s "utf-8")))
>
> And the OtpErlangBinary gets 47 bytes of data for a 20 byte string.
> However, if I change that code to read:
>
> (defmethod to-otp String [ s ]
>  (new OtpErlangBinary (.getBytes (str s) "utf-8")))
>
> (notice the (str s)), the code works.  It seems really strange to me
> that this should happen, but I don't know clojure well enough to
> determine what's going on under the hood.  Is there some way to dump
> the java code equivalent of those two functions, so I can compare
> them?   I've tried making a minimal test case for converting strings
> into OtpErlangBinaries, but I can't get this bug to manifest in any
> circumstances other than this one program.
>
> Any tips for debugging would be much appreciated.  I'm running clojure
> 1.0.0, with java 1.6.0, and erlang jinterface 1.4.
>
> >
>

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