| 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 class _FileInputStream implements InputStream { | 5 class _FileInputStream implements FileInputStream { |
| 6 _FileInputStream(File file) { | 6 _FileInputStream(File file) { |
| 7 _file = file; | 7 _file = new File(file.name); |
| 8 _file.openSync(); |
| 8 } | 9 } |
| 9 | 10 |
| 10 List<int> read([int len]) { | 11 List<int> read([int len]) { |
| 11 int bytesToRead = available(); | 12 int bytesToRead = available(); |
| 12 if (bytesToRead == 0) { | 13 if (bytesToRead == 0) { |
| 13 return null; | 14 return null; |
| 14 } | 15 } |
| 15 if (len != null) { | 16 if (len != null) { |
| 16 if (len <= 0) { | 17 if (len <= 0) { |
| 17 throw new StreamException("Illegal length $len"); | 18 throw new StreamException("Illegal length $len"); |
| 18 } else if (bytesToRead > len) { | 19 } else if (bytesToRead > len) { |
| 19 bytesToRead = len; | 20 bytesToRead = len; |
| 20 } | 21 } |
| 21 } | 22 } |
| 22 List<int> buffer = new List<int>(bytesToRead); | 23 List<int> buffer = new List<int>(bytesToRead); |
| 23 int bytesRead = _file.readList(buffer, 0, bytesToRead); | 24 int bytesRead = _file.readListSync(buffer, 0, bytesToRead); |
| 24 if (bytesRead < bytesToRead) { | 25 if (bytesRead < bytesToRead) { |
| 25 List<int> newBuffer = new List<int>(bytesRead); | 26 List<int> newBuffer = new List<int>(bytesRead); |
| 26 newBuffer.copyFrom(buffer, 0, 0, bytesRead); | 27 newBuffer.copyFrom(buffer, 0, 0, bytesRead); |
| 27 return newBuffer; | 28 return newBuffer; |
| 28 } else { | 29 } else { |
| 29 return buffer; | 30 return buffer; |
| 30 } | 31 } |
| 31 } | 32 } |
| 32 | 33 |
| 33 int readInto(List<int> buffer, int offset, int len) { | 34 int readInto(List<int> buffer, int offset, int len) { |
| 34 if (offset == null) offset = 0; | 35 if (offset == null) offset = 0; |
| 35 if (len == null) len = buffer.length; | 36 if (len == null) len = buffer.length; |
| 36 if (offset < 0) throw new StreamException("Illegal offset $offset"); | 37 if (offset < 0) throw new StreamException("Illegal offset $offset"); |
| 37 if (len < 0) throw new StreamException("Illegal length $len"); | 38 if (len < 0) throw new StreamException("Illegal length $len"); |
| 38 return _file.readList(buffer, offset, len); | 39 return _file.readListSync(buffer, offset, len); |
| 39 } | 40 } |
| 40 | 41 |
| 41 int available() { | 42 int available() { |
| 42 return _file.length - _file.position; | 43 return _file.lengthSync() - _file.positionSync(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 bool closed() { | 46 bool closed() { |
| 46 _file.position == _file.length; | 47 _file.positionSync() == _file.lengthSync(); |
| 48 } |
| 49 |
| 50 void close() { |
| 51 _file.closeSync(); |
| 47 } | 52 } |
| 48 | 53 |
| 49 void set dataHandler(void callback()) { | 54 void set dataHandler(void callback()) { |
| 50 // TODO(sgjesse): How to handle this? | 55 // TODO(sgjesse): How to handle this? |
| 51 } | 56 } |
| 52 | 57 |
| 53 void set closeHandler(void callback()) { | 58 void set closeHandler(void callback()) { |
| 54 // TODO(sgjesse): How to handle this? | 59 // TODO(sgjesse): How to handle this? |
| 55 } | 60 } |
| 56 | 61 |
| 57 void set errorHandler(void callback()) { | 62 void set errorHandler(void callback()) { |
| 58 // TODO(sgjesse): How to handle this? | 63 // TODO(sgjesse): How to handle this? |
| 59 } | 64 } |
| 60 | 65 |
| 61 File _file; | 66 File _file; |
| 62 } | 67 } |
| 63 | 68 |
| 64 | 69 |
| 65 class _FileOutputStream implements OutputStream { | 70 class _FileOutputStream implements FileOutputStream { |
| 66 _FileOutputStream(File file) { | 71 _FileOutputStream(File file) { |
| 67 _file = file; | 72 _file = new File(file.name); |
| 73 _file.openSync(true); |
| 68 } | 74 } |
| 69 | 75 |
| 70 bool write(List<int> buffer, int offset, int len, void callback()) { | 76 bool write(List<int> buffer, int offset, int len, void callback()) { |
| 71 int bytesWritten = _file.writeList(buffer, offset, len); | 77 int bytesWritten = _file.writeListSync(buffer, offset, len); |
| 72 | 78 |
| 73 if (bytesWritten == len) { | 79 if (bytesWritten == len) { |
| 74 return true; | 80 return true; |
| 75 } else { | 81 } else { |
| 76 throw "FileOutputStream: write error"; | 82 throw "FileOutputStream: write error"; |
| 77 } | 83 } |
| 78 } | 84 } |
| 79 | 85 |
| 86 void close() { |
| 87 _file.closeSync(); |
| 88 } |
| 89 |
| 80 File _file; | 90 File _file; |
| 81 } | 91 } |
| 82 | 92 |
| 83 | 93 |
| 84 // Class for encapsulating the native implementation of files. | 94 // Class for encapsulating the native implementation of files. |
| 85 class _File extends FileNativeWrapper implements File { | 95 class _File implements File { |
| 86 // Constructor for file. | 96 // Constructor for file. |
| 87 factory _File(String name, bool writable) { | 97 _File(String this._name); |
| 88 _File file = new _File._internal(); | 98 |
| 89 if (!file._openFile(name, writable)) { | 99 void exists() { |
| 90 return null; | 100 throw "Unimplemented"; |
| 101 } |
| 102 |
| 103 bool existsSync() { |
| 104 return _fileExists(_name); |
| 105 } |
| 106 |
| 107 bool _fileExists(String name) native "File_Exists"; |
| 108 |
| 109 void create() { |
| 110 throw "Unimplemented"; |
| 111 } |
| 112 |
| 113 void createSync() { |
| 114 throw "Unimplemented"; |
| 115 } |
| 116 |
| 117 void open([bool writable = false]) { |
| 118 throw "Unimplemented"; |
| 119 } |
| 120 |
| 121 void openSync([bool writable = false]) { |
| 122 if (!_openFile(_name, writable)) { |
| 123 throw new FileIOException("Cannot open file: $_name"); |
| 91 } | 124 } |
| 92 file._writeable = writable; | |
| 93 return file; | |
| 94 } | 125 } |
| 95 _File._internal(); | |
| 96 | 126 |
| 97 bool _openFile(String name, bool writable) native "File_OpenFile"; | 127 bool _openFile(String name, bool writable) native "File_OpenFile"; |
| 98 | 128 |
| 99 void close() { | 129 void close() { |
| 100 _close(); | 130 throw "Unimplemented"; |
| 101 } | 131 } |
| 132 |
| 133 void closeSync() { |
| 134 _close(); |
| 135 } |
| 136 |
| 102 int _close() native "File_Close"; | 137 int _close() native "File_Close"; |
| 103 | 138 |
| 104 int readByte() { | 139 void readByte() { |
| 140 throw "Unimplemented"; |
| 141 } |
| 142 |
| 143 int readByteSync() { |
| 105 int result = _readByte(); | 144 int result = _readByte(); |
| 106 if (result == -1) { | 145 if (result == -1) { |
| 107 throw new FileIOException("Error: readByte failed"); | 146 throw new FileIOException("Error: readByte failed"); |
| 108 } | 147 } |
| 109 return result; | 148 return result; |
| 110 } | 149 } |
| 150 |
| 111 int _readByte() native "File_ReadByte"; | 151 int _readByte() native "File_ReadByte"; |
| 112 | 152 |
| 113 int writeByte(int value) { | 153 void readList(List<int> buffer, int offset, int bytes) { |
| 114 int result = _writeByte(value); | 154 throw "Unimplemented"; |
| 115 if (result == -1) { | |
| 116 throw new FileIOException("Error: writeByte failed"); | |
| 117 } | |
| 118 return result; | |
| 119 } | 155 } |
| 120 int _writeByte(int value) native "File_WriteByte"; | |
| 121 | 156 |
| 122 int writeString(String string) { | 157 int readListSync(List<int> buffer, int offset, int bytes) { |
| 123 int result = _writeString(string); | |
| 124 if (result == -1) { | |
| 125 throw new FileIOException("Error: writeString failed"); | |
| 126 } | |
| 127 return result; | |
| 128 } | |
| 129 int _writeString(String string) native "File_WriteString"; | |
| 130 | |
| 131 int readList(List<int> buffer, int offset, int bytes) { | |
| 132 if (bytes == 0) { | 158 if (bytes == 0) { |
| 133 return 0; | 159 return 0; |
| 134 } | 160 } |
| 135 if (offset < 0) { | 161 if (offset < 0) { |
| 136 throw new IndexOutOfRangeException(offset); | 162 throw new IndexOutOfRangeException(offset); |
| 137 } | 163 } |
| 138 if (bytes < 0) { | 164 if (bytes < 0) { |
| 139 throw new IndexOutOfRangeException(bytes); | 165 throw new IndexOutOfRangeException(bytes); |
| 140 } | 166 } |
| 141 if ((offset + bytes) > buffer.length) { | 167 if ((offset + bytes) > buffer.length) { |
| 142 throw new IndexOutOfRangeException(offset + bytes); | 168 throw new IndexOutOfRangeException(offset + bytes); |
| 143 } | 169 } |
| 144 int result = _readList(buffer, offset, bytes); | 170 int result = _readList(buffer, offset, bytes); |
| 145 if (result == -1) { | 171 if (result == -1) { |
| 146 throw new FileIOException("Error: readList failed"); | 172 throw new FileIOException("Error: readList failed"); |
| 147 } | 173 } |
| 148 return result; | 174 return result; |
| 149 } | 175 } |
| 150 int _readList(List<int> buffer, int offset, int bytes) | 176 |
| 151 native "File_ReadList"; | 177 int _readList(List<int> buffer, int offset, int bytes) native "File_ReadList"; |
| 178 |
| 179 void writeByte(int value) { |
| 180 throw "Unimplemented"; |
| 181 } |
| 182 |
| 183 int writeByteSync(int value) { |
| 184 int result = _writeByte(value); |
| 185 if (result == -1) { |
| 186 throw new FileIOException("Error: writeByte failed"); |
| 187 } |
| 188 return result; |
| 189 } |
| 190 |
| 191 int _writeByte(int value) native "File_WriteByte"; |
| 152 | 192 |
| 153 int writeList(List<int> buffer, int offset, int bytes) { | 193 int writeList(List<int> buffer, int offset, int bytes) { |
| 194 throw "Unimplemented"; |
| 195 } |
| 196 |
| 197 int writeListSync(List<int> buffer, int offset, int bytes) { |
| 154 if (bytes == 0) { | 198 if (bytes == 0) { |
| 155 return 0; | 199 return 0; |
| 156 } | 200 } |
| 157 if (offset < 0) { | 201 if (offset < 0) { |
| 158 throw new IndexOutOfRangeException(offset); | 202 throw new IndexOutOfRangeException(offset); |
| 159 } | 203 } |
| 160 if (bytes < 0) { | 204 if (bytes < 0) { |
| 161 throw new IndexOutOfRangeException(bytes); | 205 throw new IndexOutOfRangeException(bytes); |
| 162 } | 206 } |
| 163 if ((offset + bytes) > buffer.length) { | 207 if ((offset + bytes) > buffer.length) { |
| 164 throw new IndexOutOfRangeException(offset + bytes); | 208 throw new IndexOutOfRangeException(offset + bytes); |
| 165 } | 209 } |
| 166 int result = _writeList(buffer, offset, bytes); | 210 int result = _writeList(buffer, offset, bytes); |
| 167 if (result == -1) { | 211 if (result == -1) { |
| 168 throw new FileIOException("Error: writeList failed"); | 212 throw new FileIOException("Error: writeList failed"); |
| 169 } | 213 } |
| 170 return result; | 214 return result; |
| 171 } | 215 } |
| 216 |
| 172 int _writeList(List<int> buffer, int offset, int bytes) | 217 int _writeList(List<int> buffer, int offset, int bytes) |
| 173 native "File_WriteList"; | 218 native "File_WriteList"; |
| 174 | 219 |
| 175 int get position() { | 220 int writeString(String string) { |
| 221 throw "Unimplemented"; |
| 222 } |
| 223 |
| 224 int writeStringSync(String string) { |
| 225 int result = _writeString(string); |
| 226 if (result == -1) { |
| 227 throw new FileIOException("Error: writeString failed"); |
| 228 } |
| 229 return result; |
| 230 } |
| 231 |
| 232 int _writeString(String string) native "File_WriteString"; |
| 233 |
| 234 int position() { |
| 235 throw "Unimplemented"; |
| 236 } |
| 237 |
| 238 int positionSync() { |
| 176 int result = _position; | 239 int result = _position; |
| 177 if (result == -1) { | 240 if (result == -1) { |
| 178 throw new FileIOException("Error: get position failed"); | 241 throw new FileIOException("Error: get position failed"); |
| 179 } | 242 } |
| 180 return result; | 243 return result; |
| 181 } | 244 } |
| 245 |
| 182 int get _position() native "File_Position"; | 246 int get _position() native "File_Position"; |
| 183 | 247 |
| 184 int get length() { | 248 int length() { |
| 249 throw "Unimplemented"; |
| 250 } |
| 251 |
| 252 int lengthSync() { |
| 185 int result = _length; | 253 int result = _length; |
| 186 if (result == -1) { | 254 if (result == -1) { |
| 187 throw new FileIOException("Error: get length failed"); | 255 throw new FileIOException("Error: get length failed"); |
| 188 } | 256 } |
| 189 return result; | 257 return result; |
| 190 } | 258 } |
| 259 |
| 191 int get _length() native "File_Length"; | 260 int get _length() native "File_Length"; |
| 192 | 261 |
| 193 void flush() { | 262 void flush() { |
| 263 throw "Unimplemented"; |
| 264 } |
| 265 |
| 266 void flushSync() { |
| 194 int result = _flush(); | 267 int result = _flush(); |
| 195 if (result == -1) { | 268 if (result == -1) { |
| 196 throw new FileIOException("Error: flush failed"); | 269 throw new FileIOException("Error: flush failed"); |
| 197 } | 270 } |
| 198 } | 271 } |
| 272 |
| 199 int _flush() native "File_Flush"; | 273 int _flush() native "File_Flush"; |
| 200 | 274 |
| 201 // Each file has an unique InputStream. | 275 InputStream openInputStream() { |
| 202 InputStream get inputStream() { | 276 return new _FileInputStream(this); |
| 203 if (_inputStream == null) { | |
| 204 _inputStream = new _FileInputStream(this); | |
| 205 } | |
| 206 return _inputStream; | |
| 207 } | 277 } |
| 208 | 278 |
| 209 // Each file has an unique OutputStream. | 279 OutputStream openOutputStream() { |
| 210 OutputStream get outputStream() { | 280 return new _FileOutputStream(this); |
| 211 if (!_writeable) { | |
| 212 throw "File is not writable"; | |
| 213 } | |
| 214 if (_outputStream == null) { | |
| 215 _outputStream = new _FileOutputStream(this); | |
| 216 } | |
| 217 return _outputStream; | |
| 218 } | 281 } |
| 219 | 282 |
| 220 // Set of native methods used to provide file functionality. | 283 String get name() { |
| 221 static bool fileExists(String name) native "File_Exists"; | 284 return _name; |
| 285 } |
| 222 | 286 |
| 223 bool _writeable; | 287 void set existsHandler(void handler(bool exists)) { |
| 224 InputStream _inputStream; | 288 _existsHandler = handler; |
| 225 OutputStream _outputStream; | 289 } |
| 290 |
| 291 void set createHandler(void handler()) { |
| 292 _createHandler = handler; |
| 293 } |
| 294 |
| 295 void set openHandler(void handler()) { |
| 296 _openHandler = handler; |
| 297 } |
| 298 |
| 299 void set closeHandler(void handler()) { |
| 300 _closeHandler = handler; |
| 301 } |
| 302 |
| 303 void set readByteHandler(void handler(int byte)) { |
| 304 _readByteHandler = handler; |
| 305 } |
| 306 |
| 307 void set readListHandler(void handler(int read)) { |
| 308 _readListHandler = handler; |
| 309 } |
| 310 |
| 311 void set noPendingWriteHandler(void handler()) { |
| 312 _noPendingWriteHandler = handler; |
| 313 } |
| 314 |
| 315 void set errorHandler(void handler(String error)) { |
| 316 _errorHandler = handler; |
| 317 } |
| 318 |
| 319 String _name; |
| 320 int _id; |
| 321 |
| 322 var _existsHandler; |
| 323 var _createHandler; |
| 324 var _openHandler; |
| 325 var _closeHandler; |
| 326 var _readByteHandler; |
| 327 var _readListHandler; |
| 328 var _noPendingWriteHandler; |
| 329 var _errorHandler; |
| 226 } | 330 } |
| OLD | NEW |