-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringWriter.js
205 lines (175 loc) · 4.94 KB
/
StringWriter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var Guacamole = Guacamole || {};
/**
* A writer which automatically writes to the given output stream with text
* data.
*
* @constructor
* @param {!Guacamole.OutputStream} stream
* The stream that data will be written to.
*/
Guacamole.StringWriter = function(stream) {
/**
* Reference to this Guacamole.StringWriter.
*
* @private
* @type {!Guacamole.StringWriter}
*/
var guac_writer = this;
/**
* Wrapped Guacamole.ArrayBufferWriter.
*
* @private
* @type {!Guacamole.ArrayBufferWriter}
*/
var array_writer = new Guacamole.ArrayBufferWriter(stream);
/**
* Internal buffer for UTF-8 output.
*
* @private
* @type {!Uint8Array}
*/
var buffer = new Uint8Array(8192);
/**
* The number of bytes currently in the buffer.
*
* @private
* @type {!number}
*/
var length = 0;
// Simply call onack for acknowledgements
array_writer.onack = function(status) {
if (guac_writer.onack)
guac_writer.onack(status);
};
/**
* Expands the size of the underlying buffer by the given number of bytes,
* updating the length appropriately.
*
* @private
* @param {!number} bytes
* The number of bytes to add to the underlying buffer.
*/
function __expand(bytes) {
// Resize buffer if more space needed
if (length+bytes >= buffer.length) {
var new_buffer = new Uint8Array((length+bytes)*2);
new_buffer.set(buffer);
buffer = new_buffer;
}
length += bytes;
}
/**
* Appends a single Unicode character to the current buffer, resizing the
* buffer if necessary. The character will be encoded as UTF-8.
*
* @private
* @param {!number} codepoint
* The codepoint of the Unicode character to append.
*/
function __append_utf8(codepoint) {
var mask;
var bytes;
// 1 byte
if (codepoint <= 0x7F) {
mask = 0x00;
bytes = 1;
}
// 2 byte
else if (codepoint <= 0x7FF) {
mask = 0xC0;
bytes = 2;
}
// 3 byte
else if (codepoint <= 0xFFFF) {
mask = 0xE0;
bytes = 3;
}
// 4 byte
else if (codepoint <= 0x1FFFFF) {
mask = 0xF0;
bytes = 4;
}
// If invalid codepoint, append replacement character
else {
__append_utf8(0xFFFD);
return;
}
// Offset buffer by size
__expand(bytes);
var offset = length - 1;
// Add trailing bytes, if any
for (var i=1; i<bytes; i++) {
buffer[offset--] = 0x80 | (codepoint & 0x3F);
codepoint >>= 6;
}
// Set initial byte
buffer[offset] = mask | codepoint;
}
/**
* Encodes the given string as UTF-8, returning an ArrayBuffer containing
* the resulting bytes.
*
* @private
* @param {!string} text
* The string to encode as UTF-8.
*
* @return {!Uint8Array}
* The encoded UTF-8 data.
*/
function __encode_utf8(text) {
// Fill buffer with UTF-8
for (var i=0; i<text.length; i++) {
var codepoint = text.charCodeAt(i);
__append_utf8(codepoint);
}
// Flush buffer
if (length > 0) {
var out_buffer = buffer.subarray(0, length);
length = 0;
return out_buffer;
}
}
/**
* Sends the given text.
*
* @param {!string} text
* The text to send.
*/
this.sendText = function(text) {
if (text.length)
array_writer.sendData(__encode_utf8(text));
};
/**
* Signals that no further text will be sent, effectively closing the
* stream.
*/
this.sendEnd = function() {
array_writer.sendEnd();
};
/**
* Fired for received data, if acknowledged by the server.
*
* @event
* @param {!Guacamole.Status} status
* The status of the operation.
*/
this.onack = null;
};