1 /* 2 * Copyright (C) 2013 Glyptodon LLC 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 * THE SOFTWARE. 21 */ 22 23 var Guacamole = Guacamole || {}; 24 25 /** 26 * A reader which automatically handles the given input stream, assembling all 27 * received blobs into a single blob by appending them to each other in order. 28 * Note that this object will overwrite any installed event handlers on the 29 * given Guacamole.InputStream. 30 * 31 * @constructor 32 * @param {Guacamole.InputStream} stream The stream that data will be read 33 * from. 34 * @param {String} mimetype The mimetype of the blob being built. 35 */ 36 Guacamole.BlobReader = function(stream, mimetype) { 37 38 /** 39 * Reference to this Guacamole.InputStream. 40 * @private 41 */ 42 var guac_reader = this; 43 44 /** 45 * The length of this Guacamole.InputStream in bytes. 46 * @private 47 */ 48 var length = 0; 49 50 // Get blob builder 51 var blob_builder; 52 if (window.BlobBuilder) blob_builder = new BlobBuilder(); 53 else if (window.WebKitBlobBuilder) blob_builder = new WebKitBlobBuilder(); 54 else if (window.MozBlobBuilder) blob_builder = new MozBlobBuilder(); 55 else 56 blob_builder = new (function() { 57 58 var blobs = []; 59 60 /** @ignore */ 61 this.append = function(data) { 62 blobs.push(new Blob([data], {"type": mimetype})); 63 }; 64 65 /** @ignore */ 66 this.getBlob = function() { 67 return new Blob(blobs, {"type": mimetype}); 68 }; 69 70 })(); 71 72 // Append received blobs 73 stream.onblob = function(data) { 74 75 // Convert to ArrayBuffer 76 var binary = window.atob(data); 77 var arrayBuffer = new ArrayBuffer(binary.length); 78 var bufferView = new Uint8Array(arrayBuffer); 79 80 for (var i=0; i<binary.length; i++) 81 bufferView[i] = binary.charCodeAt(i); 82 83 blob_builder.append(arrayBuffer); 84 length += arrayBuffer.byteLength; 85 86 // Call handler, if present 87 if (guac_reader.onprogress) 88 guac_reader.onprogress(arrayBuffer.byteLength); 89 90 // Send success response 91 stream.sendAck("OK", 0x0000); 92 93 }; 94 95 // Simply call onend when end received 96 stream.onend = function() { 97 if (guac_reader.onend) 98 guac_reader.onend(); 99 }; 100 101 /** 102 * Returns the current length of this Guacamole.InputStream, in bytes. 103 * @return {Number} The current length of this Guacamole.InputStream. 104 */ 105 this.getLength = function() { 106 return length; 107 }; 108 109 /** 110 * Returns the contents of this Guacamole.BlobReader as a Blob. 111 * @return {Blob} The contents of this Guacamole.BlobReader. 112 */ 113 this.getBlob = function() { 114 return blob_builder.getBlob(); 115 }; 116 117 /** 118 * Fired once for every blob of data received. 119 * 120 * @event 121 * @param {Number} length The number of bytes received. 122 */ 123 this.onprogress = null; 124 125 /** 126 * Fired once this stream is finished and no further data will be written. 127 * @event 128 */ 129 this.onend = null; 130 131 };