Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: lib/io/string_stream.dart

Issue 11345027: Do error propagation for decoder errors in string stream. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/standalone/io/string_stream_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 12 matching lines...) Expand all
23 // call to [decode] or [decodeLine]. Returns null if no decoded data 23 // call to [decode] or [decodeLine]. Returns null if no decoded data
24 // is available. If [len] is not specified all decoded characters 24 // is available. If [len] is not specified all decoded characters
25 // are returned. 25 // are returned.
26 String decoded([int len]); 26 String decoded([int len]);
27 27
28 // Get the string data decoded since the last call to [decode] or 28 // Get the string data decoded since the last call to [decode] or
29 // [decodeLine] up to the next line break present. Returns null if 29 // [decodeLine] up to the next line break present. Returns null if
30 // no line break is present. The line break character sequence is 30 // no line break is present. The line break character sequence is
31 // discarded. 31 // discarded.
32 String get decodedLine; 32 String get decodedLine;
33
34 // Set the handler that will be called if an error ocurs while decoding.
35 void set onError(Function callback);
33 } 36 }
34 37
35 38
36 class _StringDecoders { 39 class _StringDecoders {
37 static _StringDecoder decoder(Encoding encoding) { 40 static _StringDecoder decoder(Encoding encoding) {
38 if (encoding == Encoding.UTF_8) { 41 if (encoding == Encoding.UTF_8) {
39 return new _UTF8Decoder(); 42 return new _UTF8Decoder();
40 } else if (encoding == Encoding.ISO_8859_1) { 43 } else if (encoding == Encoding.ISO_8859_1) {
41 return new _Latin1Decoder(); 44 return new _Latin1Decoder();
42 } else if (encoding == Encoding.ASCII) { 45 } else if (encoding == Encoding.ASCII) {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 _BufferList _bufferList; 162 _BufferList _bufferList;
160 int _resultOffset = 0; 163 int _resultOffset = 0;
161 List<int> _result; 164 List<int> _result;
162 int _lineBreaks = 0; // Number of line breaks in the current list. 165 int _lineBreaks = 0; // Number of line breaks in the current list.
163 // The positions of the line breaks are tracked in terms of absolute 166 // The positions of the line breaks are tracked in terms of absolute
164 // character positions from the begining of the decoded data. 167 // character positions from the begining of the decoded data.
165 Queue<int> _lineBreakEnds; // Character position of known line breaks. 168 Queue<int> _lineBreakEnds; // Character position of known line breaks.
166 int _charOffset = 0; // Character number of the first character in the list. 169 int _charOffset = 0; // Character number of the first character in the list.
167 int _charCount = 0; // Total number of characters decoded. 170 int _charCount = 0; // Total number of characters decoded.
168 int _lastCharCode = -1; 171 int _lastCharCode = -1;
172 Function onError;
169 173
170 final int LF = 10; 174 final int LF = 10;
171 final int CR = 13; 175 final int CR = 13;
172 } 176 }
173 177
174 178
175 // Utility class for decoding UTF-8 from data delivered as a stream of 179 // Utility class for decoding UTF-8 from data delivered as a stream of
176 // bytes. 180 // bytes.
177 class _UTF8Decoder extends _StringDecoderBase { 181 class _UTF8Decoder extends _StringDecoderBase {
178 // Process the next UTF-8 encoded character. 182 // Process the next UTF-8 encoded character.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 218
215 219
216 // Utility class for decoding ascii data delivered as a stream of 220 // Utility class for decoding ascii data delivered as a stream of
217 // bytes. 221 // bytes.
218 class _AsciiDecoder extends _StringDecoderBase { 222 class _AsciiDecoder extends _StringDecoderBase {
219 // Process the next ascii encoded character. 223 // Process the next ascii encoded character.
220 bool _processNext() { 224 bool _processNext() {
221 while (_bufferList.length > 0) { 225 while (_bufferList.length > 0) {
222 int byte = _bufferList.next(); 226 int byte = _bufferList.next();
223 if (byte > 127) { 227 if (byte > 127) {
224 throw new DecoderException("Illegal ASCII character $byte"); 228 var error = new DecoderException("Illegal ASCII character $byte");
229 if (onError != null) {
230 onError(error);
231 return false;
232 } else {
233 throw error;
234 }
225 } 235 }
226 addChar(byte); 236 addChar(byte);
227 } 237 }
228 return true; 238 return true;
229 } 239 }
230 } 240 }
231 241
232 242
233 // Utility class for decoding Latin-1 data delivered as a stream of 243 // Utility class for decoding Latin-1 data delivered as a stream of
234 // bytes. 244 // bytes.
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 _checkInstallDataHandler(); 412 _checkInstallDataHandler();
403 _checkScheduleCallback(); 413 _checkScheduleCallback();
404 } 414 }
405 415
406 void set onClosed(void callback()) { 416 void set onClosed(void callback()) {
407 _clientCloseHandler = callback; 417 _clientCloseHandler = callback;
408 } 418 }
409 419
410 void set onError(void callback(e)) { 420 void set onError(void callback(e)) {
411 _input.onError = callback; 421 _input.onError = callback;
422 _decoder.onError = (e) {
423 _clientCloseHandler = null;
424 _input.close();
425 callback(e);
426 };
412 } 427 }
413 428
414 void _onData() { 429 void _onData() {
415 _readData(); 430 _readData();
416 if (!_decoder.isEmpty && _clientDataHandler !== null) { 431 if (!_decoder.isEmpty && _clientDataHandler !== null) {
417 _clientDataHandler(); 432 _clientDataHandler();
418 } 433 }
419 if (_decoder.lineBreaks > 0 && _clientLineHandler !== null) { 434 if (_decoder.lineBreaks > 0 && _clientLineHandler !== null) {
420 _clientLineHandler(); 435 _clientLineHandler();
421 } 436 }
422 _checkScheduleCallback(); 437 _checkScheduleCallback();
423 _checkInstallDataHandler(); 438 _checkInstallDataHandler();
424 } 439 }
425 440
426 void _onClosed() { 441 void _onClosed() {
427 _inputClosed = true; 442 _inputClosed = true;
428 if (_decoder.isEmpty && _clientCloseHandler != null) { 443 if (_decoder.isEmpty && _clientCloseHandler != null) {
429 _clientCloseHandler(); 444 _clientCloseHandler();
430 _closed = true;
431 } else { 445 } else {
432 _checkScheduleCallback(); 446 _checkScheduleCallback();
433 } 447 }
434 } 448 }
435 449
436 void _readData() { 450 void _readData() {
437 List<int> data = _input.read(); 451 List<int> data = _input.read();
438 if (data !== null) { 452 if (data !== null) {
439 _decoder.write(data); 453 _decoder.write(data);
440 } 454 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 bool _inputClosed = false; // Is the underlying input stream closed? 537 bool _inputClosed = false; // Is the underlying input stream closed?
524 bool _closed = false; // Is this stream closed. 538 bool _closed = false; // Is this stream closed.
525 bool _eof = false; // Has all data been read from the decoder? 539 bool _eof = false; // Has all data been read from the decoder?
526 Timer _scheduledDataCallback; 540 Timer _scheduledDataCallback;
527 Timer _scheduledLineCallback; 541 Timer _scheduledLineCallback;
528 Timer _scheduledCloseCallback; 542 Timer _scheduledCloseCallback;
529 Function _clientDataHandler; 543 Function _clientDataHandler;
530 Function _clientLineHandler; 544 Function _clientLineHandler;
531 Function _clientCloseHandler; 545 Function _clientCloseHandler;
532 } 546 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/io/string_stream_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698