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 class _FileInputStream extends _BaseDataInputStream implements InputStream { | 5 class _FileInputStream extends _BaseDataInputStream implements InputStream { |
6 _FileInputStream(String name) { | 6 _FileInputStream(String name) { |
7 _file = new File(name); | 7 _file = new File(name); |
8 _data = []; | 8 _data = []; |
9 _position = 0; | 9 _position = 0; |
10 _file.onError = (String s) { | 10 _file.onError = (String s) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 _closed = true; | 84 _closed = true; |
85 } | 85 } |
86 | 86 |
87 File _file; | 87 File _file; |
88 List<int> _data; | 88 List<int> _data; |
89 int _position; | 89 int _position; |
90 bool _closed = false; | 90 bool _closed = false; |
91 } | 91 } |
92 | 92 |
93 | 93 |
94 class _FileOutputStream implements OutputStream { | 94 class _FileOutputStream extends _BaseOutputStream implements OutputStream { |
95 _FileOutputStream(String name, FileMode mode) { | 95 _FileOutputStream(String name, FileMode mode) { |
96 _pendingOperations = new List<List<int>>(); | 96 _pendingOperations = new List<List<int>>(); |
97 var f = new File(name); | 97 var f = new File(name); |
98 f.open(mode, (openedFile) { | 98 f.open(mode, (openedFile) { |
99 _file = openedFile; | 99 _file = openedFile; |
100 _setupFileHandlers(); | 100 _setupFileHandlers(); |
101 _processPendingOperations(); | 101 _processPendingOperations(); |
102 }); | 102 }); |
103 f.onError = (e) { | 103 f.onError = (e) { |
104 if (_onError != null) _onError(); | 104 if (_onError != null) _onError(); |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 var length = opened.lengthSync(); | 542 var length = opened.lengthSync(); |
543 var result = new ByteArray(length); | 543 var result = new ByteArray(length); |
544 var read = opened.readListSync(result, 0, length); | 544 var read = opened.readListSync(result, 0, length); |
545 if (read != length) { | 545 if (read != length) { |
546 throw new FileIOException("Failed reading file as bytes: $_name"); | 546 throw new FileIOException("Failed reading file as bytes: $_name"); |
547 } | 547 } |
548 opened.closeSync(); | 548 opened.closeSync(); |
549 return result; | 549 return result; |
550 } | 550 } |
551 | 551 |
552 _StringDecoder _getDecoder(encoding) { | 552 void readAsText(Encoding encoding, void callback(String text)) { |
553 if (encoding == "UTF-8") { | |
554 return new _UTF8Decoder(); | |
555 } else if (encoding == "ISO-8859-1") { | |
556 return new _Latin1Decoder(); | |
557 } else if (encoding == "ASCII") { | |
558 return new _AsciiDecoder(); | |
559 } | |
560 throw new FileIOException("Unsupported encoding $_encoding"); | |
561 } | |
562 | |
563 void readAsText(String encoding, void callback(String text)) { | |
564 _asyncUsed = true; | 553 _asyncUsed = true; |
565 var decoder = _getDecoder(encoding); | 554 var decoder = _StringDecoders.decoder(encoding); |
566 readAsBytes((bytes) { | 555 readAsBytes((bytes) { |
567 try { | 556 try { |
568 decoder.write(bytes); | 557 decoder.write(bytes); |
569 } catch (var e) { | 558 } catch (var e) { |
570 if (_onError != null) { | 559 if (_onError != null) { |
571 _onError(e.toString()); | 560 _onError(e.toString()); |
572 return; | 561 return; |
573 } | 562 } |
574 } | 563 } |
575 callback(decoder.decoded); | 564 callback(decoder.decoded); |
576 }); | 565 }); |
577 } | 566 } |
578 | 567 |
579 String readAsTextSync([String encoding = 'UTF-8']) { | 568 String readAsTextSync([Encoding encoding = Encoding.UTF_8]) { |
580 if (_asyncUsed) { | 569 if (_asyncUsed) { |
581 throw new FileIOException( | 570 throw new FileIOException( |
582 "Mixed use of synchronous and asynchronous API"); | 571 "Mixed use of synchronous and asynchronous API"); |
583 } | 572 } |
584 var decoder = _getDecoder(encoding); | 573 var decoder = _StringDecoders.decoder(encoding); |
585 List<int> bytes = readAsBytesSync(); | 574 List<int> bytes = readAsBytesSync(); |
586 decoder.write(bytes); | 575 decoder.write(bytes); |
587 return decoder.decoded; | 576 return decoder.decoded; |
588 } | 577 } |
589 | 578 |
590 List<String> _getDecodedLines(_StringDecoder decoder) { | 579 List<String> _getDecodedLines(_StringDecoder decoder) { |
591 List<String> result = []; | 580 List<String> result = []; |
592 var line = decoder.decodedLine; | 581 var line = decoder.decodedLine; |
593 while (line != null) { | 582 while (line != null) { |
594 result.add(line); | 583 result.add(line); |
595 line = decoder.decodedLine; | 584 line = decoder.decodedLine; |
596 } | 585 } |
597 // If there is more data with no terminating line break we treat | 586 // If there is more data with no terminating line break we treat |
598 // it as the last line. | 587 // it as the last line. |
599 var data = decoder.decoded; | 588 var data = decoder.decoded; |
600 if (data != null) { | 589 if (data != null) { |
601 result.add(data); | 590 result.add(data); |
602 } | 591 } |
603 return result; | 592 return result; |
604 } | 593 } |
605 | 594 |
606 void readAsLines(String encoding, void callback(List<String> lines)) { | 595 void readAsLines(Encoding encoding, void callback(List<String> lines)) { |
607 _asyncUsed = true; | 596 _asyncUsed = true; |
608 var decoder = _getDecoder(encoding); | 597 var decoder = _StringDecoders.decoder(encoding); |
609 readAsBytes((bytes) { | 598 readAsBytes((bytes) { |
610 try { | 599 try { |
611 decoder.write(bytes); | 600 decoder.write(bytes); |
612 } catch (var e) { | 601 } catch (var e) { |
613 if (_onError != null) { | 602 if (_onError != null) { |
614 _onError(e.toString()); | 603 _onError(e.toString()); |
615 return; | 604 return; |
616 } | 605 } |
617 } | 606 } |
618 callback(_getDecodedLines(decoder)); | 607 callback(_getDecodedLines(decoder)); |
619 }); | 608 }); |
620 } | 609 } |
621 | 610 |
622 List<String> readAsLinesSync([String encoding = "UTF-8"]) { | 611 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { |
623 if (_asyncUsed) { | 612 if (_asyncUsed) { |
624 throw new FileIOException( | 613 throw new FileIOException( |
625 "Mixed use of synchronous and asynchronous API"); | 614 "Mixed use of synchronous and asynchronous API"); |
626 } | 615 } |
627 var decoder = _getDecoder(encoding); | 616 var decoder = _StringDecoders.decoder(encoding); |
628 List<int> bytes = readAsBytesSync(); | 617 List<int> bytes = readAsBytesSync(); |
629 decoder.write(bytes); | 618 decoder.write(bytes); |
630 return _getDecodedLines(decoder); | 619 return _getDecodedLines(decoder); |
631 } | 620 } |
632 | 621 |
633 String get name() => _name; | 622 String get name() => _name; |
634 | 623 |
635 void set onError(void handler(String error)) { | 624 void set onError(void handler(String error)) { |
636 _onError = handler; | 625 _onError = handler; |
637 } | 626 } |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 if (index != 0) { | 831 if (index != 0) { |
843 throw new IndexOutOfRangeException(index); | 832 throw new IndexOutOfRangeException(index); |
844 } | 833 } |
845 int result = _FileUtils.writeList(_id, buffer, offset, bytes); | 834 int result = _FileUtils.writeList(_id, buffer, offset, bytes); |
846 if (result == -1) { | 835 if (result == -1) { |
847 throw new FileIOException("writeList failed"); | 836 throw new FileIOException("writeList failed"); |
848 } | 837 } |
849 return result; | 838 return result; |
850 } | 839 } |
851 | 840 |
852 void writeString(String string) { | 841 void writeString(String string, [Encoding encoding = Encoding.UTF_8]) { |
853 _ensureFileService(); | 842 _ensureFileService(); |
854 _asyncUsed = true; | 843 _asyncUsed = true; |
855 List request = new List(3); | 844 List request = new List(3); |
856 request[0] = _FileUtils.kWriteStringRequest; | 845 request[0] = _FileUtils.kWriteStringRequest; |
857 request[1] = _id; | 846 request[1] = _id; |
858 request[2] = string; | 847 request[2] = string; |
859 _writeEnqueued(); | 848 _writeEnqueued(); |
860 _fileService.call(request).receive((result, replyTo) { | 849 _fileService.call(request).receive((result, replyTo) { |
861 _writeCompleted(); | 850 _writeCompleted(); |
862 if (result == -1 && _onError !== null) { | 851 if (result == -1 && _onError !== null) { |
863 _onError("writeString failed"); | 852 _onError("writeString failed"); |
864 } | 853 } |
865 }); | 854 }); |
866 } | 855 } |
867 | 856 |
868 int writeStringSync(String string) { | 857 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { |
869 if (_asyncUsed) { | 858 if (_asyncUsed) { |
870 throw new FileIOException( | 859 throw new FileIOException( |
871 "Mixed use of synchronous and asynchronous API"); | 860 "Mixed use of synchronous and asynchronous API"); |
872 } | 861 } |
873 int result = _FileUtils.checkedWriteString(_id, string); | 862 int result = _FileUtils.checkedWriteString(_id, string); |
874 if (result == -1) { | 863 if (result == -1) { |
875 throw new FileIOException("writeString failed"); | 864 throw new FileIOException("writeString failed"); |
876 } | 865 } |
877 return result; | 866 return result; |
878 } | 867 } |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 bool _asyncUsed; | 1043 bool _asyncUsed; |
1055 int _pendingWrites = 0; | 1044 int _pendingWrites = 0; |
1056 | 1045 |
1057 SendPort _fileService; | 1046 SendPort _fileService; |
1058 | 1047 |
1059 Timer _noPendingWriteTimer; | 1048 Timer _noPendingWriteTimer; |
1060 | 1049 |
1061 Function _onNoPendingWrites; | 1050 Function _onNoPendingWrites; |
1062 Function _onError; | 1051 Function _onError; |
1063 } | 1052 } |
OLD | NEW |