mirror of
https://github.com/webodf/WebODF.git
synced 2026-11-30 16:47:58 +01:00
Previously, byte arrays were implemented as string with values from 0 to 255 inclusive. Some runtimes such as Node and modern browsers have native array implementations. These should be faster. This commit changes changes WebODF to use the native arrays. Since byte arrays differ across runtimes, they are now abstracted by the Runtime.
69 lines
1.7 KiB
JavaScript
69 lines
1.7 KiB
JavaScript
/*global runtime core*/
|
|
/*jslint bitwise: false */
|
|
/**
|
|
* @constructor
|
|
* @param {!string} encoding
|
|
*/
|
|
core.ByteArrayWriter = function ByteArrayWriter(encoding) {
|
|
var self = this,
|
|
data = new runtime.ByteArray(0);
|
|
|
|
/**
|
|
* @param {!core.ByteArrayWriter} writer
|
|
* @return {undefined}
|
|
*/
|
|
this.appendByteArrayWriter = function (writer) {
|
|
data = runtime.concatByteArrays(data, writer.getByteArray());
|
|
};
|
|
/**
|
|
* @param {!Runtime.ByteArray} array
|
|
* @return {undefined}
|
|
*/
|
|
this.appendByteArray = function (array) {
|
|
data = runtime.concatByteArrays(data, array);
|
|
};
|
|
/**
|
|
* @param {!Array.<!number>} array
|
|
* @return {undefined}
|
|
*/
|
|
this.appendArray = function (array) {
|
|
data = runtime.concatByteArrays(data,
|
|
runtime.byteArrayFromArray(array));
|
|
};
|
|
/**
|
|
* @param {!number} value
|
|
* @return {undefined}
|
|
*/
|
|
this.appendUInt16LE = function (value) {
|
|
self.appendArray([value & 0xff, (value >> 8) & 0xff]);
|
|
};
|
|
/**
|
|
* @param {!number} value
|
|
* @return {undefined}
|
|
*/
|
|
this.appendUInt32LE = function (value) {
|
|
self.appendArray([value & 0xff, (value >> 8) & 0xff,
|
|
(value >> 16) & 0xff, (value >> 24) & 0xff]);
|
|
};
|
|
/**
|
|
* @param {!string} string
|
|
* @return {undefined}
|
|
*/
|
|
this.appendString = function (string) {
|
|
data = runtime.concatByteArrays(data,
|
|
runtime.byteArrayFromString(string, encoding));
|
|
};
|
|
/**
|
|
* @return {!number}
|
|
*/
|
|
this.getLength = function () {
|
|
return data.length;
|
|
};
|
|
/**
|
|
* @return {!Runtime.ByteArray}
|
|
*/
|
|
this.getByteArray = function () {
|
|
return data;
|
|
};
|
|
};
|