Chromium Code Reviews| 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 interface File factory _File { | 5 interface File factory _File { |
| 6 // Open a file. | 6 /** |
| 7 File(String name, bool writable); | 7 * Create a File object. |
| 8 */ | |
| 9 File(String name); | |
| 8 | 10 |
| 9 // Close the file. | 11 /** |
| 12 * Check if the file exists. The [existsHandler] is called with the | |
| 13 * result when the operation completes. | |
| 14 */ | |
| 15 void exists(); | |
| 16 | |
| 17 /** | |
| 18 * Synchronously check if the file exists. | |
| 19 */ | |
| 20 bool existsSync(); | |
| 21 | |
| 22 /** | |
| 23 * Create the file. The [createHandler] is called when the file has | |
| 24 * been created. The errorHandler is called if the file cannot be | |
| 25 * created. | |
| 26 */ | |
| 27 void create(); | |
| 28 | |
| 29 /** | |
| 30 * Synchronously create the file. | |
| 31 */ | |
| 32 void createSync(); | |
| 33 | |
| 34 /** | |
| 35 * Open the file for random access operations. When the file is | |
| 36 * opened the openHandler is called. Opened files must be closed | |
| 37 * using the [close] method. By default writable is false. | |
| 38 */ | |
| 39 void open([bool writable]); | |
|
Søren Gjesse
2011/10/27 13:07:50
[bool writable] -> [bool writable = false]
Mads Ager (google)
2011/10/27 13:21:46
You can only supply the default when you implement
| |
| 40 | |
| 41 /** | |
| 42 * Synchronously open the file for random access operations. Opened | |
| 43 * files must be closed using the [close] method. By default | |
| 44 * writable is false. | |
| 45 */ | |
| 46 void openSync([bool writable]); | |
|
Søren Gjesse
2011/10/27 13:07:50
Ditto.
Mads Ager (google)
2011/10/27 13:21:46
Same, can only supply the default value when you i
| |
| 47 | |
| 48 /** | |
| 49 * Close the file. When the file is closed the closeHandler is | |
| 50 * called. | |
| 51 */ | |
| 10 void close(); | 52 void close(); |
| 11 | 53 |
| 12 // Synchronously read a single byte from the file. | 54 /** |
| 13 // TODO(jrgfogh): Remove this call. | 55 * Synchronously close the file. |
| 14 int readByte(); | 56 */ |
| 57 void closeSync(); | |
| 15 | 58 |
| 16 // Synchronously write a single byte to the file. | 59 /** |
| 17 // TODO(jrgfogh): Remove this call. | 60 * Read a byte from the file. When the byte has been read the |
| 18 int writeByte(int value); | 61 * [readByteHandler] is called with the value. |
| 62 */ | |
| 63 void readByte(); | |
| 19 | 64 |
| 20 // Synchronously write a single string to the file. | 65 /** |
| 21 // TODO(jrgfogh): Remove this call. | 66 * Synchronously read a single byte from the file. |
| 22 int writeString(String string); | 67 */ |
| 68 int readByteSync(); | |
| 23 | 69 |
| 24 // Synchronously read a List<int> from the file. | 70 /** |
| 25 // TODO(jrgfogh): Remove this call. | 71 * Read a List<int> from the file. When the list has been read the |
| 26 int readList(List<int> buffer, int offset, int bytes); | 72 * [readListHandler] is called with an integer indicating how much |
| 73 * was read. | |
| 74 */ | |
| 75 void readList(List<int> buffer, int offset, int bytes); | |
| 27 | 76 |
| 28 // Synchronously write a List<int> to the file. | 77 /** |
| 29 // TODO(jrgfogh): Remove this call. | 78 * Synchronously read a List<int> from the file. Returns the number |
| 30 int writeList(List<int> buffer, int offset, int bytes); | 79 * of bytes read. |
| 80 */ | |
| 81 int readListSync(List<int> buffer, int offset, int bytes); | |
| 31 | 82 |
| 32 // The current position of the file handle. | 83 /** |
| 33 int get position(); | 84 * Write a single byte to the file. If the byte cannot be written |
| 85 * the [errorHandler] is called. When all pending write operations | |
| 86 * have finished the [noPendingWriteHandler] is called. | |
| 87 */ | |
| 88 void writeByte(int value); | |
| 34 | 89 |
| 35 // The length of the file. | 90 /** |
| 36 int get length(); | 91 * Synchronously write a single byte to the file. Returns true if |
| 92 * the byte was successfully written and false otherwise. | |
| 93 */ | |
| 94 bool writeByteSync(int value); | |
| 37 | 95 |
| 38 // Flush the contents of the file to disk. | 96 /** |
| 39 void flush(); | 97 * Write a List<int> to the file. If the list cannot be written the |
| 98 * [errorHandler] is called. When all pending write operations have | |
| 99 * finished the [noPendingWriteHandler] is called. | |
| 100 */ | |
| 101 void writeList(List<int> buffer, int offset, int bytes); | |
| 40 | 102 |
| 41 // Each file has an unique InputStream. | 103 /** |
| 42 InputStream get inputStream(); | 104 * Synchronously write a List<int> to the file. If the list cannot be |
|
Søren Gjesse
2011/10/27 13:07:50
Replace "If the list..." with !Returns true if the
Mads Ager (google)
2011/10/27 13:21:46
Thanks! Done.
| |
| 105 * written the [errorHandler] is called. When all pending write | |
| 106 * operations have finished the [noPendingWriteHandler] is called. | |
| 107 */ | |
| 108 bool writeListSync(List<int> buffer, int offset, int bytes); | |
| 43 | 109 |
| 44 // Each file has an unique OutputStream. | 110 /** |
| 45 OutputStream get outputStream(); | 111 * Write a string to the file. If the string cannot be written the |
| 112 * [errorHandler] is called. When all pending write operations have | |
| 113 * finished the [noPendingWriteHandler] is called. | |
| 114 */ | |
| 115 void writeString(String string); | |
|
Søren Gjesse
2011/10/27 13:07:50
Shouldn't this have an encoding?
Mads Ager (google)
2011/10/27 13:21:46
Yes, it should. I'll add a TODO and address it in
| |
| 116 | |
| 117 /** | |
| 118 * Synchronously write a single string to the file. Returns true if | |
| 119 * the string was successfully written and false otherwise. | |
| 120 */ | |
| 121 bool writeStringSync(String string); | |
|
Søren Gjesse
2011/10/27 13:07:50
Ditto.
Mads Ager (google)
2011/10/27 13:21:46
Yes, I'll add a TODO.
| |
| 122 | |
| 123 /** | |
| 124 * Get the current position of the file. When the operation | |
| 125 * completes the [positionHandler] is called with the position. | |
| 126 */ | |
| 127 void position(); | |
| 128 | |
| 129 /** | |
| 130 * Synchronously get the current position of the file. | |
| 131 */ | |
| 132 int positionSync(); | |
| 133 | |
| 134 /** | |
| 135 * Get the length of the file. When the operation completes the | |
| 136 * [lengthHandler] is called with the length. | |
| 137 */ | |
| 138 void length(); | |
| 139 | |
| 140 /** | |
| 141 * Get the length of the file. When the operation completes the | |
| 142 * [lengthHandler] is called with the length. | |
| 143 */ | |
| 144 int lengthSync(); | |
| 145 | |
| 146 /** | |
| 147 * Flush the contents of the file to disk. If there are no pending | |
| 148 * write operation after the flush operation completes, the | |
| 149 * [noPendingWriteHandler] is called. | |
| 150 */ | |
| 151 void flush(); | |
| 152 | |
| 153 /** | |
| 154 * Synchronously flush the contents of the file to disk. | |
| 155 */ | |
| 156 void flushSync(); | |
| 157 | |
| 158 /** | |
| 159 * Create a new independent input stream for the file. The file | |
| 160 * input stream must be closed when no longer used. | |
| 161 */ | |
| 162 FileInputStream openInputStream(); | |
| 163 | |
| 164 /** | |
| 165 * Creates a new independent output stream for the file. The file | |
| 166 * output stream must be closed when no longer used. | |
| 167 */ | |
| 168 FileOutputStream openOutputStream(); | |
| 169 | |
| 170 /** | |
| 171 * Get the name of the file. | |
| 172 */ | |
| 173 String get name(); | |
| 174 | |
| 175 // Event handlers. | |
| 176 void set existsHandler(void handler(bool exists)); | |
| 177 void set createHandler(void handler()); | |
| 178 void set openHandler(void handler()); | |
| 179 void set closeHandler(void handler()); | |
| 180 void set readByteHandler(void handler(int byte)); | |
| 181 void set readListHandler(void handler(int read)); | |
| 182 void set noPendingWriteHandler(void handler()); | |
| 183 void set errorHandler(void handler(String error)); | |
| 46 } | 184 } |
| 47 | 185 |
| 48 | 186 |
| 49 class FileUtil { | 187 interface FileInputStream extends InputStream { |
| 50 static bool fileExists(String name) native "File_Exists"; | 188 void close(); |
| 189 } | |
| 190 | |
| 191 | |
| 192 interface FileOutputStream extends OutputStream { | |
| 193 void close(); | |
| 51 } | 194 } |
| 52 | 195 |
| 53 | 196 |
| 54 class FileIOException implements Exception { | 197 class FileIOException implements Exception { |
| 55 const FileIOException([String this.message = ""]); | 198 const FileIOException([String this.message = ""]); |
| 56 String toString() => "FileIOException: $message"; | 199 String toString() => "FileIOException: $message"; |
| 57 | |
| 58 /* | |
| 59 * Contains the exception message. | |
| 60 */ | |
| 61 final String message; | 200 final String message; |
| 62 } | 201 } |
| OLD | NEW |