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

Side by Side Diff: sdk/lib/io/file_impl.dart

Issue 15337005: Ensure to close file-stream on all errors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 months 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 | no next file » | 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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 part of dart.io; 5 part of dart.io;
6 6
7 // Read the file in blocks of size 64k. 7 // Read the file in blocks of size 64k.
8 const int _BLOCK_SIZE = 64 * 1024; 8 const int _BLOCK_SIZE = 64 * 1024;
9 9
10 10
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 } 69 }
70 70
71 void _readBlock() { 71 void _readBlock() {
72 // Don't start a new read if one is already in progress. 72 // Don't start a new read if one is already in progress.
73 if (_readInProgress) return; 73 if (_readInProgress) return;
74 _readInProgress = true; 74 _readInProgress = true;
75 int readBytes = _BLOCK_SIZE; 75 int readBytes = _BLOCK_SIZE;
76 if (_end != null) { 76 if (_end != null) {
77 readBytes = min(readBytes, _end - _position); 77 readBytes = min(readBytes, _end - _position);
78 if (readBytes < 0) { 78 if (readBytes < 0) {
79 throw new RangeError("Bad end position: $_end"); 79 if (!_unsubscribed) {
80 _controller.addError(new RangeError("Bad end position: $_end"));
81 _closeFile().then((_) { _controller.close(); });
kustermann 2013/05/21 11:23:57 I don't know if the Future returned by '_closeFile
Anders Johnsen 2013/05/21 11:37:27 I'm quite sure it can't. Can't come to think of an
82 _unsubscribed = true;
83 }
84 return;
80 } 85 }
81 } 86 }
82 _openedFile.read(readBytes) 87 _openedFile.read(readBytes)
83 .then((block) { 88 .then((block) {
84 _readInProgress = false; 89 _readInProgress = false;
85 if (block.length == 0) { 90 if (block.length == 0) {
86 if (!_unsubscribed) { 91 if (!_unsubscribed) {
87 _closeFile().then((_) { _controller.close(); }); 92 _closeFile().then((_) { _controller.close(); });
88 _unsubscribed = true; 93 _unsubscribed = true;
89 } 94 }
(...skipping 10 matching lines...) Expand all
100 .catchError((e) { 105 .catchError((e) {
101 if (!_unsubscribed) { 106 if (!_unsubscribed) {
102 _controller.addError(e); 107 _controller.addError(e);
103 _closeFile().then((_) { _controller.close(); }); 108 _closeFile().then((_) { _controller.close(); });
104 _unsubscribed = true; 109 _unsubscribed = true;
105 } 110 }
106 }); 111 });
107 } 112 }
108 113
109 void _start() { 114 void _start() {
115 if (_position == null) {
116 _position = 0;
117 } else if (_position < 0) {
118 _controller.addError(new RangeError("Bad start position: $_position"));
119 _controller.close();
120 return;
121 }
110 Future<RandomAccessFile> openFuture; 122 Future<RandomAccessFile> openFuture;
111 if (_path != null) { 123 if (_path != null) {
112 openFuture = new File(_path).open(mode: FileMode.READ); 124 openFuture = new File(_path).open(mode: FileMode.READ);
113 } else { 125 } else {
114 openFuture = new Future.value(_File._openStdioSync(0)); 126 openFuture = new Future.value(_File._openStdioSync(0));
115 } 127 }
116 openFuture 128 openFuture
117 .then((RandomAccessFile opened) { 129 .then((RandomAccessFile opened) {
118 _openedFile = opened; 130 _openedFile = opened;
119 if (_position == null) {
120 _position = 0;
121 }
122 if (_position > 0) { 131 if (_position > 0) {
123 return opened.setPosition(_position); 132 return opened.setPosition(_position);
124 } else if (_position < 0) {
125 throw new RangeError("Bad start position: $_position");
126 } 133 }
127 }) 134 })
128 .then((_) => _readBlock()) 135 .then((_) => _readBlock())
129 .catchError((e) { 136 .catchError((e) {
130 _controller.addError(e); 137 _controller.addError(e);
131 _controller.close(); 138 _controller.close();
132 }); 139 });
133 } 140 }
134 141
135 void _resume() { 142 void _resume() {
(...skipping 891 matching lines...) Expand 10 before | Expand all | Expand 10 after
1027 new FileIOException("File closed '$_path'")); 1034 new FileIOException("File closed '$_path'"));
1028 }); 1035 });
1029 return completer.future; 1036 return completer.future;
1030 } 1037 }
1031 1038
1032 final String _path; 1039 final String _path;
1033 int _id; 1040 int _id;
1034 1041
1035 SendPort _fileService; 1042 SendPort _fileService;
1036 } 1043 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698