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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * [Directory] objects are used for working with directories. | 8 * [Directory] objects are used for working with directories. |
9 */ | 9 */ |
10 abstract class Directory { | 10 abstract class Directory extends FileSystemEntity { |
11 /** | 11 /** |
12 * Creates a directory object. The path is either an absolute path, | 12 * Creates a directory object. The path is either an absolute path, |
13 * or it is a relative path which is interpreted relative to the directory | 13 * or it is a relative path which is interpreted relative to the directory |
14 * in which the Dart VM was started. | 14 * in which the Dart VM was started. |
15 */ | 15 */ |
16 factory Directory(String path) => new _Directory(path); | 16 factory Directory(String path) => new _Directory(path); |
17 | 17 |
18 /** | 18 /** |
19 * Creates a directory object from a Path object. The path is either | 19 * Creates a directory object from a Path object. The path is either |
20 * an absolute path, or it is a relative path which is interpreted | 20 * an absolute path, or it is a relative path which is interpreted |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 * Synchronously rename this directory. Returns a [Directory] | 124 * Synchronously rename this directory. Returns a [Directory] |
125 * instance for the renamed directory. | 125 * instance for the renamed directory. |
126 * | 126 * |
127 * If newPath identifies an existing directory, that directory is | 127 * If newPath identifies an existing directory, that directory is |
128 * replaced. If newPath identifies an existing file the operation | 128 * replaced. If newPath identifies an existing file the operation |
129 * fails and an exception is thrown. | 129 * fails and an exception is thrown. |
130 */ | 130 */ |
131 Directory renameSync(String newPath); | 131 Directory renameSync(String newPath); |
132 | 132 |
133 /** | 133 /** |
134 * List the sub-directories and files of this | 134 * List the sub-directories and files of this [Directory]. |
135 * [Directory]. Optionally recurse into sub-directories. Returns a | 135 * Optionally recurse into sub-directories. |
136 * [DirectoryLister] object representing the active listing | 136 * |
137 * operation. Handlers for files and directories should be | 137 * The result is a stream of [FileSystemEntity] objects |
138 * registered on this DirectoryLister object. | 138 * for the directories and files. |
139 */ | 139 */ |
140 DirectoryLister list({bool recursive: false}); | 140 Stream<FileSystemEntity> list({bool recursive: false}); |
141 | 141 |
142 /** | 142 /** |
143 * List the sub-directories and files of this | 143 * List the sub-directories and files of this [Directory]. |
144 * [Directory]. Optionally recurse into sub-directories. Returns a | 144 * Optionally recurse into sub-directories. |
145 * List containing Directory and File objects. | 145 * |
| 146 * Returns a [List] containing [FileSystemEntity] objects for the |
| 147 * directories and files. |
146 */ | 148 */ |
147 List listSync({bool recursive: false}); | 149 List<FileSystemEntity> listSync({bool recursive: false}); |
148 | 150 |
149 /** | 151 /** |
150 * Returns a human readable string for this Directory instance. | 152 * Returns a human readable string for this Directory instance. |
151 */ | 153 */ |
152 String toString(); | 154 String toString(); |
153 | 155 |
154 /** | 156 /** |
155 * Gets the path of this directory. | 157 * Gets the path of this directory. |
156 */ | 158 */ |
157 final String path; | 159 final String path; |
158 } | 160 } |
159 | 161 |
160 | 162 |
161 /** | |
162 * A [DirectoryLister] represents an actively running listing operation. | |
163 * | |
164 * A [DirectoryLister] is obtained from a [Directory] object by calling | |
165 * the [:Directory.list:] method. | |
166 * | |
167 * Directory dir = new Directory('path/to/my/dir'); | |
168 * DirectoryLister lister = dir.list(); | |
169 * | |
170 * For each file and directory, the file or directory handler is | |
171 * called. When all directories have been listed the done handler is | |
172 * called. If the listing operation is recursive, the error handler is | |
173 * called if a subdirectory cannot be opened for listing. | |
174 */ | |
175 abstract class DirectoryLister { | |
176 /** | |
177 * Sets the directory handler that is called for all directories | |
178 * during listing. The directory handler is called with the full | |
179 * path of the directory. | |
180 */ | |
181 void set onDir(void onDir(String dir)); | |
182 | |
183 /** | |
184 * Sets the handler that is called for all files during listing. The | |
185 * file handler is called with the full path of the file. | |
186 */ | |
187 void set onFile(void onFile(String file)); | |
188 | |
189 /** | |
190 * Set the handler that is called when a listing is done. The | |
191 * handler is called with an indication of whether or not the | |
192 * listing operation completed. | |
193 */ | |
194 void set onDone(void onDone(bool completed)); | |
195 | |
196 /** | |
197 * Sets the handler that is called if there is an error while | |
198 * listing directories. | |
199 */ | |
200 void set onError(void onError(e)); | |
201 } | |
202 | |
203 | |
204 class DirectoryIOException implements Exception { | 163 class DirectoryIOException implements Exception { |
205 const DirectoryIOException([String this.message = "", | 164 const DirectoryIOException([String this.message = "", |
206 String this.path = "", | 165 String this.path = "", |
207 OSError this.osError = null]); | 166 OSError this.osError = null]); |
208 String toString() { | 167 String toString() { |
209 StringBuffer sb = new StringBuffer(); | 168 StringBuffer sb = new StringBuffer(); |
210 sb.add("DirectoryIOException"); | 169 sb.add("DirectoryIOException"); |
211 if (!message.isEmpty) { | 170 if (!message.isEmpty) { |
212 sb.add(": $message"); | 171 sb.add(": $message"); |
213 if (path != null) { | 172 if (path != null) { |
214 sb.add(", path = $path"); | 173 sb.add(", path = $path"); |
215 } | 174 } |
216 if (osError != null) { | 175 if (osError != null) { |
217 sb.add(" ($osError)"); | 176 sb.add(" ($osError)"); |
218 } | 177 } |
219 } else if (osError != null) { | 178 } else if (osError != null) { |
220 sb.add(": $osError"); | 179 sb.add(": $osError"); |
221 if (path != null) { | 180 if (path != null) { |
222 sb.add(", path = $path"); | 181 sb.add(", path = $path"); |
223 } | 182 } |
224 } | 183 } |
225 return sb.toString(); | 184 return sb.toString(); |
226 } | 185 } |
227 final String message; | 186 final String message; |
228 final String path; | 187 final String path; |
229 final OSError osError; | 188 final OSError osError; |
230 } | 189 } |
OLD | NEW |