source: OpenRLabs-Git/web2py/applications/rlabs/static/js/guacamole-common-js/modules/StringReader.js

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20var Guacamole = Guacamole || {};
21
22/**
23 * A reader which automatically handles the given input stream, returning
24 * strictly text data. Note that this object will overwrite any installed event
25 * handlers on the given Guacamole.InputStream.
26 *
27 * @constructor
28 * @param {Guacamole.InputStream} stream The stream that data will be read
29 *                                       from.
30 */
31Guacamole.StringReader = function(stream) {
32
33    /**
34     * Reference to this Guacamole.InputStream.
35     * @private
36     */
37    var guac_reader = this;
38
39    /**
40     * Wrapped Guacamole.ArrayBufferReader.
41     * @private
42     * @type {Guacamole.ArrayBufferReader}
43     */
44    var array_reader = new Guacamole.ArrayBufferReader(stream);
45
46    /**
47     * The number of bytes remaining for the current codepoint.
48     *
49     * @private
50     * @type {Number}
51     */
52    var bytes_remaining = 0;
53
54    /**
55     * The current codepoint value, as calculated from bytes read so far.
56     *
57     * @private
58     * @type {Number}
59     */
60    var codepoint = 0;
61
62    /**
63     * Decodes the given UTF-8 data into a Unicode string. The data may end in
64     * the middle of a multibyte character.
65     *
66     * @private
67     * @param {ArrayBuffer} buffer Arbitrary UTF-8 data.
68     * @return {String} A decoded Unicode string.
69     */
70    function __decode_utf8(buffer) {
71
72        var text = "";
73
74        var bytes = new Uint8Array(buffer);
75        for (var i=0; i<bytes.length; i++) {
76
77            // Get current byte
78            var value = bytes[i];
79
80            // Start new codepoint if nothing yet read
81            if (bytes_remaining === 0) {
82
83                // 1 byte (0xxxxxxx)
84                if ((value | 0x7F) === 0x7F)
85                    text += String.fromCharCode(value);
86
87                // 2 byte (110xxxxx)
88                else if ((value | 0x1F) === 0xDF) {
89                    codepoint = value & 0x1F;
90                    bytes_remaining = 1;
91                }
92
93                // 3 byte (1110xxxx)
94                else if ((value | 0x0F )=== 0xEF) {
95                    codepoint = value & 0x0F;
96                    bytes_remaining = 2;
97                }
98
99                // 4 byte (11110xxx)
100                else if ((value | 0x07) === 0xF7) {
101                    codepoint = value & 0x07;
102                    bytes_remaining = 3;
103                }
104
105                // Invalid byte
106                else
107                    text += "\uFFFD";
108
109            }
110
111            // Continue existing codepoint (10xxxxxx)
112            else if ((value | 0x3F) === 0xBF) {
113
114                codepoint = (codepoint << 6) | (value & 0x3F);
115                bytes_remaining--;
116
117                // Write codepoint if finished
118                if (bytes_remaining === 0)
119                    text += String.fromCharCode(codepoint);
120
121            }
122
123            // Invalid byte
124            else {
125                bytes_remaining = 0;
126                text += "\uFFFD";
127            }
128
129        }
130
131        return text;
132
133    }
134
135    // Receive blobs as strings
136    array_reader.ondata = function(buffer) {
137
138        // Decode UTF-8
139        var text = __decode_utf8(buffer);
140
141        // Call handler, if present
142        if (guac_reader.ontext)
143            guac_reader.ontext(text);
144
145    };
146
147    // Simply call onend when end received
148    array_reader.onend = function() {
149        if (guac_reader.onend)
150            guac_reader.onend();
151    };
152
153    /**
154     * Fired once for every blob of text data received.
155     *
156     * @event
157     * @param {String} text The data packet received.
158     */
159    this.ontext = null;
160
161    /**
162     * Fired once this stream is finished and no further data will be written.
163     * @event
164     */
165    this.onend = null;
166
167};
Note: See TracBrowser for help on using the repository browser.