Chromium Code Reviews| 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 = (e) { | 10 _file.onError = (e) { |
| (...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 _PendingOperation { | |
| 95 const _PendingOperation(); | |
| 96 static final _PendingOperation CLOSE = const _PendingOperation(); | |
| 97 static final _PendingOperation FLUSH = const _PendingOperation(); | |
| 98 } | |
| 99 | |
| 100 | |
| 94 class _FileOutputStream extends _BaseOutputStream implements OutputStream { | 101 class _FileOutputStream extends _BaseOutputStream implements OutputStream { |
| 95 _FileOutputStream(String name, FileMode mode) { | 102 _FileOutputStream(String name, FileMode mode) { |
| 96 _pendingOperations = new List<List<int>>(); | 103 _pendingOperations = new List(); |
| 97 var f = new File(name); | 104 var f = new File(name); |
| 98 f.open(mode, (openedFile) { | 105 f.open(mode, (openedFile) { |
| 99 _file = openedFile; | 106 _file = openedFile; |
| 100 _setupFileHandlers(); | 107 _setupFileHandlers(); |
| 101 _processPendingOperations(); | 108 _processPendingOperations(); |
| 102 }); | 109 }); |
| 103 f.onError = (e) { | 110 f.onError = (e) { |
| 104 if (_onError != null) _onError(e); | 111 if (_onError != null) _onError(e); |
| 105 }; | 112 }; |
| 106 } | 113 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 var length = buffer.length - offset; | 150 var length = buffer.length - offset; |
| 144 if (len != null) { | 151 if (len != null) { |
| 145 if (len > length) throw new IndexOutOfRangeException(len); | 152 if (len > length) throw new IndexOutOfRangeException(len); |
| 146 length = len; | 153 length = len; |
| 147 } | 154 } |
| 148 var copy = new ByteArray(length); | 155 var copy = new ByteArray(length); |
| 149 copy.setRange(0, length, buffer, offset); | 156 copy.setRange(0, length, buffer, offset); |
| 150 return write(copy); | 157 return write(copy); |
| 151 } | 158 } |
| 152 | 159 |
| 160 void flush() { | |
| 161 if (_file == null) { | |
| 162 _pendingOperations.add(_PendingOperation.FLUSH); | |
| 163 } else { | |
| 164 _file.flush(); | |
|
Mads Ager (google)
2012/04/17 11:56:44
Isn't _file a RandomAccessFile? Doesn't flush take
Søren Gjesse
2012/04/17 13:14:19
It does - not sure why no tests failed.
Turend ou
| |
| 165 } | |
| 166 } | |
| 167 | |
| 153 void close() { | 168 void close() { |
| 154 if (_file == null) { | 169 if (_file == null) { |
| 155 _pendingOperations.add(null); | 170 _pendingOperations.add(_PendingOperation.CLOSE); |
| 156 } else if (!_streamMarkedClosed) { | 171 } else if (!_streamMarkedClosed) { |
| 157 _file.close(() { | 172 _file.close(() { |
| 158 if (_onClosed != null) _onClosed(); | 173 if (_onClosed != null) _onClosed(); |
| 159 }); | 174 }); |
| 160 _streamMarkedClosed = true; | 175 _streamMarkedClosed = true; |
| 161 } | 176 } |
| 162 } | 177 } |
| 163 | 178 |
| 164 void set onNoPendingWrites(void callback()) { | 179 void set onNoPendingWrites(void callback()) { |
| 165 _onNoPendingWrites = callback; | 180 _onNoPendingWrites = callback; |
| 166 } | 181 } |
| 167 | 182 |
| 168 void set onClosed(void callback()) { | 183 void set onClosed(void callback()) { |
| 169 _onClosed = callback; | 184 _onClosed = callback; |
| 170 } | 185 } |
| 171 | 186 |
| 172 void set onError(void callback(Exception e)) { | 187 void set onError(void callback(Exception e)) { |
| 173 _onError = callback; | 188 _onError = callback; |
| 174 } | 189 } |
| 175 | 190 |
| 176 void _processPendingOperations() { | 191 void _processPendingOperations() { |
| 177 _pendingOperations.forEach((buffer) { | 192 _pendingOperations.forEach((buffer) { |
| 178 (buffer != null) ? write(buffer) : close(); | 193 if (buffer is _PendingOperation) { |
| 194 if (buffer === _PendingOperation.CLOSE) { | |
| 195 close(); | |
| 196 } else { | |
| 197 assert(buffer === _PendingOperation.FLUSH); | |
| 198 flush(); | |
| 199 } | |
| 200 } else { | |
| 201 write(buffer); | |
| 202 } | |
| 179 }); | 203 }); |
| 180 _pendingOperations = null; | 204 _pendingOperations = null; |
| 181 } | 205 } |
| 182 | 206 |
| 183 void _write(List<int> buffer, int offset, int len) { | 207 void _write(List<int> buffer, int offset, int len) { |
| 184 _file.writeList(buffer, offset, len); | 208 _file.writeList(buffer, offset, len); |
| 185 } | 209 } |
| 186 | 210 |
| 187 RandomAccessFile _file; | 211 RandomAccessFile _file; |
| 188 | 212 |
| 189 // When this is set to true the stream is marked closed. When a | 213 // When this is set to true the stream is marked closed. When a |
| 190 // stream is marked closed no more data can be written. | 214 // stream is marked closed no more data can be written. |
| 191 bool _streamMarkedClosed = false; | 215 bool _streamMarkedClosed = false; |
| 192 | 216 |
| 193 // When this is set to true the close callback has been called and | 217 // When this is set to true the close callback has been called and |
| 194 // the stream is fully closed. | 218 // the stream is fully closed. |
| 195 bool _closeCallbackCalled = false; | 219 bool _closeCallbackCalled = false; |
| 196 | 220 |
| 197 // List of pending writes that were issued before the underlying | 221 // List of pending writes that were issued before the underlying |
| 198 // file was successfully opened. | 222 // file was successfully opened. |
| 199 List<List<int>> _pendingOperations; | 223 List _pendingOperations; |
| 200 | 224 |
| 201 Function _onNoPendingWrites; | 225 Function _onNoPendingWrites; |
| 202 Function _onClosed; | 226 Function _onClosed; |
| 203 Function _onError; | 227 Function _onError; |
| 204 } | 228 } |
| 205 | 229 |
| 206 | 230 |
| 207 // Helper class containing static file helper methods. | 231 // Helper class containing static file helper methods. |
| 208 class _FileUtils { | 232 class _FileUtils { |
| 209 static final kExistsRequest = 0; | 233 static final kExistsRequest = 0; |
| (...skipping 849 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1059 String _name; | 1083 String _name; |
| 1060 int _id; | 1084 int _id; |
| 1061 int _pendingWrites = 0; | 1085 int _pendingWrites = 0; |
| 1062 | 1086 |
| 1063 SendPort _fileService; | 1087 SendPort _fileService; |
| 1064 | 1088 |
| 1065 Timer _noPendingWriteTimer; | 1089 Timer _noPendingWriteTimer; |
| 1066 | 1090 |
| 1067 Function _onNoPendingWrites; | 1091 Function _onNoPendingWrites; |
| 1068 } | 1092 } |
| OLD | NEW |