[ringojs] Creating a ByteString from a bytes array.

3 views
Skip to first unread message

George Moschovitis

unread,
May 7, 2010, 2:56:06 PM5/7/10
to RingoJS
In narwhal I am using this hack to initialize a ByteString:

var bytes = <java byte[]>;

var ByteString = require("binary").ByteString;

var b = new ByteString();
b._bytes = bytes;
b._offset = 0;
b._length = Number(b._bytes.length);

How can I accomplish something similar in RingoJS? Is there a standard
CommonJS way to achieve this?

-g.

--
You received this message because you are subscribed to the Google Groups "RingoJS" group.
To post to this group, send email to rin...@googlegroups.com.
To unsubscribe from this group, send email to ringojs+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ringojs?hl=en.

Hannes Wallnoefer

unread,
May 7, 2010, 3:09:26 PM5/7/10
to rin...@googlegroups.com
2010/5/7 George Moschovitis <george.mo...@gmail.com>:
> In narwhal I am using this hack to initialize a ByteString:
>
> var bytes = <java byte[]>;
>
> var ByteString = require("binary").ByteString;
>
> var b = new ByteString();
> b._bytes = bytes;
> b._offset = 0;
> b._length = Number(b._bytes.length);
>
> How can I accomplish something similar in RingoJS? Is there a standard
> CommonJS way to achieve this?

In ringojs, you just do:

var b = new ByteString(bytes);

or, if you explicitly don't want the byte array to be copied (but then
changing the underlying byte[] would break the "immutability contract"
of ByteString):

var b = ByteString.wrap(byte);

also, you can pass a ByteString or ByteArray into any Java method
taking a byte[], it will automatically be unwrapped.

I don't think there's a standard CommonJS way to do this since byte[]
is inherently Java-specific. It would be nice to have a common way of
doing this between ringojs and narwhal/rhino, though.

Hannes

George Moschovitis

unread,
May 7, 2010, 4:22:15 PM5/7/10
to RingoJS
> In  ringojs, you just do:
>
> var b = new ByteString(bytes);
> var b = ByteString.wrap(byte);

perfect!

> also, you can pass a ByteString or ByteArray into any Java method
> taking a byte[], it will automatically be unwrapped.

cool, I am wondering how this is implemented...

> It would be nice to have a common way of doing this between ringojs and narwhal/rhino, though.

yeah, more cooperation between narwhal/ringo and ringojs would be
great.

regards,
Reply all
Reply to author
Forward
0 new messages