OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Utility class which can deliver bytes one by one from a number of | 5 // Utility class which can deliver bytes one by one from a number of |
6 // buffers added. | 6 // buffers added. |
7 class _BufferList { | 7 class _BufferList { |
8 _BufferList() : _index = 0, _length = 0, _buffers = new Queue(); | 8 _BufferList() : _index = 0, _length = 0, _buffers = new Queue(); |
9 | 9 |
10 void add(List<int> buffer) { | 10 void add(List<int> buffer) { |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 _bufferList.next(); | 164 _bufferList.next(); |
165 } | 165 } |
166 _result.addCharCode(value); | 166 _result.addCharCode(value); |
167 return true; | 167 return true; |
168 } | 168 } |
169 } | 169 } |
170 | 170 |
171 | 171 |
172 class _StringInputStream implements StringInputStream { | 172 class _StringInputStream implements StringInputStream { |
173 _StringInputStream(InputStream this._input, [String this._encoding]) { | 173 _StringInputStream(InputStream this._input, [String this._encoding]) { |
174 if (_encoding == null) { | 174 if (_encoding === null) { |
175 _encoding = "UTF-8"; | 175 _encoding = "UTF-8"; |
176 } | 176 } |
177 if (_encoding == "UTF-8") { | 177 if (_encoding == "UTF-8") { |
178 _decoder = new _UTF8Decoder(); | 178 _decoder = new _UTF8Decoder(); |
179 } else if (_encoding == "ISO-8859-1") { | 179 } else if (_encoding == "ISO-8859-1") { |
180 _decoder = new _Latin1Decoder(); | 180 _decoder = new _Latin1Decoder(); |
181 } else if (_encoding == "ASCII") { | 181 } else if (_encoding == "ASCII") { |
182 _decoder = new _AsciiDecoder(); | 182 _decoder = new _AsciiDecoder(); |
183 } else { | 183 } else { |
184 throw new StreamException("Unsupported encoding $_encoding"); | 184 throw new StreamException("Unsupported encoding $_encoding"); |
(...skipping 21 matching lines...) Expand all Loading... |
206 } | 206 } |
207 | 207 |
208 String readLine() { | 208 String readLine() { |
209 // Get line from the buffer if possible. | 209 // Get line from the buffer if possible. |
210 if (_buffer != null) { | 210 if (_buffer != null) { |
211 var result = _readLineFromBuffer(); | 211 var result = _readLineFromBuffer(); |
212 if (result != null) return result; | 212 if (result != null) return result; |
213 } | 213 } |
214 // Try to fill more data into the buffer and read a line. | 214 // Try to fill more data into the buffer and read a line. |
215 if (_fillBuffer()) { | 215 if (_fillBuffer()) { |
216 if (_eof && _buffer == null) return null; | 216 if (_eof && _buffer === null) return null; |
217 return _readLineFromBuffer(); | 217 return _readLineFromBuffer(); |
218 } | 218 } |
219 return null; | 219 return null; |
220 } | 220 } |
221 | 221 |
222 String get encoding() => _encoding; | 222 String get encoding() => _encoding; |
223 | 223 |
224 bool get closed() => _closed; | 224 bool get closed() => _closed; |
225 | 225 |
226 void set dataHandler(void callback()) { | 226 void set dataHandler(void callback()) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 // Fill decoded data into the buffer. Returns true if more data was | 292 // Fill decoded data into the buffer. Returns true if more data was |
293 // added or end of file was reached. | 293 // added or end of file was reached. |
294 bool _fillBuffer() { | 294 bool _fillBuffer() { |
295 if (_eof) return false; | 295 if (_eof) return false; |
296 if (_buffer != null && _bufferLineStart == _buffer.length) { | 296 if (_buffer != null && _bufferLineStart == _buffer.length) { |
297 _buffer = null; | 297 _buffer = null; |
298 _bufferLineStart = null; | 298 _bufferLineStart = null; |
299 } | 299 } |
300 _readData(); | 300 _readData(); |
301 var decodedString = _decoder.decoded; | 301 var decodedString = _decoder.decoded; |
302 if (decodedString == null && _closed) { | 302 if (decodedString === null && _closed) { |
303 _eof = true; | 303 _eof = true; |
304 return true; | 304 return true; |
305 } | 305 } |
306 if (_buffer == null) { | 306 if (_buffer === null) { |
307 _buffer = decodedString; | 307 _buffer = decodedString; |
308 if (_buffer != null) { | 308 if (_buffer != null) { |
309 _bufferLineStart = 0; | 309 _bufferLineStart = 0; |
310 return true; | 310 return true; |
311 } | 311 } |
312 } else if (decodedString != null) { | 312 } else if (decodedString != null) { |
313 _buffer = _buffer.substring(_bufferLineStart) + decodedString; | 313 _buffer = _buffer.substring(_bufferLineStart) + decodedString; |
314 _bufferLineStart = 0; | 314 _bufferLineStart = 0; |
315 return true; | 315 return true; |
316 } | 316 } |
317 return false; | 317 return false; |
318 } | 318 } |
319 | 319 |
320 InputStream _input; | 320 InputStream _input; |
321 String _encoding; | 321 String _encoding; |
322 _Decoder _decoder; | 322 _Decoder _decoder; |
323 String _buffer; // String can be buffered here if readLine is used. | 323 String _buffer; // String can be buffered here if readLine is used. |
324 int _bufferLineStart; // Current offset into _buffer if any. | 324 int _bufferLineStart; // Current offset into _buffer if any. |
325 bool _closed = false; | 325 bool _closed = false; |
326 bool _eof = false; // Has all data been read from the decoder? | 326 bool _eof = false; // Has all data been read from the decoder? |
327 var _clientDataHandler; | 327 var _clientDataHandler; |
328 var _clientCloseHandler; | 328 var _clientCloseHandler; |
329 } | 329 } |
OLD | NEW |