| 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 interface _StringDecoder { | 7 interface _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); |
| 11 | 11 |
| 12 // Returns whether any decoded data is available. | 12 // Returns whether any decoded data is available. |
| 13 bool isEmpty(); | 13 bool isEmpty(); |
| 14 | 14 |
| 15 // Get the number of line breaks present in the current decoded | 15 // Get the number of line breaks present in the current decoded |
| 16 // data. | 16 // data. |
| 17 int lineBreaks(); | 17 int lineBreaks(); |
| 18 | 18 |
| 19 // Get the string data decoded since the last call to [decode] or | 19 // Get the string data decoded since the last call to [decode] or |
| 20 // [decodeLine]. Returns null if no decoded data is available. | 20 // [decodeLine]. Returns null if no decoded data is available. |
| 21 String get decoded(); | 21 String get decoded; |
| 22 | 22 |
| 23 // Get the string data decoded since the last call to [decode] or | 23 // Get the string data decoded since the last call to [decode] or |
| 24 // [decodeLine] up to the next line break present. Returns null if | 24 // [decodeLine] up to the next line break present. Returns null if |
| 25 // no line break is present. The line break character sequence is | 25 // no line break is present. The line break character sequence is |
| 26 // discarded. | 26 // discarded. |
| 27 String get decodedLine(); | 27 String get decodedLine; |
| 28 } | 28 } |
| 29 | 29 |
| 30 | 30 |
| 31 class _StringDecoders { | 31 class _StringDecoders { |
| 32 static _StringDecoder decoder(Encoding encoding) { | 32 static _StringDecoder decoder(Encoding encoding) { |
| 33 if (encoding == Encoding.UTF_8) { | 33 if (encoding == Encoding.UTF_8) { |
| 34 return new _UTF8Decoder(); | 34 return new _UTF8Decoder(); |
| 35 } else if (encoding == Encoding.ISO_8859_1) { | 35 } else if (encoding == Encoding.ISO_8859_1) { |
| 36 return new _Latin1Decoder(); | 36 return new _Latin1Decoder(); |
| 37 } else if (encoding == Encoding.ASCII) { | 37 } else if (encoding == Encoding.ASCII) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 break; | 70 break; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 return buffer.length; | 73 return buffer.length; |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool isEmpty() { | 76 bool isEmpty() { |
| 77 return _result.isEmpty(); | 77 return _result.isEmpty(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 int get lineBreaks() => _lineBreaks; | 80 int get lineBreaks => _lineBreaks; |
| 81 | 81 |
| 82 String get decoded() { | 82 String get decoded { |
| 83 if (isEmpty()) return null; | 83 if (isEmpty()) return null; |
| 84 | 84 |
| 85 String result; | 85 String result; |
| 86 if (_resultOffset == 0) { | 86 if (_resultOffset == 0) { |
| 87 result = new String.fromCharCodes(_result); | 87 result = new String.fromCharCodes(_result); |
| 88 } else { | 88 } else { |
| 89 result = | 89 result = |
| 90 new String.fromCharCodes( | 90 new String.fromCharCodes( |
| 91 _result.getRange(_resultOffset, _result.length - _resultOffset)); | 91 _result.getRange(_resultOffset, _result.length - _resultOffset)); |
| 92 } | 92 } |
| 93 while (!_lineBreakEnds.isEmpty() && _lineBreakEnds.first() < _charOffset) { | 93 while (!_lineBreakEnds.isEmpty() && _lineBreakEnds.first() < _charOffset) { |
| 94 _lineBreakEnds.removeFirst(); | 94 _lineBreakEnds.removeFirst(); |
| 95 _lineBreaks--; | 95 _lineBreaks--; |
| 96 } | 96 } |
| 97 _resetResult(); | 97 _resetResult(); |
| 98 return result; | 98 return result; |
| 99 } | 99 } |
| 100 | 100 |
| 101 String get decodedLine() { | 101 String get decodedLine { |
| 102 if (_lineBreakEnds.isEmpty()) return null; | 102 if (_lineBreakEnds.isEmpty()) return null; |
| 103 int lineEnd = _lineBreakEnds.removeFirst(); | 103 int lineEnd = _lineBreakEnds.removeFirst(); |
| 104 int terminationSequenceLength = 1; | 104 int terminationSequenceLength = 1; |
| 105 if (_result[lineEnd - _charOffset] == LF && | 105 if (_result[lineEnd - _charOffset] == LF && |
| 106 lineEnd > _charOffset && | 106 lineEnd > _charOffset && |
| 107 _result[lineEnd - _charOffset - 1] == CR) { | 107 _result[lineEnd - _charOffset - 1] == CR) { |
| 108 terminationSequenceLength = 2; | 108 terminationSequenceLength = 2; |
| 109 } | 109 } |
| 110 var lineLength = | 110 var lineLength = |
| 111 lineEnd - _charOffset - _resultOffset - terminationSequenceLength + 1; | 111 lineEnd - _charOffset - _resultOffset - terminationSequenceLength + 1; |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 decodedLine = decodedLine.substring(0, decodedLine.length - 1); | 369 decodedLine = decodedLine.substring(0, decodedLine.length - 1); |
| 370 } | 370 } |
| 371 } | 371 } |
| 372 } | 372 } |
| 373 _checkInstallDataHandler(); | 373 _checkInstallDataHandler(); |
| 374 return decodedLine; | 374 return decodedLine; |
| 375 } | 375 } |
| 376 | 376 |
| 377 int available() => _decoder.available(); | 377 int available() => _decoder.available(); |
| 378 | 378 |
| 379 Encoding get encoding() => _encoding; | 379 Encoding get encoding => _encoding; |
| 380 | 380 |
| 381 bool get closed() => _inputClosed && _decoder.isEmpty(); | 381 bool get closed => _inputClosed && _decoder.isEmpty(); |
| 382 | 382 |
| 383 void set onData(void callback()) { | 383 void set onData(void callback()) { |
| 384 _clientDataHandler = callback; | 384 _clientDataHandler = callback; |
| 385 _clientLineHandler = null; | 385 _clientLineHandler = null; |
| 386 _checkInstallDataHandler(); | 386 _checkInstallDataHandler(); |
| 387 _checkScheduleCallback(); | 387 _checkScheduleCallback(); |
| 388 } | 388 } |
| 389 | 389 |
| 390 void set onLine(void callback()) { | 390 void set onLine(void callback()) { |
| 391 _clientLineHandler = callback; | 391 _clientLineHandler = callback; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 bool _inputClosed = false; // Is the underlying input stream closed? | 514 bool _inputClosed = false; // Is the underlying input stream closed? |
| 515 bool _closed = false; // Is this stream closed. | 515 bool _closed = false; // Is this stream closed. |
| 516 bool _eof = false; // Has all data been read from the decoder? | 516 bool _eof = false; // Has all data been read from the decoder? |
| 517 Timer _scheduledDataCallback; | 517 Timer _scheduledDataCallback; |
| 518 Timer _scheduledLineCallback; | 518 Timer _scheduledLineCallback; |
| 519 Timer _scheduledCloseCallback; | 519 Timer _scheduledCloseCallback; |
| 520 Function _clientDataHandler; | 520 Function _clientDataHandler; |
| 521 Function _clientLineHandler; | 521 Function _clientLineHandler; |
| 522 Function _clientCloseHandler; | 522 Function _clientCloseHandler; |
| 523 } | 523 } |
| OLD | NEW |