Forum: JRuby Byte Arrays in JRuby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bc77637b2e39fcd4f3e8a6b61d82a2b5?d=identicon&s=25 Jabari Zakiya (jabari_z83)
on 2015-12-16 01:37
1) Does Java allow for creating byte arrays (each element is a single
8-bit addressable element)?

2) If so, can JRuby create and access them like regular Ruby class Array
objects?  (Ex: myarray = BArray.new; 10.times {|i| myarray << i}

3) Is there an existing Java/JRuby library/gem that provides this for
JRuby?

I'm doing numerical processing where I really need boolean arrays (an
array of bits), but since most cpus are byte addressable I guess the
best I can get, and still perform as regular Ruby arrays, is byte
addressable arrays.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-01-05 10:21
Jabari Zakiya wrote in post #1179970:
> 1) Does Java allow for creating byte arrays (each element is a single
> 8-bit addressable element)?


You have the type java.lang.Byte:

https://docs.oracle.com/javase/7/docs/api/java/lan...


> 2) If so, can JRuby create and access them like regular Ruby class Array
> objects?  (Ex: myarray = BArray.new; 10.times {|i| myarray << i}

Can't you just create a normal Ruby array, make sure that you stuff into
it java.lang.Byte objects, and when you finally need it on the Java
side, apply the 'to_java' method on it?

Ronald
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-01-11 16:30
BTW, on

https://github.com/jruby/jruby/wiki/FAQs

have a look at the section which says "How do I create a primitive Java
array in JRuby?"
Bc77637b2e39fcd4f3e8a6b61d82a2b5?d=identicon&s=25 Jabari Zakiya (jabari_z83)
on 2016-01-11 19:20
Thanks for your reply.

I am not a Java programmer (have done no "real" Java program) so I know
virtually nothing of manipulating the language.

I'll look at the links you supplied and see if they help me.

I'm trying to extend usable memory when running the same gem in MRI vs
JRuby.  JRuby runs out of memory much faster on my apps than MRI does on
certain use cases, making it more of a niche language for me than a
general go to Ruby implementation.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-01-12 07:33
I don't quite see, how working with byte arrays might help you. If I
remember right, there is an option when starting Java, where you set the
maximum memory to be used. If you don't find it in the Java docs, I
suggest you ask this in a Java forum.
Bc77637b2e39fcd4f3e8a6b61d82a2b5?d=identicon&s=25 Jabari Zakiya (jabari_z83)
on 2016-01-12 21:06
You can read why I would like to do this from the document below:

https://www.scribd.com/doc/266461408/Primes-Utils-Handbook
Bc77637b2e39fcd4f3e8a6b61d82a2b5?d=identicon&s=25 Jabari Zakiya (jabari_z83)
on 2016-01-12 21:29
Ah, I think I found the answer.

This is how to do it, according to this site:

https://github.com/jruby/jruby/wiki/FAQs

------------------------------------------------------------------------
How do I create a primitive Java array in JRuby?

You can convert existing ruby arrays to primitive Java arrays very
simply:

    [1,2,3].to_java # makes an object array
    [1,2,3].to_java :byte # makes a byte array
    [1,2,3].to_java :String # makes a String array

To create empty arrays:

    Java::byte[12].new # makes a new byte[]
    java.lang.String[12].new # makes a new String[]
This topic is locked and can not be replied to.