3

I'm trying to implement a Java extension for JRuby to perform string xors. I'm just uncertain how to type cast a byte array into a RubyString:

public static RubyString xor(ThreadContext context,  IRubyObject self, RubyString x, RubyString y) {
    byte[] xBytes = x.getBytes();
    byte[] yBytes = y.getBytes();

    int length = yBytes.length < xBytes.length ? yBytes.length : xBytes.length;

    for(int i = 0; i < length; i++) {
        xBytes[i] = (byte) (xBytes[i] ^ yBytes[i]);
    }

    // How to return a RubyString with xBytes as its content?
}

Also, how would one perform the same operation in-place (i.e. xs value is updated)?

3
  • There are constructors on RubyString that accept a byte[] here org.jruby.RubyString.RubyString(Ruby runtime, RubyClass rubyClass, byte[] value) not sure if that's applicable in this instance? Commented Jul 13, 2015 at 3:47
  • Looks promising. Any idea what the rubyClass argument is supposed to be? Commented Jul 13, 2015 at 4:00
  • Not sure about that argument unfortunately. I've used Jruby for years, but haven't tried writing an extension before. Dropping by the #jruby channel on freenode irc may be the quickest way to get some more info Commented Jul 13, 2015 at 4:03

2 Answers 2

1

return context.runtime.newString(new ByteList(xBytes, false));

Sign up to request clarification or add additional context in comments.

1 Comment

What is this? Some explanation would be nice. If you don't want to do that, post a comment.
0

You'll first need to wrap the bytes in a ByteList: new ByteList(xBytes, false). The last parameter (Boolean copy) dictates whether to wrap a copy of the Byte array.

To update the string in place, use [RubyString#setValue()][2]:

x.setValue(new ByteList(xBytes, false);
return x;

To return a new RubyString, you can pass that list to the current runtime's #newString():

return context.runtime.newString(new ByteList(xBytes, false));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.