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 writer which automatically writes to the given output stream with text
 27  * data.
 28  * 
 29  * @constructor
 30  * @param {Guacamole.OutputStream} stream The stream that data will be written
 31  *                                        to.
 32  */
 33 Guacamole.StringWriter = function(stream) {
 34 
 35     /**
 36      * Reference to this Guacamole.StringWriter.
 37      * @private
 38      */
 39     var guac_writer = this;
 40 
 41     /**
 42      * Wrapped Guacamole.ArrayBufferWriter.
 43      * @private
 44      * @type Guacamole.ArrayBufferWriter
 45      */
 46     var array_writer = new Guacamole.ArrayBufferWriter(stream);
 47 
 48     /**
 49      * Internal buffer for UTF-8 output.
 50      * @private
 51      */
 52     var buffer = new Uint8Array(8192);
 53 
 54     /**
 55      * The number of bytes currently in the buffer.
 56      * @private
 57      */
 58     var length = 0;
 59 
 60     // Simply call onack for acknowledgements
 61     array_writer.onack = function(status) {
 62         if (guac_writer.onack)
 63             guac_writer.onack(status);
 64     };
 65 
 66     /**
 67      * Expands the size of the underlying buffer by the given number of bytes,
 68      * updating the length appropriately.
 69      * 
 70      * @private
 71      * @param {Number} bytes The number of bytes to add to the underlying
 72      *                       buffer.
 73      */
 74     function __expand(bytes) {
 75 
 76         // Resize buffer if more space needed
 77         if (length+bytes >= buffer.length) {
 78             var new_buffer = new Uint8Array((length+bytes)*2);
 79             new_buffer.set(buffer);
 80             buffer = new_buffer;
 81         }
 82 
 83         length += bytes;
 84 
 85     }
 86 
 87     /**
 88      * Appends a single Unicode character to the current buffer, resizing the
 89      * buffer if necessary. The character will be encoded as UTF-8.
 90      * 
 91      * @private
 92      * @param {Number} codepoint The codepoint of the Unicode character to
 93      *                           append.
 94      */
 95     function __append_utf8(codepoint) {
 96 
 97         var mask;
 98         var bytes;
 99 
100         // 1 byte
101         if (codepoint <= 0x7F) {
102             mask = 0x00;
103             bytes = 1;
104         }
105 
106         // 2 byte
107         else if (codepoint <= 0x7FF) {
108             mask = 0xC0;
109             bytes = 2;
110         }
111 
112         // 3 byte
113         else if (codepoint <= 0xFFFF) {
114             mask = 0xE0;
115             bytes = 3;
116         }
117 
118         // 4 byte
119         else if (codepoint <= 0x1FFFFF) {
120             mask = 0xF0;
121             bytes = 4;
122         }
123 
124         // If invalid codepoint, append replacement character
125         else {
126             __append_utf8(0xFFFD);
127             return;
128         }
129 
130         // Offset buffer by size
131         __expand(bytes);
132         var offset = length - 1;
133 
134         // Add trailing bytes, if any
135         for (var i=1; i<bytes; i++) {
136             buffer[offset--] = 0x80 | (codepoint & 0x3F);
137             codepoint >>= 6;
138         }
139 
140         // Set initial byte
141         buffer[offset] = mask | codepoint;
142 
143     }
144 
145     /**
146      * Encodes the given string as UTF-8, returning an ArrayBuffer containing
147      * the resulting bytes.
148      * 
149      * @private
150      * @param {String} text The string to encode as UTF-8.
151      * @return {Uint8Array} The encoded UTF-8 data.
152      */
153     function __encode_utf8(text) {
154 
155         // Fill buffer with UTF-8
156         for (var i=0; i<text.length; i++) {
157             var codepoint = text.charCodeAt(i);
158             __append_utf8(codepoint);
159         }
160 
161         // Flush buffer
162         if (length > 0) {
163             var out_buffer = buffer.subarray(0, length);
164             length = 0;
165             return out_buffer;
166         }
167 
168     }
169 
170     /**
171      * Sends the given text.
172      * 
173      * @param {String} text The text to send.
174      */
175     this.sendText = function(text) {
176         array_writer.sendData(__encode_utf8(text));
177     };
178 
179     /**
180      * Signals that no further text will be sent, effectively closing the
181      * stream.
182      */
183     this.sendEnd = function() {
184         array_writer.sendEnd();
185     };
186 
187     /**
188      * Fired for received data, if acknowledged by the server.
189      * @event
190      * @param {Guacamole.Status} status The status of the operation.
191      */
192     this.onack = null;
193 
194 };