Skip to content

Instantly share code, notes, and snippets.

@thomasuster
Created January 24, 2017 00:53
Show Gist options
  • Save thomasuster/d6b52406490f452982ba58eac1a84f46 to your computer and use it in GitHub Desktop.
Save thomasuster/d6b52406490f452982ba58eac1a84f46 to your computer and use it in GitHub Desktop.
Convert a haxe.io.BytesBuffer to js.html.ArrayBuffer
package com.thomasuster.js;
import js.html.Blob;
import haxe.io.Bytes;
import haxe.io.BytesBuffer;
import js.html.ArrayBuffer;
import js.html.DataView;
class ByteBufferConverter {
public function new():Void {}
public function toArrayBuffer(bb:BytesBuffer):ArrayBuffer {
var bytes:Bytes = bb.getBytes();
var buffer:ArrayBuffer = new ArrayBuffer(bytes.length);
var view:DataView = new DataView(buffer, 0, buffer.byteLength);
for (i in 0...bytes.length) {
view.setUint8(i, bytes.get(i));
}
return buffer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment