1 /*
  2  * Copyright (C) 2015 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, returning
 27  * received blobs as a single data URI built over the course of the stream.
 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
 33  *     The stream that data will be read from.
 34  */
 35 Guacamole.DataURIReader = function(stream, mimetype) {
 36 
 37     /**
 38      * Reference to this Guacamole.DataURIReader.
 39      * @private
 40      */
 41     var guac_reader = this;
 42 
 43     /**
 44      * Current data URI.
 45      *
 46      * @type String
 47      */
 48     var uri = 'data:' + mimetype + ';base64,';
 49 
 50     // Receive blobs as array buffers
 51     stream.onblob = function dataURIReaderBlob(data) {
 52 
 53         // Currently assuming data will ALWAYS be safe to simply append. This
 54         // will not be true if the received base64 data encodes a number of
 55         // bytes that isn't a multiple of three (as base64 expands in a ratio
 56         // of exactly 3:4).
 57         uri += data;
 58 
 59     };
 60 
 61     // Simply call onend when end received
 62     stream.onend = function dataURIReaderEnd() {
 63         if (guac_reader.onend)
 64             guac_reader.onend();
 65     };
 66 
 67     /**
 68      * Returns the data URI of all data received through the underlying stream
 69      * thus far.
 70      *
 71      * @returns {String}
 72      *     The data URI of all data received through the underlying stream thus
 73      *     far.
 74      */
 75     this.getURI = function getURI() {
 76         return uri;
 77     };
 78 
 79     /**
 80      * Fired once this stream is finished and no further data will be written.
 81      *
 82      * @event
 83      */
 84     this.onend = null;
 85 
 86 };