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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 class DecoderException implements Exception { | 55 class DecoderException implements Exception { |
56 const DecoderException([String this.message]); | 56 const DecoderException([String this.message]); |
57 String toString() => "DecoderException: $message"; | 57 String toString() => "DecoderException: $message"; |
58 final String message; | 58 final String message; |
59 } | 59 } |
60 | 60 |
61 | 61 |
62 // Utility class for decoding UTF-8 from data delivered as a stream of | 62 // Utility class for decoding UTF-8 from data delivered as a stream of |
63 // bytes. | 63 // bytes. |
64 class _StringDecoderBase implements _StringDecoder { | 64 abstract class _StringDecoderBase implements _StringDecoder { |
65 _StringDecoderBase() | 65 _StringDecoderBase() |
66 : _bufferList = new _BufferList(), | 66 : _bufferList = new _BufferList(), |
67 _result = new List<int>(), | 67 _result = new List<int>(), |
68 _lineBreakEnds = new Queue<int>(); | 68 _lineBreakEnds = new Queue<int>(); |
69 | 69 |
70 int write(List<int> buffer) { | 70 int write(List<int> buffer) { |
71 _bufferList.add(buffer); | 71 _bufferList.add(buffer); |
72 // Decode as many bytes into characters as possible. | 72 // Decode as many bytes into characters as possible. |
73 while (_bufferList.length > 0) { | 73 while (_bufferList.length > 0) { |
74 if (!_processNext()) { | 74 if (!_processNext()) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 _lineBreakEnds.add(charPos); | 147 _lineBreakEnds.add(charPos); |
148 _lineBreaks++; | 148 _lineBreaks++; |
149 } | 149 } |
150 | 150 |
151 void _resetResult() { | 151 void _resetResult() { |
152 _charOffset += _result.length; | 152 _charOffset += _result.length; |
153 _result = new List<int>(); | 153 _result = new List<int>(); |
154 _resultOffset = 0; | 154 _resultOffset = 0; |
155 } | 155 } |
156 | 156 |
157 abstract bool _processNext(); | 157 bool _processNext(); |
158 | 158 |
159 _BufferList _bufferList; | 159 _BufferList _bufferList; |
160 int _resultOffset = 0; | 160 int _resultOffset = 0; |
161 List<int> _result; | 161 List<int> _result; |
162 int _lineBreaks = 0; // Number of line breaks in the current list. | 162 int _lineBreaks = 0; // Number of line breaks in the current list. |
163 // The positions of the line breaks are tracked in terms of absolute | 163 // The positions of the line breaks are tracked in terms of absolute |
164 // character positions from the begining of the decoded data. | 164 // character positions from the begining of the decoded data. |
165 Queue<int> _lineBreakEnds; // Character position of known line breaks. | 165 Queue<int> _lineBreakEnds; // Character position of known line breaks. |
166 int _charOffset = 0; // Character number of the first character in the list. | 166 int _charOffset = 0; // Character number of the first character in the list. |
167 int _charCount = 0; // Total number of characters decoded. | 167 int _charCount = 0; // Total number of characters decoded. |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 bool _inputClosed = false; // Is the underlying input stream closed? | 523 bool _inputClosed = false; // Is the underlying input stream closed? |
524 bool _closed = false; // Is this stream closed. | 524 bool _closed = false; // Is this stream closed. |
525 bool _eof = false; // Has all data been read from the decoder? | 525 bool _eof = false; // Has all data been read from the decoder? |
526 Timer _scheduledDataCallback; | 526 Timer _scheduledDataCallback; |
527 Timer _scheduledLineCallback; | 527 Timer _scheduledLineCallback; |
528 Timer _scheduledCloseCallback; | 528 Timer _scheduledCloseCallback; |
529 Function _clientDataHandler; | 529 Function _clientDataHandler; |
530 Function _clientLineHandler; | 530 Function _clientLineHandler; |
531 Function _clientCloseHandler; | 531 Function _clientCloseHandler; |
532 } | 532 } |
OLD | NEW |