Convert JRuby string to Java byte array, and back again

Theo picture Theo · May 24, 2011 · Viewed 7.8k times · Source

I'm trying to pass a binary string in JRuby as a byte[] through some Java library and into JRuby again, where I want to convert it back into a string, but I can't figure out how to do it without the string getting messed up.

Specifically I'm encoding a Ruby hash as BSON and passing it over AMQP, but it's the conversion to and from byte[] that doesn't work. This code

import org.jruby.RubyString
blob = BSON.serialize({'test' => 123123123123}).to_s
p blob
p RubyString.bytes_to_string(RubyString.string_to_bytes(blob))

outputs

"\x13\x00\x00\x00\x12test\x00\xB3\xC3\xB5\xAA\x1C\x00\x00\x00\x00"
"\x13\x00\x00\x00\x12test\x00\xC2\xB3\xC3\x83\xC2\xB5\xC2\xAA\x1C\x00\x00\x00\x00"

I have also tried

java.lang.String.new(blob.to_java.bytes).to_s

but it outputs the same, wrong, string.

Is there any easier/safer way to convert to and from a JRuby string and byte[]?

Answer

Theo picture Theo · May 24, 2011

I found the answer myself, turned out there was a #to_java_bytes on String, and a helper method .from_java_bytes that handle the conversion without issue:

blob = BSON.serialize({'test' => 123123123123}).to_s
p blob
p String.from_java_bytes(blob.to_java_bytes)