OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Interface for decoders decoding binary data into string data. The | 5 // Interface for decoders decoding binary data into string data. The |
6 // decoder keeps track of line breaks during decoding. | 6 // decoder keeps track of line breaks during decoding. |
7 abstract class _StringDecoder { | 7 abstract class _StringDecoder { |
8 // Add more binary data to be decoded. The ownership of the buffer | 8 // Add more binary data to be decoded. The ownership of the buffer |
9 // is transfered to the decoder and the caller most not modify it any more. | 9 // is transfered to the decoder and the caller most not modify it any more. |
10 int write(List<int> buffer); | 10 int write(List<int> buffer); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 List<int> encodeString(String string) { | 261 List<int> encodeString(String string) { |
262 int size = _encodingSize(string); | 262 int size = _encodingSize(string); |
263 List<int> result = new Uint8List(size); | 263 List<int> result = new Uint8List(size); |
264 _encodeString(string, result); | 264 _encodeString(string, result); |
265 return result; | 265 return result; |
266 } | 266 } |
267 | 267 |
268 static int _encodingSize(String string) => _encodeString(string, null); | 268 static int _encodingSize(String string) => _encodeString(string, null); |
269 | 269 |
270 static int _encodeString(String string, List<int> buffer) { | 270 static int _encodeString(String string, List<int> buffer) { |
271 int pos = 0; | 271 List<int> utf8CodeUnits = encodeUtf8(string); |
272 int length = string.length; | 272 if (buffer != null) { |
273 for (int i = 0; i < length; i++) { | 273 for (int i = 0; i < utf8CodeUnits.length; i++) { |
274 int additionalBytes; | 274 buffer[i] = utf8CodeUnits[i]; |
275 int charCode = string.charCodeAt(i); | |
276 if (charCode <= 0x007F) { | |
277 additionalBytes = 0; | |
278 if (buffer != null) buffer[pos] = charCode; | |
279 } else if (charCode <= 0x07FF) { | |
280 // 110xxxxx (xxxxx is top 5 bits). | |
281 if (buffer != null) buffer[pos] = ((charCode >> 6) & 0x1F) | 0xC0; | |
282 additionalBytes = 1; | |
283 } else if (charCode <= 0xFFFF) { | |
284 // 1110xxxx (xxxx is top 4 bits) | |
285 if (buffer != null) buffer[pos] = ((charCode >> 12) & 0x0F)| 0xE0; | |
286 additionalBytes = 2; | |
287 } else { | |
288 // 11110xxx (xxx is top 3 bits) | |
289 if (buffer != null) buffer[pos] = ((charCode >> 18) & 0x07) | 0xF0; | |
290 additionalBytes = 3; | |
291 } | |
292 pos++; | |
293 if (buffer != null) { | |
294 for (int i = additionalBytes; i > 0; i--) { | |
295 // 10xxxxxx (xxxxxx is next 6 bits from the top). | |
296 buffer[pos++] = ((charCode >> (6 * (i - 1))) & 0x3F) | 0x80; | |
297 } | |
298 } else { | |
299 pos += additionalBytes; | |
300 } | 275 } |
301 } | 276 } |
302 return pos; | 277 return utf8CodeUnits.length; |
303 } | 278 } |
304 } | 279 } |
305 | 280 |
306 | 281 |
307 // Utility class for encoding a string into a Latin1 byte stream. | 282 // Utility class for encoding a string into a Latin1 byte stream. |
308 class _Latin1Encoder implements _StringEncoder { | 283 class _Latin1Encoder implements _StringEncoder { |
309 List<int> encodeString(String string) { | 284 List<int> encodeString(String string) { |
310 List<int> result = new Uint8List(string.length); | 285 List<int> result = new Uint8List(string.length); |
311 for (int i = 0; i < string.length; i++) { | 286 for (int i = 0; i < string.length; i++) { |
312 int charCode = string.charCodeAt(i); | 287 int charCode = string.charCodeAt(i); |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 bool _inputClosed = false; // Is the underlying input stream closed? | 508 bool _inputClosed = false; // Is the underlying input stream closed? |
534 bool _closed = false; // Is this stream closed. | 509 bool _closed = false; // Is this stream closed. |
535 bool _eof = false; // Has all data been read from the decoder? | 510 bool _eof = false; // Has all data been read from the decoder? |
536 Timer _scheduledDataCallback; | 511 Timer _scheduledDataCallback; |
537 Timer _scheduledLineCallback; | 512 Timer _scheduledLineCallback; |
538 Timer _scheduledCloseCallback; | 513 Timer _scheduledCloseCallback; |
539 Function _clientDataHandler; | 514 Function _clientDataHandler; |
540 Function _clientLineHandler; | 515 Function _clientLineHandler; |
541 Function _clientCloseHandler; | 516 Function _clientCloseHandler; |
542 } | 517 } |
OLD | NEW |