| 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); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 _charCount++; | 104 _charCount++; |
| 105 // Check for line ends (\r, \n and \r\n). | 105 // Check for line ends (\r, \n and \r\n). |
| 106 if (charCode == LF) { | 106 if (charCode == LF) { |
| 107 _recordLineBreakEnd(_charCount - 1); | 107 _recordLineBreakEnd(_charCount - 1); |
| 108 } else if (_lastCharCode == CR) { | 108 } else if (_lastCharCode == CR) { |
| 109 _recordLineBreakEnd(_charCount - 2); | 109 _recordLineBreakEnd(_charCount - 2); |
| 110 } | 110 } |
| 111 _lastCharCode = charCode; | 111 _lastCharCode = charCode; |
| 112 } | 112 } |
| 113 | 113 |
| 114 int available() => _result.length - _resultOffset; |
| 115 |
| 114 void _recordLineBreakEnd(int charPos) { | 116 void _recordLineBreakEnd(int charPos) { |
| 115 _lineBreakEnds.add(charPos); | 117 _lineBreakEnds.add(charPos); |
| 116 _lineBreaks++; | 118 _lineBreaks++; |
| 117 } | 119 } |
| 118 | 120 |
| 119 void _resetResult() { | 121 void _resetResult() { |
| 120 _charOffset += _result.length; | 122 _charOffset += _result.length; |
| 121 _result = new List<int>(); | 123 _result = new List<int>(); |
| 122 _resultOffset = 0; | 124 _resultOffset = 0; |
| 123 } | 125 } |
| 124 | 126 |
| 125 abstract bool _processNext(); | 127 abstract bool _processNext(); |
| 126 | 128 |
| 127 _BufferList _bufferList; | 129 _BufferList _bufferList; |
| 128 int _resultOffset = 0; | 130 int _resultOffset = 0; |
| 129 List<int> _result; | 131 List<int> _result; |
| 130 int _lineBreaks = 0; // Number of line breaks in the current list. | 132 int _lineBreaks = 0; // Number of line breaks in the current list. |
| 131 // The positions of the line breaks are tracked in terms of absolute | 133 // The positions of the line breaks are tracked in terms of absolute |
| 132 // character positions from the begining of the decoded data. | 134 // character positions from the begining of the decoded data. |
| 133 Queue<int> _lineBreakEnds; // Character position of known line breaks. | 135 Queue<int> _lineBreakEnds; // Character position of known line breaks. |
| 134 int _charOffset = 0; // Character number of the first character in the list. | 136 int _charOffset = 0; // Character number of the first character in the list. |
| 135 int _charCount = 0; // Total number of characters decodes. | 137 int _charCount = 0; // Total number of characters decoded. |
| 136 int _lastCharCode = -1; | 138 int _lastCharCode = -1; |
| 137 | 139 |
| 138 final int LF = 10; | 140 final int LF = 10; |
| 139 final int CR = 13; | 141 final int CR = 13; |
| 140 } | 142 } |
| 141 | 143 |
| 142 | 144 |
| 143 // Utility class for decoding ascii data delivered as a stream of | 145 // Utility class for decoding ascii data delivered as a stream of |
| 144 // bytes. | 146 // bytes. |
| 145 class _AsciiDecoder extends _StringDecoderBase { | 147 class _AsciiDecoder extends _StringDecoderBase { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 if (decodedLine != null && | 248 if (decodedLine != null && |
| 247 decodedLine[decodedLine.length - 1] == '\r') { | 249 decodedLine[decodedLine.length - 1] == '\r') { |
| 248 decodedLine = decodedLine.substring(0, decodedLine.length - 1); | 250 decodedLine = decodedLine.substring(0, decodedLine.length - 1); |
| 249 } | 251 } |
| 250 } | 252 } |
| 251 } | 253 } |
| 252 _checkInstallDataHandler(); | 254 _checkInstallDataHandler(); |
| 253 return decodedLine; | 255 return decodedLine; |
| 254 } | 256 } |
| 255 | 257 |
| 258 int available() => _decoder.available(); |
| 259 |
| 256 String get encoding() => _encoding; | 260 String get encoding() => _encoding; |
| 257 | 261 |
| 258 bool get closed() => _inputClosed && _decoder.isEmpty(); | 262 bool get closed() => _inputClosed && _decoder.isEmpty(); |
| 259 | 263 |
| 260 void set onData(void callback()) { | 264 void set onData(void callback()) { |
| 261 _clientDataHandler = callback; | 265 _clientDataHandler = callback; |
| 262 _clientLineHandler = null; | 266 _clientLineHandler = null; |
| 263 _checkInstallDataHandler(); | 267 _checkInstallDataHandler(); |
| 264 _checkScheduleCallback(); | 268 _checkScheduleCallback(); |
| 265 } | 269 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 bool _inputClosed = false; // Is the underlying input stream closed? | 395 bool _inputClosed = false; // Is the underlying input stream closed? |
| 392 bool _closed = false; // Is this stream closed. | 396 bool _closed = false; // Is this stream closed. |
| 393 bool _eof = false; // Has all data been read from the decoder? | 397 bool _eof = false; // Has all data been read from the decoder? |
| 394 Timer _scheduledDataCallback; | 398 Timer _scheduledDataCallback; |
| 395 Timer _scheduledLineCallback; | 399 Timer _scheduledLineCallback; |
| 396 Timer _scheduledCloseCallback; | 400 Timer _scheduledCloseCallback; |
| 397 Function _clientDataHandler; | 401 Function _clientDataHandler; |
| 398 Function _clientLineHandler; | 402 Function _clientLineHandler; |
| 399 Function _clientCloseHandler; | 403 Function _clientCloseHandler; |
| 400 } | 404 } |
| OLD | NEW |