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 /// Helper functionality to make working with IO easier. | 5 /// Helper functionality to make working with IO easier. |
6 library io; | 6 library io; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 (contents) { | 78 (contents) { |
79 // Sanity check: don't spew a huge file. | 79 // Sanity check: don't spew a huge file. |
80 if (contents.length < 1024 * 1024) { | 80 if (contents.length < 1024 * 1024) { |
81 return "Read $path. Contents:\n$contents"; | 81 return "Read $path. Contents:\n$contents"; |
82 } else { | 82 } else { |
83 return "Read ${contents.length} characters from $path."; | 83 return "Read ${contents.length} characters from $path."; |
84 } | 84 } |
85 }); | 85 }); |
86 } | 86 } |
87 | 87 |
88 /// Reads the contents of the binary file [file], which can either be a [String] | |
89 /// or a [File]. | |
90 Future<List<int>> readByteFile(file) { | |
Bob Nystrom
2013/01/31 19:08:29
"Byte" -> "Binary"?
"ByteFile" reads strangely to
nweiz
2013/01/31 21:39:10
Done.
| |
91 var path = _getPath(file); | |
92 return log.ioAsync("Reading byte file $path.", | |
93 new File(path).readAsBytes(), | |
Bob Nystrom
2013/01/31 19:08:29
Feel free to make this sync if you prefer.
nweiz
2013/01/31 21:39:10
Done.
| |
94 (contents) => "Read ${contents.length} bytes from $path."); | |
95 } | |
96 | |
88 /// Creates [file] (which can either be a [String] or a [File]), and writes | 97 /// Creates [file] (which can either be a [String] or a [File]), and writes |
89 /// [contents] to it. Completes when the file is written and closed. | 98 /// [contents] to it. Completes when the file is written and closed. |
90 /// | 99 /// |
91 /// If [dontLogContents] is true, the contents of the file will never be logged. | 100 /// If [dontLogContents] is true, the contents of the file will never be logged. |
92 Future<File> writeTextFile(file, String contents, {dontLogContents: false}) { | 101 Future<File> writeTextFile(file, String contents, {dontLogContents: false}) { |
93 var path = _getPath(file); | 102 var path = _getPath(file); |
94 file = new File(path); | 103 file = new File(path); |
95 | 104 |
96 // Sanity check: don't spew a huge file. | 105 // Sanity check: don't spew a huge file. |
97 log.io("Writing ${contents.length} characters to text file $path."); | 106 log.io("Writing ${contents.length} characters to text file $path."); |
98 if (!dontLogContents && contents.length < 1024 * 1024) { | 107 if (!dontLogContents && contents.length < 1024 * 1024) { |
99 log.fine("Contents:\n$contents"); | 108 log.fine("Contents:\n$contents"); |
100 } | 109 } |
101 | 110 |
102 return file.open(FileMode.WRITE).then((opened) { | 111 return file.open(FileMode.WRITE).then((opened) { |
103 return opened.writeString(contents).then((ignore) { | 112 return opened.writeString(contents).then((ignore) { |
104 return opened.close().then((_) { | 113 return opened.close().then((_) { |
105 log.fine("Wrote text file $path."); | 114 log.fine("Wrote text file $path."); |
106 return file; | 115 return file; |
107 }); | 116 }); |
108 }); | 117 }); |
109 }); | 118 }); |
110 } | 119 } |
111 | 120 |
121 /// Creates [file] (which can either be a [String] or a [File]), and writes | |
122 /// [contents] to it. Completes when the file is written and closed. | |
123 Future<File> writeByteFile(file, List<int> contents) { | |
Bob Nystrom
2013/01/31 19:08:29
Byte -> Binary.
nweiz
2013/01/31 21:39:10
Done.
| |
124 var path = _getPath(file); | |
125 file = new File(path); | |
126 | |
127 log.io("Writing ${contents.length} characters to binary file $path."); | |
Bob Nystrom
2013/01/31 19:08:29
"characters" -> "bytes".
nweiz
2013/01/31 21:39:10
Done.
| |
128 return file.open(FileMode.WRITE).then((opened) { | |
129 return opened.writeList(contents, 0, contents.length) | |
130 .then((_) => opened.close()); | |
131 }).then((_) { | |
132 log.fine("Wrote text file $path."); | |
133 return file; | |
134 }); | |
Bob Nystrom
2013/01/31 19:08:29
then() then() then()
Make this sync?
nweiz
2013/01/31 21:39:10
Done.
| |
135 } | |
136 | |
112 /// Asynchronously deletes [file], which can be a [String] or a [File]. Returns | 137 /// Asynchronously deletes [file], which can be a [String] or a [File]. Returns |
113 /// a [Future] that completes when the deletion is done. | 138 /// a [Future] that completes when the deletion is done. |
114 Future<File> deleteFile(file) { | 139 Future<File> deleteFile(file) { |
115 var path = _getPath(file); | 140 var path = _getPath(file); |
116 return log.ioAsync("delete file $path", | 141 return log.ioAsync("delete file $path", |
117 new File(path).delete()); | 142 new File(path).delete()); |
118 } | 143 } |
119 | 144 |
120 /// Writes [stream] to a new file at [path], which may be a [String] or a | 145 /// Writes [stream] to a new file at [path], which may be a [String] or a |
121 /// [File]. Will replace any file already at that path. Completes when the file | 146 /// [File]. Will replace any file already at that path. Completes when the file |
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
966 Directory _getDirectory(entry) { | 991 Directory _getDirectory(entry) { |
967 if (entry is Directory) return entry; | 992 if (entry is Directory) return entry; |
968 return new Directory(entry); | 993 return new Directory(entry); |
969 } | 994 } |
970 | 995 |
971 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 996 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
972 Uri _getUri(uri) { | 997 Uri _getUri(uri) { |
973 if (uri is Uri) return uri; | 998 if (uri is Uri) return uri; |
974 return Uri.parse(uri); | 999 return Uri.parse(uri); |
975 } | 1000 } |
OLD | NEW |