2 * A Blob implementation.
5 * By Eli Grey, http://eligrey.com
6 * By Devin Samarin, https://github.com/dsamarin
8 * See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md
11 /*global self, unescape */
12 /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
15 /*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
20 view.URL = view.URL || view.webkitURL;
22 if (view.Blob && view.URL) {
29 // Internally we use a BlobBuilder implementation to base Blob off of
30 // in order to support older browsers that only have BlobBuilder
31 var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || (function(view) {
33 get_class = function(object) {
34 return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
36 , FakeBlobBuilder = function BlobBuilder() {
39 , FakeBlob = function Blob(data, type, encoding) {
41 this.size = data.length;
43 this.encoding = encoding;
45 , FBB_proto = FakeBlobBuilder.prototype
46 , FB_proto = FakeBlob.prototype
47 , FileReaderSync = view.FileReaderSync
48 , FileException = function(type) {
49 this.code = this[this.name = type];
52 "NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
53 + "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
55 , file_ex_code = file_ex_codes.length
56 , real_URL = view.URL || view.webkitURL || view
57 , real_create_object_URL = real_URL.createObjectURL
58 , real_revoke_object_URL = real_URL.revokeObjectURL
63 , ArrayBuffer = view.ArrayBuffer
64 , Uint8Array = view.Uint8Array
66 , origin = /^[\w-]+:\/*\[?[\w\.:-]+\]?(?::[0-9]+)?/
68 FakeBlob.fake = FB_proto.fake = true;
69 while (file_ex_code--) {
70 FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
73 if (!real_URL.createObjectURL) {
74 URL = view.URL = function(uri) {
76 uri_info = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
80 if (!("origin" in uri_info)) {
81 if (uri_info.protocol.toLowerCase() === "data:") {
82 uri_info.origin = null;
84 uri_origin = uri.match(origin);
85 uri_info.origin = uri_origin && uri_origin[1];
91 URL.createObjectURL = function(blob) {
97 type = "application/octet-stream";
99 if (blob instanceof FakeBlob) {
100 data_URI_header = "data:" + type;
101 if (blob.encoding === "base64") {
102 return data_URI_header + ";base64," + blob.data;
103 } else if (blob.encoding === "URI") {
104 return data_URI_header + "," + decodeURIComponent(blob.data);
106 return data_URI_header + ";base64," + btoa(blob.data);
108 return data_URI_header + "," + encodeURIComponent(blob.data);
110 } else if (real_create_object_URL) {
111 return real_create_object_URL.call(real_URL, blob);
114 URL.revokeObjectURL = function(object_URL) {
115 if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
116 real_revoke_object_URL.call(real_URL, object_URL);
119 FBB_proto.append = function(data/*, endings*/) {
121 // decode data to a binary string
122 if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
125 , buf = new Uint8Array(data)
127 , buf_len = buf.length
129 for (; i < buf_len; i++) {
130 str += String.fromCharCode(buf[i]);
133 } else if (get_class(data) === "Blob" || get_class(data) === "File") {
134 if (FileReaderSync) {
135 var fr = new FileReaderSync;
136 bb.push(fr.readAsBinaryString(data));
138 // async FileReader won't work as BlobBuilder is sync
139 throw new FileException("NOT_READABLE_ERR");
141 } else if (data instanceof FakeBlob) {
142 if (data.encoding === "base64" && atob) {
143 bb.push(atob(data.data));
144 } else if (data.encoding === "URI") {
145 bb.push(decodeURIComponent(data.data));
146 } else if (data.encoding === "raw") {
150 if (typeof data !== "string") {
151 data += ""; // convert unsupported types to strings
153 // decode UTF-16 to binary string
154 bb.push(unescape(encodeURIComponent(data)));
157 FBB_proto.getBlob = function(type) {
158 if (!arguments.length) {
161 return new FakeBlob(this.data.join(""), type, "raw");
163 FBB_proto.toString = function() {
164 return "[object BlobBuilder]";
166 FB_proto.slice = function(start, end, type) {
167 var args = arguments.length;
172 this.data.slice(start, args > 1 ? end : this.data.length)
177 FB_proto.toString = function() {
178 return "[object Blob]";
180 FB_proto.close = function() {
184 return FakeBlobBuilder;
187 view.Blob = function(blobParts, options) {
188 var type = options ? (options.type || "") : "";
189 var builder = new BlobBuilder();
191 for (var i = 0, len = blobParts.length; i < len; i++) {
192 builder.append(blobParts[i]);
195 return builder.getBlob(type);
197 }(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));