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 26 matching lines...) Expand all Loading... |
37 | 37 |
38 | 38 |
39 class _StringDecoders { | 39 class _StringDecoders { |
40 static _StringDecoder decoder(Encoding encoding) { | 40 static _StringDecoder decoder(Encoding encoding) { |
41 if (encoding == Encoding.UTF_8) { | 41 if (encoding == Encoding.UTF_8) { |
42 return new _UTF8Decoder(); | 42 return new _UTF8Decoder(); |
43 } else if (encoding == Encoding.ISO_8859_1) { | 43 } else if (encoding == Encoding.ISO_8859_1) { |
44 return new _Latin1Decoder(); | 44 return new _Latin1Decoder(); |
45 } else if (encoding == Encoding.ASCII) { | 45 } else if (encoding == Encoding.ASCII) { |
46 return new _AsciiDecoder(); | 46 return new _AsciiDecoder(); |
| 47 } else if (encoding == Encoding.SYSTEM) { |
| 48 if (Platform.operatingSystem == 'windows') { |
| 49 return new _WindowsCodePageDecoder(); |
| 50 } |
| 51 return new _UTF8Decoder(); |
47 } else { | 52 } else { |
48 if (encoding is Encoding) { | 53 if (encoding is Encoding) { |
49 throw new StreamException("Unsupported encoding ${encoding.name}"); | 54 throw new StreamException("Unsupported encoding ${encoding.name}"); |
50 } else { | 55 } else { |
51 throw new StreamException("Unsupported encoding ${encoding}"); | 56 throw new StreamException("Unsupported encoding ${encoding}"); |
52 } | 57 } |
53 } | 58 } |
54 } | 59 } |
55 } | 60 } |
56 | 61 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 bool _processNext() { | 248 bool _processNext() { |
244 while (_bufferList.length > 0) { | 249 while (_bufferList.length > 0) { |
245 int byte = _bufferList.next(); | 250 int byte = _bufferList.next(); |
246 addChar(byte); | 251 addChar(byte); |
247 } | 252 } |
248 return true; | 253 return true; |
249 } | 254 } |
250 } | 255 } |
251 | 256 |
252 | 257 |
| 258 // Utility class for decoding Windows current code page data delivered |
| 259 // as a stream of bytes. |
| 260 class _WindowsCodePageDecoder extends _StringDecoderBase { |
| 261 // Process the next chunk of data. |
| 262 bool _processNext() { |
| 263 List<int> bytes = _bufferList.readBytes(_bufferList.length); |
| 264 for (var charCode in _decodeBytes(bytes).charCodes) { |
| 265 addChar(charCode); |
| 266 } |
| 267 return true; |
| 268 } |
| 269 |
| 270 external static String _decodeBytes(List<int> bytes); |
| 271 } |
| 272 |
| 273 |
253 // Interface for encoders encoding string data into binary data. | 274 // Interface for encoders encoding string data into binary data. |
254 abstract class _StringEncoder { | 275 abstract class _StringEncoder { |
255 List<int> encodeString(String string); | 276 List<int> encodeString(String string); |
256 } | 277 } |
257 | 278 |
258 | 279 |
259 // Utility class for encoding a string into UTF-8 byte stream. | 280 // Utility class for encoding a string into UTF-8 byte stream. |
260 class _UTF8Encoder implements _StringEncoder { | 281 class _UTF8Encoder implements _StringEncoder { |
261 List<int> encodeString(String string) { | 282 List<int> encodeString(String string) { |
262 int size = _encodingSize(string); | 283 int size = _encodingSize(string); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 throw new EncoderException( | 327 throw new EncoderException( |
307 "No ASCII encoding for code point $charCode"); | 328 "No ASCII encoding for code point $charCode"); |
308 } | 329 } |
309 result[i] = charCode; | 330 result[i] = charCode; |
310 } | 331 } |
311 return result; | 332 return result; |
312 } | 333 } |
313 } | 334 } |
314 | 335 |
315 | 336 |
| 337 // Utility class for encoding a string into a current windows |
| 338 // code page byte list. |
| 339 class _WindowsCodePageEncoder implements _StringEncoder { |
| 340 List<int> encodeString(String string) { |
| 341 return _encodeString(string); |
| 342 } |
| 343 |
| 344 external static List<int> _encodeString(String string); |
| 345 } |
| 346 |
| 347 |
316 class _StringEncoders { | 348 class _StringEncoders { |
317 static _StringEncoder encoder(Encoding encoding) { | 349 static _StringEncoder encoder(Encoding encoding) { |
318 if (encoding == Encoding.UTF_8) { | 350 if (encoding == Encoding.UTF_8) { |
319 return new _UTF8Encoder(); | 351 return new _UTF8Encoder(); |
320 } else if (encoding == Encoding.ISO_8859_1) { | 352 } else if (encoding == Encoding.ISO_8859_1) { |
321 return new _Latin1Encoder(); | 353 return new _Latin1Encoder(); |
322 } else if (encoding == Encoding.ASCII) { | 354 } else if (encoding == Encoding.ASCII) { |
323 return new _AsciiEncoder(); | 355 return new _AsciiEncoder(); |
| 356 } else if (encoding == Encoding.SYSTEM) { |
| 357 if (Platform.operatingSystem == 'windows') { |
| 358 return new _WindowsCodePageEncoder(); |
| 359 } |
| 360 return new _UTF8Encoder(); |
324 } else { | 361 } else { |
325 throw new StreamException("Unsupported encoding ${encoding.name}"); | 362 throw new StreamException("Unsupported encoding ${encoding.name}"); |
326 } | 363 } |
327 } | 364 } |
328 } | 365 } |
329 | 366 |
330 | 367 |
331 class EncoderException implements Exception { | 368 class EncoderException implements Exception { |
332 const EncoderException([String this.message]); | 369 const EncoderException([String this.message]); |
333 String toString() => "EncoderException: $message"; | 370 String toString() => "EncoderException: $message"; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 bool _inputClosed = false; // Is the underlying input stream closed? | 545 bool _inputClosed = false; // Is the underlying input stream closed? |
509 bool _closed = false; // Is this stream closed. | 546 bool _closed = false; // Is this stream closed. |
510 bool _eof = false; // Has all data been read from the decoder? | 547 bool _eof = false; // Has all data been read from the decoder? |
511 Timer _scheduledDataCallback; | 548 Timer _scheduledDataCallback; |
512 Timer _scheduledLineCallback; | 549 Timer _scheduledLineCallback; |
513 Timer _scheduledCloseCallback; | 550 Timer _scheduledCloseCallback; |
514 Function _clientDataHandler; | 551 Function _clientDataHandler; |
515 Function _clientLineHandler; | 552 Function _clientLineHandler; |
516 Function _clientCloseHandler; | 553 Function _clientCloseHandler; |
517 } | 554 } |
OLD | NEW |