Jsbinding+bridge: Manipulating binary data in logical code

Source: Internet
Author: User

Take these 2 functions as an example

class file{    publicstaticbyte[] ReadAllBytes (string  path);      Public Static void WriteAllBytes (stringbyte[] data);

If you do not do special processing, readallbytes in the return data to JS, is a byte of a byte copy to JS array. This performance is extremely poor. And, in most cases, the logic code does not need to directly modify the 2 binary data, but just hold it.

Therefore, do not use the above 2 functions directly.

The following is an optional scenario.

1. First implement a byte[] wrapper class in the framework code:

 Public classbytearray{ PublicByteArray (intLength =0) {bytes=New byte[length]; }     PublicByteArray (byte[] bytes_) {bytes=Bytes_; }     Public byte[] bytes;  Public intLength {Get        {        returnBytes! =NULL? bytes. Length:0; }    }     Public Static voidCopy (ByteArray src, ByteArray DST,intLen)    {array.copy (src.bytes, Dst.bytes, Len); }     Public Static voidCopy (ByteArray src,intSpos, ByteArray DST,intDposintLen)    {array.copy (src.bytes, Spos, Dst.bytes, Dpos, Len); }}

2. Fill in the ByteArray into the jsbindingsettings.classes array to export

3. For a function with a byte[] parameter and a return value of byte[] , write another version, and change the byte[] to ByteArray, for example, the above 2 function handwriting, becomes:

class // Change the name of the class {    publicstatic ByteArray readallbytes (string  path)    {         byte [] data = file.readallbytes (path);         return New ByteArray () {bytes = data};    }      Public Static void WriteAllBytes (string  path, ByteArray ByteArray)    {        file.writeallbytes (path, bytearray.bytes);}    }

4. Fill in the Fileex into the jsbindingsettings.classes array to export

5. In the logic code, the version of ByteArray is used, which involves byte[]. For example, the following code reads 2 binary data from a file, and then writes it to another file (the use of logical code to byte[] is a typical transit function)

ByteArray data = fileex.readallbytes ("d:/1.png"); Fileex.writeallbytes ("d:/2.png", data);

Back to: jsbinding+bridge.net:unity Game hot Update scheme

Jsbinding+bridge: Manipulating binary data in logical code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.