1 /**
  2  * Copyright (C) 2012 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global runtime, core, Uint8Array, ArrayBuffer*/
 26 /*jslint bitwise: true */
 27 
 28 /**
 29  * @constructor
 30  * @param {!string} encoding
 31  */
 32 core.ByteArrayWriter = function ByteArrayWriter(encoding) {
 33     "use strict";
 34     var self = this,
 35         /**@type{!number}*/
 36         length = 0,
 37         /**@type{!number}*/
 38         bufferSize = 1024,
 39         /**@type{!Uint8Array}*/
 40         data = new Uint8Array(new ArrayBuffer(bufferSize));
 41 
 42     /**
 43      * @param {!number} extraLength
 44      * @return {undefined}
 45      */
 46     function expand(extraLength) {
 47         var newData;
 48         if (extraLength > bufferSize - length) {
 49             bufferSize = Math.max(2 * bufferSize, length + extraLength);
 50             newData = new Uint8Array(new ArrayBuffer(bufferSize));
 51             newData.set(data);
 52             data = newData;
 53         }
 54     }
 55     /**
 56      * @param {!core.ByteArrayWriter} writer
 57      * @return {undefined}
 58      */
 59     this.appendByteArrayWriter = function (writer) {
 60         self.appendByteArray(writer.getByteArray());
 61     };
 62     /**
 63      * @param {!Uint8Array} array
 64      * @return {undefined}
 65      */
 66     this.appendByteArray = function (array) {
 67         var l = array.length;
 68         expand(l);
 69         data.set(array, length);
 70         length += l;
 71     };
 72     /**
 73      * @param {!Array.<!number>} array
 74      * @return {undefined}
 75      */
 76     this.appendArray = function (array) {
 77         var l = array.length;
 78         expand(l);
 79         data.set(array, length);
 80         length += l;
 81     };
 82     /**
 83      * @param {!number} value
 84      * @return {undefined}
 85      */
 86     this.appendUInt16LE = function (value) {
 87         self.appendArray([value & 0xff, (value >> 8) & 0xff]);
 88     };
 89     /**
 90      * @param {!number} value
 91      * @return {undefined}
 92      */
 93     this.appendUInt32LE = function (value) {
 94         self.appendArray([value & 0xff, (value >> 8) & 0xff,
 95                 (value >> 16) & 0xff, (value >> 24) & 0xff]);
 96     };
 97     /**
 98      * @param {!string} string
 99      * @return {undefined}
100      */
101     this.appendString = function (string) {
102         self.appendByteArray(runtime.byteArrayFromString(string, encoding));
103     };
104     /**
105      * @return {!number}
106      */
107     this.getLength = function () {
108         return length;
109     };
110     /**
111      * @return {!Uint8Array}
112      */
113     this.getByteArray = function () {
114         var a = new Uint8Array(new ArrayBuffer(length));
115         a.set(data.subarray(0, length));
116         return a;
117     };
118 };
119