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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 return buffer.length; | 81 return buffer.length; |
82 } | 82 } |
83 | 83 |
84 bool get isEmpty => _result.isEmpty; | 84 bool get isEmpty => _result.isEmpty; |
85 | 85 |
86 int get lineBreaks => _lineBreaks; | 86 int get lineBreaks => _lineBreaks; |
87 | 87 |
88 String decoded([int len]) { | 88 String decoded([int len]) { |
89 if (isEmpty) return null; | 89 if (isEmpty) return null; |
90 | 90 |
91 String result; | 91 List<int> result; |
92 if (len !== null && len < available()) { | 92 if (len !== null && len < available()) { |
93 result = new String.fromCharCodes(_result.getRange(_resultOffset, len)); | 93 result = _result.getRange(_resultOffset, len); |
94 } else { | 94 } else { |
Mads Ager (google)
2012/11/07 06:54:40
Let's change this to
if (len !== null && len < av
Bob Nystrom
2012/11/07 17:28:05
Done.
| |
95 if (_resultOffset == 0) { | 95 if (_resultOffset == 0) { |
96 result = new String.fromCharCodes(_result); | 96 result = _result; |
97 } else { | 97 } else { |
98 result = | 98 result = _result.getRange(_resultOffset, |
99 new String.fromCharCodes( | 99 _result.length - _resultOffset); |
100 _result.getRange(_resultOffset, | |
101 _result.length - _resultOffset)); | |
102 } | 100 } |
103 } | 101 } |
102 | |
104 _resultOffset += result.length; | 103 _resultOffset += result.length; |
105 while (!_lineBreakEnds.isEmpty && | 104 while (!_lineBreakEnds.isEmpty && |
106 _lineBreakEnds.first < _charOffset + _resultOffset) { | 105 _lineBreakEnds.first < _charOffset + _resultOffset) { |
107 _lineBreakEnds.removeFirst(); | 106 _lineBreakEnds.removeFirst(); |
108 _lineBreaks--; | 107 _lineBreaks--; |
109 } | 108 } |
110 if (_result.length == _resultOffset) _resetResult(); | 109 if (_result.length == _resultOffset) _resetResult(); |
111 return result; | 110 return new String.fromCharCodes(result); |
112 } | 111 } |
113 | 112 |
114 String get decodedLine { | 113 String get decodedLine { |
115 if (_lineBreakEnds.isEmpty) return null; | 114 if (_lineBreakEnds.isEmpty) return null; |
116 int lineEnd = _lineBreakEnds.removeFirst(); | 115 int lineEnd = _lineBreakEnds.removeFirst(); |
117 int terminationSequenceLength = 1; | 116 int terminationSequenceLength = 1; |
118 if (_result[lineEnd - _charOffset] == LF && | 117 if (_result[lineEnd - _charOffset] == LF && |
119 lineEnd > _charOffset && | 118 lineEnd > _charOffset && |
120 _resultOffset < lineEnd && | 119 _resultOffset < lineEnd && |
121 _result[lineEnd - _charOffset - 1] == CR) { | 120 _result[lineEnd - _charOffset - 1] == CR) { |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
412 _checkInstallDataHandler(); | 411 _checkInstallDataHandler(); |
413 _checkScheduleCallback(); | 412 _checkScheduleCallback(); |
414 } | 413 } |
415 | 414 |
416 void set onClosed(void callback()) { | 415 void set onClosed(void callback()) { |
417 _clientCloseHandler = callback; | 416 _clientCloseHandler = callback; |
418 } | 417 } |
419 | 418 |
420 void set onError(void callback(e)) { | 419 void set onError(void callback(e)) { |
421 _input.onError = callback; | 420 _input.onError = callback; |
422 _decoder.onError = (e) { | 421 _decoder.onError = (e) { |
423 _clientCloseHandler = null; | 422 _clientCloseHandler = null; |
424 _input.close(); | 423 _input.close(); |
425 callback(e); | 424 callback(e); |
426 }; | 425 }; |
427 } | 426 } |
428 | 427 |
429 void _onData() { | 428 void _onData() { |
430 _readData(); | 429 _readData(); |
431 if (!_decoder.isEmpty && _clientDataHandler !== null) { | 430 if (!_decoder.isEmpty && _clientDataHandler !== null) { |
432 _clientDataHandler(); | 431 _clientDataHandler(); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
537 bool _inputClosed = false; // Is the underlying input stream closed? | 536 bool _inputClosed = false; // Is the underlying input stream closed? |
538 bool _closed = false; // Is this stream closed. | 537 bool _closed = false; // Is this stream closed. |
539 bool _eof = false; // Has all data been read from the decoder? | 538 bool _eof = false; // Has all data been read from the decoder? |
540 Timer _scheduledDataCallback; | 539 Timer _scheduledDataCallback; |
541 Timer _scheduledLineCallback; | 540 Timer _scheduledLineCallback; |
542 Timer _scheduledCloseCallback; | 541 Timer _scheduledCloseCallback; |
543 Function _clientDataHandler; | 542 Function _clientDataHandler; |
544 Function _clientLineHandler; | 543 Function _clientLineHandler; |
545 Function _clientCloseHandler; | 544 Function _clientCloseHandler; |
546 } | 545 } |
OLD | NEW |