Skip to content

Conversation

@dfahlander
Copy link

If you want to encode a TypedArray, such as a Float64Array, Uint16Array or so, it can be backed by just a portion of an ArrayBuffer. If wanting to encode such a TypedArray without having to copy the portion into a new buffer, the encode function should take an offset and a length.

Example

var buffer = new ArrayBuffer(65536);
...fill buffer with something...

// Create a Uint16 view of portion of bytes between [1024...2047]:
var array = new Uint16Array(buffer, 1024, 512); // 512 = length in 16-bit units.

// Base64 encode that portion:
encode (array.buffer, array.byteOffset, array.byteLength);

If you want to encode a TypedArray, such as a Float64Array, Uint16Array or so, it can be backed by just a portion of an ArrayBuffer. If wanting to encode such a TypedArray without having to copy the portion into a new buffer, the encode function should take an offset and a length.

Example

```js
var buffer = new ArrayBuffer(65536);
...fill buffer with something...
// Create a Uint16 view of bytes [1024...2047]:
var array = new Uint16Array(buffer, 1024, 512); // 512 = length in 16-bit units.

encode (array.buffer, array.byteOffset, array.byteLength);

```
@brettz9
Copy link

brettz9 commented Feb 17, 2017

Any chance of getting this pulled?

This line:

var bytes = new Uint8Array(arraybuffer, offset || 0, length !== undefined ? length : arraybuffer.byteLength),

could be simplified, however, to:

var bytes = new Uint8Array(arraybuffer, offset, length),

as per https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length which references the ToIndex abstract operation indicating that if the byte offset is undefined, index should be 0. Likewise, for length, the steps indicate the equivalence of explicit or implicit undefined "If length is either not present or undefined, then..."

brettz9 added a commit to brettz9/base64-arraybuffer that referenced this pull request Feb 17, 2017
   replacement for PR niklasvh#7 by relying on ES defaults
@dfahlander
Copy link
Author

This PR is replaced by #8.

@dfahlander dfahlander closed this Jul 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants