Debian Codegraph @

closure-library/closure/goog/testing/fs/blob.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Mock blob object.
17 *
18 */
19
20 goog.setTestOnly('goog.testing.fs.Blob');
21 goog.provide('goog.testing.fs.Blob');
22
23 goog.require('goog.crypt');
24 goog.require('goog.crypt.base64');
25
26
27
28 /**
29 * A mock Blob object. The data is stored as an Array of bytes, a "byte" being a
30 * JS number in the range 0-255.
31 *
32 * This blob simplifies writing test code because it has the toString() method
33 * that returns immediately, while the File API only provides asynchronous
34 * reads.
35 * @see https://www.w3.org/TR/FileAPI/#constructorBlob
36 *
37 * @param {(string|Array<(string|number|!Uint8Array)>)=} opt_data The data
38 * encapsulated by the blob.
39 * @param {string=} opt_type The mime type of the blob.
40 * @constructor
41 */
42 goog.testing.fs.Blob = function(opt_data, opt_type) {
43 /**
44 * @see http://www.w3.org/TR/FileAPI/#dfn-type
45 * @type {string}
46 */
47 this.type = opt_type || '';
48
49 /**
50 * The data encapsulated by the blob as an Array of bytes, a "byte" being a
51 * JS number in the range 0-255.
52 * @private {!Array<number>}
53 */
54 this.data_ = [];
55
56 /**
57 * @see http://www.w3.org/TR/FileAPI/#dfn-size
58 * @type {number}
59 */
60 this.size = 0;
61
62 this.setDataInternal(opt_data || '');
63 };
64
65
66 /**
67 * Creates a blob with bytes of a blob ranging from the optional start
68 * parameter up to but not including the optional end parameter, and with a type
69 * attribute that is the value of the optional contentType parameter.
70 * @see http://www.w3.org/TR/FileAPI/#dfn-slice
71 * @param {number=} opt_start The start byte offset.
72 * @param {number=} opt_end The end point of a slice.
73 * @param {string=} opt_contentType The type of the resulting Blob.
74 * @return {!goog.testing.fs.Blob} The result blob of the slice operation.
75 */
76 goog.testing.fs.Blob.prototype.slice = function(
77 opt_start, opt_end, opt_contentType) {
78 var relativeStart;
79 if (goog.isNumber(opt_start)) {
80 relativeStart = (opt_start < 0) ? Math.max(this.size + opt_start, 0) :
81 Math.min(opt_start, this.size);
82 } else {
83 relativeStart = 0;
84 }
85 var relativeEnd;
86 if (goog.isNumber(opt_end)) {
87 relativeEnd = (opt_end < 0) ? Math.max(this.size + opt_end, 0) :
88 Math.min(opt_end, this.size);
89 } else {
90 relativeEnd = this.size;
91 }
92 var span = Math.max(relativeEnd - relativeStart, 0);
93 var blob = new goog.testing.fs.Blob(
94 this.data_.slice(relativeStart, relativeStart + span), opt_contentType);
95 return blob;
96 };
97
98
99 /**
100 * @return {string} The data encapsulated by the blob as an UTF-8 string.
101 * @override
102 */
103 goog.testing.fs.Blob.prototype.toString = function() {
104 return goog.crypt.utf8ByteArrayToString(this.data_);
105 };
106
107
108 /**
109 * @return {!ArrayBuffer} The data encapsulated by the blob as an
110 * ArrayBuffer.
111 */
112 goog.testing.fs.Blob.prototype.toArrayBuffer = function() {
113 var buf = new ArrayBuffer(this.data_.length);
114 var arr = new Uint8Array(buf);
115 for (var i = 0; i < this.data_.length; i++) {
116 arr[i] = this.data_[i];
117 }
118 return buf;
119 };
120
121
122 /**
123 * @return {string} The string data encapsulated by the blob as a data: URI.
124 */
125 goog.testing.fs.Blob.prototype.toDataUrl = function() {
126 return 'data:' + this.type + ';base64,' +
127 goog.crypt.base64.encodeByteArray(this.data_);
128 };
129
130
131 /**
132 * Sets the internal contents of the blob to an Array of bytes. This should
133 * only be called by other functions inside the {@code goog.testing.fs}
134 * namespace.
135 * @param {string|Array<string|number|!Uint8Array>} data The data to write
136 * into the blob.
137 * @package
138 */
139 goog.testing.fs.Blob.prototype.setDataInternal = function(data) {
140 this.data_ = [];
141 if (typeof data === 'string') {
142 this.appendString_(data);
143 } else if (data instanceof Array) {
144 for (var i = 0; i < data.length; i++) {
145 if (typeof data[i] === 'string') {
146 this.appendString_(data[i]);
147 } else if (typeof data[i] === 'number') { // Assume Bytes array.
148 this.appendByte_(data[i]);
149 } else if (data[i] instanceof Uint8Array) {
150 this.appendUint8_(data[i]);
151 }
152 }
153 }
154 this.size = this.data_.length;
155 };
156
157
158 /**
159 * Converts the data from string to Array of bytes and appends to the blob
160 * content.
161 * @param {string} data The string to append to the blob content.
162 * @private
163 */
164 goog.testing.fs.Blob.prototype.appendString_ = function(data) {
165 Array.prototype.push.apply(
166 this.data_, goog.crypt.stringToUtf8ByteArray(data));
167 };
168
169
170 /**
171 * Appends a byte (as a number between 0 to 255) to the blob content.
172 * @param {number} data The byte to append.
173 * @private
174 */
175 goog.testing.fs.Blob.prototype.appendByte_ = function(data) {
176 this.data_.push(data);
177 };
178
179
180 /**
181 * Converts the data from Uint8Array to Array of bytes and appends it to the
182 * blob content.
183 * @param {!Uint8Array} data The array to append to the blob content.
184 * @private
185 */
186 goog.testing.fs.Blob.prototype.appendUint8_ = function(data) {
187 for (var i = 0; i < data.length; i++) {
188 this.data_.push(data[i]);
189 }
190 };