| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 | |
| 6 /** | |
| 7 * FileMode describes the modes in which a file can be opened. | |
| 8 */ | |
| 9 class FileMode { | |
| 10 static const READ = const FileMode._internal(0); | |
| 11 static const WRITE = const FileMode._internal(1); | |
| 12 static const APPEND = const FileMode._internal(2); | |
| 13 const FileMode._internal(int this._mode); | |
| 14 final int _mode; | |
| 15 } | |
| 16 | |
| 17 | |
| 18 /** | |
| 19 * [File] objects are references to files. | |
| 20 * | |
| 21 * To operate on the underlying file data you need to either get | |
| 22 * streams using [openInputStream] and [openOutputStream] or open the | |
| 23 * file for random access operations using [open]. | |
| 24 */ | |
| 25 abstract class File { | |
| 26 /** | |
| 27 * Create a File object. | |
| 28 */ | |
| 29 factory File(String name) => new _File(name); | |
| 30 | |
| 31 /** | |
| 32 * Create a File object from a Path object. | |
| 33 */ | |
| 34 factory File.fromPath(Path path) => new _File.fromPath(path); | |
| 35 | |
| 36 /** | |
| 37 * Check if the file exists. Does not block and returns a | |
| 38 * [:Future<bool>:]. | |
| 39 */ | |
| 40 Future<bool> exists(); | |
| 41 | |
| 42 /** | |
| 43 * Synchronously check if the file exists. | |
| 44 */ | |
| 45 bool existsSync(); | |
| 46 | |
| 47 /** | |
| 48 * Create the file. Returns a [:Future<File>:] that completes with | |
| 49 * the file when it has been created. | |
| 50 * | |
| 51 * Existing files are left untouched by create. Calling create on an | |
| 52 * existing file might fail if there are restrictive permissions on | |
| 53 * the file. | |
| 54 */ | |
| 55 Future<File> create(); | |
| 56 | |
| 57 /** | |
| 58 * Synchronously create the file. Existing files are left untouched | |
| 59 * by create. Calling create on an existing file might fail if there | |
| 60 * are restrictive permissions on the file. | |
| 61 */ | |
| 62 void createSync(); | |
| 63 | |
| 64 /** | |
| 65 * Delete the file. Returns a [:Future<File>:] that completes with | |
| 66 * the file when it has been deleted. | |
| 67 */ | |
| 68 Future<File> delete(); | |
| 69 | |
| 70 /** | |
| 71 * Synchronously delete the file. | |
| 72 */ | |
| 73 void deleteSync(); | |
| 74 | |
| 75 /** | |
| 76 * Get a Directory object for the directory containing this | |
| 77 * file. Returns a [:Future<Directory>:] that completes with the | |
| 78 * directory. | |
| 79 */ | |
| 80 Future<Directory> directory(); | |
| 81 | |
| 82 /** | |
| 83 * Synchronously get a Directory object for the directory containing | |
| 84 * this file. | |
| 85 */ | |
| 86 Directory directorySync(); | |
| 87 | |
| 88 /** | |
| 89 * Get the length of the file. Returns a [:Future<int>:] that | |
| 90 * completes with the length in bytes. | |
| 91 */ | |
| 92 Future<int> length(); | |
| 93 | |
| 94 /** | |
| 95 * Synchronously get the length of the file. | |
| 96 */ | |
| 97 int lengthSync(); | |
| 98 | |
| 99 /** | |
| 100 * Get the last-modified time of the file. Returns a | |
| 101 * [:Future<Date>:] that completes with a [Date] object for the | |
| 102 * modification date. | |
| 103 */ | |
| 104 Future<Date> lastModified(); | |
| 105 | |
| 106 /** | |
| 107 * Get the last-modified time of the file. Throws an exception | |
| 108 * if the file does not exist. | |
| 109 */ | |
| 110 Date lastModifiedSync(); | |
| 111 | |
| 112 /** | |
| 113 * Open the file for random access operations. Returns a | |
| 114 * [:Future<RandomAccessFile>:] that completes with the opened | |
| 115 * random access file. RandomAccessFiles must be closed using the | |
| 116 * [close] method. | |
| 117 * | |
| 118 * Files can be opened in three modes: | |
| 119 * | |
| 120 * FileMode.READ: open the file for reading. | |
| 121 * | |
| 122 * FileMode.WRITE: open the file for both reading and writing and | |
| 123 * truncate the file to length zero. If the file does not exist the | |
| 124 * file is created. | |
| 125 * | |
| 126 * FileMode.APPEND: same as FileMode.WRITE except that the file is | |
| 127 * not truncated. | |
| 128 */ | |
| 129 Future<RandomAccessFile> open([FileMode mode = FileMode.READ]); | |
| 130 | |
| 131 /** | |
| 132 * Synchronously open the file for random access operations. The | |
| 133 * result is a RandomAccessFile on which random access operations | |
| 134 * can be performed. Opened RandomAccessFiles must be closed using | |
| 135 * the [close] method. | |
| 136 * | |
| 137 * See [open] for information on the [mode] argument. | |
| 138 */ | |
| 139 RandomAccessFile openSync([FileMode mode = FileMode.READ]); | |
| 140 | |
| 141 /** | |
| 142 * Get the canonical full path corresponding to the file name. | |
| 143 * Returns a [:Future<String>:] that completes with the path. | |
| 144 */ | |
| 145 Future<String> fullPath(); | |
| 146 | |
| 147 /** | |
| 148 * Synchronously get the canonical full path corresponding to the file name. | |
| 149 */ | |
| 150 String fullPathSync(); | |
| 151 | |
| 152 /** | |
| 153 * Create a new independent input stream for the file. The file | |
| 154 * input stream must be closed when no longer used to free up system | |
| 155 * resources. | |
| 156 */ | |
| 157 InputStream openInputStream(); | |
| 158 | |
| 159 /** | |
| 160 * Creates a new independent output stream for the file. The file | |
| 161 * output stream must be closed when no longer used to free up | |
| 162 * system resources. | |
| 163 * | |
| 164 * An output stream can be opened in two modes: | |
| 165 * | |
| 166 * FileMode.WRITE: create the stream and truncate the underlying | |
| 167 * file to length zero. | |
| 168 * | |
| 169 * FileMode.APPEND: create the stream and set the position to the end of | |
| 170 * the underlying file. | |
| 171 */ | |
| 172 OutputStream openOutputStream([FileMode mode = FileMode.WRITE]); | |
| 173 | |
| 174 /** | |
| 175 * Read the entire file contents as a list of bytes. Returns a | |
| 176 * [:Future<List<int>>:] that completes with the list of bytes that | |
| 177 * is the contents of the file. | |
| 178 */ | |
| 179 Future<List<int>> readAsBytes(); | |
| 180 | |
| 181 /** | |
| 182 * Synchronously read the entire file contents as a list of bytes. | |
| 183 */ | |
| 184 List<int> readAsBytesSync(); | |
| 185 | |
| 186 /** | |
| 187 * Read the entire file contents as text using the given | |
| 188 * [encoding]. | |
| 189 * | |
| 190 * Returns a [:Future<String>:] that completes with the string once | |
| 191 * the file contents has been read. | |
| 192 */ | |
| 193 Future<String> readAsText([Encoding encoding = Encoding.UTF_8]); | |
| 194 | |
| 195 /** | |
| 196 * Synchronously read the entire file contents as text using the | |
| 197 * given [encoding]. | |
| 198 */ | |
| 199 String readAsTextSync([Encoding encoding = Encoding.UTF_8]); | |
| 200 | |
| 201 /** | |
| 202 * Read the entire file contents as lines of text using the give | |
| 203 * [encoding]. | |
| 204 * | |
| 205 * Returns a [:Future<List<String>>:] that completes with the lines | |
| 206 * once the file contents has been read. | |
| 207 */ | |
| 208 Future<List<String>> readAsLines([Encoding encoding = Encoding.UTF_8]); | |
| 209 | |
| 210 /** | |
| 211 * Synchronously read the entire file contents as lines of text | |
| 212 * using the given [encoding]. | |
| 213 */ | |
| 214 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]); | |
| 215 | |
| 216 /** | |
| 217 * Get the name of the file. | |
| 218 */ | |
| 219 String get name; | |
| 220 } | |
| 221 | |
| 222 | |
| 223 /** | |
| 224 * [RandomAccessFile] provides random access to the data in a | |
| 225 * file. [RandomAccessFile] objects are obtained by calling the | |
| 226 * [:open:] method on a [File] object. | |
| 227 */ | |
| 228 abstract class RandomAccessFile { | |
| 229 /** | |
| 230 * Close the file. Returns a [:Future<RandomAccessFile>:] that | |
| 231 * completes with this RandomAccessFile when it has been closed. | |
| 232 */ | |
| 233 Future<RandomAccessFile> close(); | |
| 234 | |
| 235 /** | |
| 236 * Synchronously close the file. | |
| 237 */ | |
| 238 void closeSync(); | |
| 239 | |
| 240 /** | |
| 241 * Read a byte from the file. Returns a [:Future<int>:] that | |
| 242 * completes with the byte or -1 if end of file has been reached. | |
| 243 */ | |
| 244 Future<int> readByte(); | |
| 245 | |
| 246 /** | |
| 247 * Synchronously read a single byte from the file. If end of file | |
| 248 * has been reached -1 is returned. | |
| 249 */ | |
| 250 int readByteSync(); | |
| 251 | |
| 252 /** | |
| 253 * Read a List<int> from the file. Returns a [:Future<int>:] that | |
| 254 * completes with an indication of how much was read. | |
| 255 */ | |
| 256 Future<int> readList(List<int> buffer, int offset, int bytes); | |
| 257 | |
| 258 /** | |
| 259 * Synchronously read a List<int> from the file. Returns the number | |
| 260 * of bytes read. | |
| 261 */ | |
| 262 int readListSync(List<int> buffer, int offset, int bytes); | |
| 263 | |
| 264 /** | |
| 265 * Write a single byte to the file. Returns a | |
| 266 * [:Future<RandomAccessFile>:] that completes with this | |
| 267 * RandomAccessFile when the write completes. | |
| 268 */ | |
| 269 Future<RandomAccessFile> writeByte(int value); | |
| 270 | |
| 271 /** | |
| 272 * Synchronously write a single byte to the file. Returns the | |
| 273 * number of bytes successfully written. | |
| 274 */ | |
| 275 int writeByteSync(int value); | |
| 276 | |
| 277 /** | |
| 278 * Write a List<int> to the file. Returns a | |
| 279 * [:Future<RandomAccessFile>:] that completes with this | |
| 280 * RandomAccessFile when the write completes. | |
| 281 */ | |
| 282 Future<RandomAccessFile> writeList(List<int> buffer, int offset, int bytes); | |
| 283 | |
| 284 /** | |
| 285 * Synchronously write a List<int> to the file. Returns the number | |
| 286 * of bytes successfully written. | |
| 287 */ | |
| 288 int writeListSync(List<int> buffer, int offset, int bytes); | |
| 289 | |
| 290 /** | |
| 291 * Write a string to the file using the given [encoding]. Returns a | |
| 292 * [:Future<RandomAccessFile>:] that completes with this | |
| 293 * RandomAccessFile when the write completes. | |
| 294 */ | |
| 295 Future<RandomAccessFile> writeString(String string, | |
| 296 [Encoding encoding = Encoding.UTF_8]); | |
| 297 | |
| 298 /** | |
| 299 * Synchronously write a single string to the file using the given | |
| 300 * [encoding]. Returns the number of characters successfully | |
| 301 * written. | |
| 302 */ | |
| 303 int writeStringSync(String string, | |
| 304 [Encoding encoding = Encoding.UTF_8]); | |
| 305 | |
| 306 /** | |
| 307 * Get the current byte position in the file. Returns a | |
| 308 * [:Future<int>:] that completes with the position. | |
| 309 */ | |
| 310 Future<int> position(); | |
| 311 | |
| 312 /** | |
| 313 * Synchronously get the current byte position in the file. | |
| 314 */ | |
| 315 int positionSync(); | |
| 316 | |
| 317 /** | |
| 318 * Set the byte position in the file. Returns a | |
| 319 * [:Future<RandomAccessFile>:] that completes with this | |
| 320 * RandomAccessFile when the position has been set. | |
| 321 */ | |
| 322 Future<RandomAccessFile> setPosition(int position); | |
| 323 | |
| 324 /** | |
| 325 * Synchronously set the byte position in the file. | |
| 326 */ | |
| 327 void setPositionSync(int position); | |
| 328 | |
| 329 /** | |
| 330 * Truncate (or extend) the file to [length] bytes. Returns a | |
| 331 * [:Future<RandomAccessFile>:] that completes with this | |
| 332 * RandomAccessFile when the truncation has been performed. | |
| 333 */ | |
| 334 Future<RandomAccessFile> truncate(int length); | |
| 335 | |
| 336 /** | |
| 337 * Synchronously truncate (or extend) the file to [length] bytes. | |
| 338 */ | |
| 339 void truncateSync(int length); | |
| 340 | |
| 341 /** | |
| 342 * Get the length of the file. Returns a [:Future<int>:] that | |
| 343 * completes with the length in bytes. | |
| 344 */ | |
| 345 Future<int> length(); | |
| 346 | |
| 347 /** | |
| 348 * Synchronously get the length of the file. | |
| 349 */ | |
| 350 int lengthSync(); | |
| 351 | |
| 352 /** | |
| 353 * Flush the contents of the file to disk. Returns a | |
| 354 * [:Future<RandomAccessFile>:] that completes with this | |
| 355 * RandomAccessFile when the flush operation completes. | |
| 356 */ | |
| 357 Future<RandomAccessFile> flush(); | |
| 358 | |
| 359 /** | |
| 360 * Synchronously flush the contents of the file to disk. | |
| 361 */ | |
| 362 void flushSync(); | |
| 363 | |
| 364 /** | |
| 365 * Get the name of the file. | |
| 366 */ | |
| 367 String get name; | |
| 368 } | |
| 369 | |
| 370 | |
| 371 class FileIOException implements Exception { | |
| 372 const FileIOException([String this.message = "", | |
| 373 OSError this.osError = null]); | |
| 374 String toString() { | |
| 375 StringBuffer sb = new StringBuffer(); | |
| 376 sb.add("FileIOException"); | |
| 377 if (!message.isEmpty) { | |
| 378 sb.add(": $message"); | |
| 379 if (osError != null) { | |
| 380 sb.add(" ($osError)"); | |
| 381 } | |
| 382 } else if (osError != null) { | |
| 383 sb.add(": osError"); | |
| 384 } | |
| 385 return sb.toString(); | |
| 386 } | |
| 387 final String message; | |
| 388 final OSError osError; | |
| 389 } | |
| OLD | NEW |