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, assembling all 27 * received blobs into a JavaScript object by appending them to each other, in 28 * order, and decoding the result as JSON. Note that this object will overwrite 29 * any installed event handlers on the given Guacamole.InputStream. 30 * 31 * @constructor 32 * @param {Guacamole.InputStream} stream 33 * The stream that JSON will be read from. 34 */ 35 Guacamole.JSONReader = function guacamoleJSONReader(stream) { 36 37 /** 38 * Reference to this Guacamole.JSONReader. 39 * 40 * @private 41 * @type Guacamole.JSONReader 42 */ 43 var guacReader = this; 44 45 /** 46 * Wrapped Guacamole.StringReader. 47 * 48 * @private 49 * @type Guacamole.StringReader 50 */ 51 var stringReader = new Guacamole.StringReader(stream); 52 53 /** 54 * All JSON read thus far. 55 * 56 * @private 57 * @type String 58 */ 59 var json = ''; 60 61 /** 62 * Returns the current length of this Guacamole.JSONReader, in characters. 63 * 64 * @return {Number} 65 * The current length of this Guacamole.JSONReader. 66 */ 67 this.getLength = function getLength() { 68 return json.length; 69 }; 70 71 /** 72 * Returns the contents of this Guacamole.JSONReader as a JavaScript 73 * object. 74 * 75 * @return {Object} 76 * The contents of this Guacamole.JSONReader, as parsed from the JSON 77 * contents of the input stream. 78 */ 79 this.getJSON = function getJSON() { 80 return JSON.parse(json); 81 }; 82 83 // Append all received text 84 stringReader.ontext = function ontext(text) { 85 86 // Append received text 87 json += text; 88 89 // Call handler, if present 90 if (guacReader.onprogress) 91 guacReader.onprogress(text.length); 92 93 }; 94 95 // Simply call onend when end received 96 stringReader.onend = function onend() { 97 if (guacReader.onend) 98 guacReader.onend(); 99 }; 100 101 /** 102 * Fired once for every blob of data received. 103 * 104 * @event 105 * @param {Number} length 106 * The number of characters received. 107 */ 108 this.onprogress = null; 109 110 /** 111 * Fired once this stream is finished and no further data will be written. 112 * 113 * @event 114 */ 115 this.onend = null; 116 117 }; 118