jaya rohith
jaya rohith

Reputation: 311

Ruby Convert a Java Byte Buffer object to String

Hi i am having an object in ruby which is of type Byte ByteBuffer

Object type: java.nio.HeapByteBuffer[pos=0 lim=437 cap=437]

I want to convert/decode this object into String in ruby

Upvotes: 0

Views: 314

Answers (1)

kares
kares

Reputation: 7181

There's String.from_java_bytes extension however it does not take an (optional) offset and length thus first copy the buffer's content into a new byte[] array:

>> bytes = Java::byte[buffer.limit].new
=> byte[0, 0, 0]@3427b02d
>> buffer.get(bytes)
=> #<Java::JavaNio::HeapByteBuffer:0x3967e60c>
>> bytes
=> byte[102, 111, 111]@3427b02d
>> String.from_java_bytes bytes

Upvotes: 1

Related Questions