javascript - Converting byte[] to ArrayBuffer in Nashorn - Stack Overflow

How do I convert an array of bytes into ArrayBuffer in Nashorn? I am trying to insert binary data into

How do I convert an array of bytes into ArrayBuffer in Nashorn? I am trying to insert binary data into a pure JavaScript environment (i.e., it doesn't have access to Java.from or Java.to) and so would like to create an instance out an array of bytes.

How do I convert an array of bytes into ArrayBuffer in Nashorn? I am trying to insert binary data into a pure JavaScript environment (i.e., it doesn't have access to Java.from or Java.to) and so would like to create an instance out an array of bytes.

Share Improve this question asked Jan 15, 2016 at 21:46 Vivin PaliathVivin Paliath 95.6k42 gold badges230 silver badges301 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Looks like I was going about this the wrong way. It made more sense to convert it into Uint8Array since what I'm sending in is an array of bytes.

I created the following function:

function byteToUint8Array(byteArray) {
    var uint8Array = new Uint8Array(byteArray.length);
    for(var i = 0; i < uint8Array.length; i++) {
        uint8Array[i] = byteArray[i];
    }

    return uint8Array;
}

This will convert an array of bytes (so byteArray is actually of type byte[]) into a Uint8Array.

I think you're right about using a Uint8Array, but this code might be preferable:

function byteToUint8Array(byteArray) {
    var uint8Array = new Uint8Array(byteArray.length);
    uint8Array.set(Java.from(byteArray));
    return uint8Array;
}

Also, if you really need an ArrayBuffer you can use uint8Array.buffer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743705172a4493243.html

相关推荐

  • javascript - Converting byte[] to ArrayBuffer in Nashorn - Stack Overflow

    How do I convert an array of bytes into ArrayBuffer in Nashorn? I am trying to insert binary data into

    2天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信