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 * File, socket, HTTP, and other I/O support for server applications. |
| 7 * |
| 8 * The I/O library is used for Dart server applications, |
| 9 * which run on a stand-alone Dart VM from the command line. |
| 10 * *This library does not work in browser-based applications.* |
| 11 * |
| 12 * This library allows you to work with files, directories, |
| 13 * sockets, processes, HTTP servers and clients, and more. |
| 14 * |
| 15 * To use this library in your code: |
| 16 * |
| 17 * import 'dart:io'; |
| 18 * |
| 19 * *Note:* Many operations related to input and output are asynchronous |
| 20 * and are handled using [Future]s or [Stream]s, both of which |
| 21 * are defined in the `dart:async` library. |
| 22 * |
| 23 * ## File, Directory, and Link |
| 24 * |
| 25 * An instance of [File], [Directory], or [Link] represents a file, |
| 26 * directory, or link, respectively, in the native file system. |
| 27 * |
| 28 * You can manipulate the file system through objects of these types. |
| 29 * For example, you can rename a file or directory: |
| 30 * |
| 31 * File myFile = new File('myFile.txt'); |
| 32 * myFile.rename('yourFile.txt').then((_) => print('file renamed')); |
| 33 * |
| 34 * Many methods provided by the File, Directory, and Link classes |
| 35 * run asynchronously and return a Future. |
| 36 * |
| 37 * ## FileSystemEntity |
| 38 * |
| 39 * File, Directory, and Link all extend [FileSystemEntity]. |
| 40 * In addition to being the superclass for these classes, |
| 41 * FileSystemEntity has a number of static methods for working with paths. |
| 42 * |
| 43 * To get information about a path, |
| 44 * you can use the FileSystemEntity static methods |
| 45 * such as 'isDirectory', 'isFile', and 'exists'. |
| 46 * Because file system access involves I/O, these methods |
| 47 * are asynchronous and return a Future. |
| 48 * |
| 49 * FileSystemEntity.isDirectory(myPath).then((isDir) { |
| 50 * if (isDir) { |
| 51 * print('$myPath is a directory'); |
| 52 * } else { |
| 53 * print('$myPath is not a directory'); |
| 54 * } |
| 55 * }); |
| 56 * |
| 57 * ## HttpServer and HttpClient |
| 58 * |
| 59 * The classes [HttpServer] and [HttpClient] |
| 60 * provide HTTP server and HTTP client functionality. |
| 61 * |
| 62 * The [HttpServer] class provides the basic functionality for |
| 63 * implementing an HTTP server. |
| 64 * For some higher-level building-blocks, we recommend that you try |
| 65 * the [http_server](https://pub.dartlang.org/packages/http_server) |
| 66 * pub package, which contains |
| 67 * a set of high-level classes that, together with the [HttpServer] class |
| 68 * in this library, make it easier to implement HTTP servers. |
| 69 * |
| 70 * ## Process |
| 71 * |
| 72 * The [Process] class provides a way to run a process on |
| 73 * the native machine. |
| 74 * For example, the following code spawns a process that recursively lists |
| 75 * the files under `web`. |
| 76 * |
| 77 * Process.start('ls', ['-R', 'web']).then((process) { |
| 78 * stdout.addStream(process.stdout); |
| 79 * stderr.addStream(process.stderr); |
| 80 * process.exitCode.then(print); |
| 81 * }); |
| 82 * |
| 83 * Using `start()` returns a Future, which completes with a [Process] object |
| 84 * when the process has started. This [Process] object allows you to interact |
| 85 * with the process while it is running. Using `run()` returns a Future, which |
| 86 * completes with a [ProcessResult] object when the spawned process has |
| 87 * terminated. This [ProcessResult] object collects the output and exit code |
| 88 * from the process. |
| 89 * |
| 90 * When using `start()`, |
| 91 * you need to read all data coming on the stdout and stderr streams otherwise |
| 92 * the system resources will not be freed. |
| 93 * |
| 94 * ## WebSocket |
| 95 * |
| 96 * The [WebSocket] class provides support for the web socket protocol. This |
| 97 * allows full-duplex communications between client and server applications. |
| 98 * Use the WebSocket class in the `dart:html` library for web clients. |
| 99 * |
| 100 * A web socket server uses a normal HTTP server for accepting web socket |
| 101 * connections. The initial handshake is a HTTP request which is then upgraded t
o a |
| 102 * web socket connection. |
| 103 * The server upgrades the request using [WebSocketTransformer] |
| 104 * and listens for the data on the returned web socket. |
| 105 * For example, here's a mini server that listens for 'ws' data |
| 106 * on a WebSocket: |
| 107 * |
| 108 * runZoned(() { |
| 109 * HttpServer.bind('127.0.0.1', 4040).then((server) { |
| 110 * server.listen((HttpRequest req) { |
| 111 * if (req.uri.path == '/ws') { |
| 112 * WebSocketTransformer.upgrade(req).then((socket) { |
| 113 * socket.listen(handleMsg); |
| 114 * }); |
| 115 * } |
| 116 * }); |
| 117 * }); |
| 118 * }, |
| 119 * onError: (e) => print("An error occurred.")); |
| 120 * |
| 121 * The client connects to the WebSocket using the `connect()` method |
| 122 * and a URI that uses the Web Socket protocol. |
| 123 * The the client can write to the WebSocket with the `add()` method. |
| 124 * For example, |
| 125 * |
| 126 * WebSocket.connect('ws://127.0.0.1:4040/ws').then((socket) { |
| 127 * socket.add('Hello, World!'); |
| 128 * }); |
| 129 * |
| 130 * Check out the |
| 131 * [dartiverse_search](https://github.com/dart-lang/sample-dartiverse-search) |
| 132 * sample for a client/server pair that uses |
| 133 * WebSockets to communicate. |
| 134 * |
| 135 * ## Socket and ServerSocket |
| 136 * |
| 137 * Clients and servers use [Socket]s to communicate using the TCP protocol. |
| 138 * Use [ServerSocket] on the server side and [Socket] on the client. |
| 139 * The server creates a listening socket using the `bind()` method and |
| 140 * then listens for incoming connections on the socket. For example: |
| 141 * |
| 142 * ServerSocket.bind('127.0.0.1', 4041) |
| 143 * .then((serverSocket) { |
| 144 * serverSocket.listen((socket) { |
| 145 * socket.transform(UTF8.decoder).listen(print); |
| 146 * }); |
| 147 * }); |
| 148 * |
| 149 * A client connects a Socket using the `connect()` method, |
| 150 * which returns a Future. |
| 151 * Using `write()`, `writeln()`, or `writeAll()` are the easiest ways to |
| 152 * send data over the socket. |
| 153 * For example: |
| 154 * |
| 155 * Socket.connect('127.0.0.1', 4041).then((socket) { |
| 156 * socket.write('Hello, World!'); |
| 157 * }); |
| 158 * |
| 159 * Besides [Socket] and [ServerSocket], the [RawSocket] and |
| 160 * [RawServerSocket] classes are available for lower-level access |
| 161 * to async socket IO. |
| 162 * |
| 163 * ## Standard output, error, and input streams |
| 164 * |
| 165 * This library provides the standard output, error, and input |
| 166 * streams, named 'stdout', 'stderr', and 'stdin', respectively. |
| 167 * |
| 168 * The stdout and stderr streams are both [IOSink]s and have the same set |
| 169 * of methods and properties. |
| 170 * |
| 171 * To write a string to 'stdout': |
| 172 * |
| 173 * stdout.writeln('Hello, World!'); |
| 174 * |
| 175 * To write a list of objects to 'stderr': |
| 176 * |
| 177 * stderr.writeAll([ 'That ', 'is ', 'an ', 'error.', '\n']); |
| 178 * |
| 179 * The standard input stream is a true [Stream], so it inherits |
| 180 * properties and methods from the Stream class. |
| 181 * |
| 182 * To read text synchronously from the command line |
| 183 * (the program blocks waiting for user to type information): |
| 184 * |
| 185 * String inputText = stdin.readLineSync(); |
| 186 * |
| 187 * ## Other resources |
| 188 * |
| 189 * For an introduction to I/O in Dart, see the [dart:io section of the library |
| 190 * tour](https://www.dartlang.org/docs/dart-up-and-running/ch03.html#dartio---io
-for-command-line-apps). |
| 191 * |
| 192 * To learn more about I/O in Dart, refer to the [tutorial about writing |
| 193 * command-line apps](https://www.dartlang.org/docs/tutorials/cmdline/). |
| 194 */ |
| 195 library dart.io; |
| 196 |
| 197 import 'dart:async'; |
| 198 import 'dart:_internal'; |
| 199 import 'dart:collection' show HashMap, |
| 200 HashSet, |
| 201 Queue, |
| 202 ListQueue, |
| 203 LinkedList, |
| 204 LinkedListEntry, |
| 205 UnmodifiableMapView; |
| 206 import 'dart:convert'; |
| 207 import 'dart:developer'; |
| 208 import 'dart:isolate'; |
| 209 import 'dart:math'; |
| 210 import 'dart:typed_data'; |
| 211 |
| 212 part 'bytes_builder.dart'; |
| 213 part 'common.dart'; |
| 214 part 'crypto.dart'; |
| 215 part 'data_transformer.dart'; |
| 216 part 'directory.dart'; |
| 217 part 'directory_impl.dart'; |
| 218 part 'eventhandler.dart'; |
| 219 part 'file.dart'; |
| 220 part 'file_impl.dart'; |
| 221 part 'file_system_entity.dart'; |
| 222 part 'http.dart'; |
| 223 part 'http_date.dart'; |
| 224 part 'http_headers.dart'; |
| 225 part 'http_impl.dart'; |
| 226 part 'http_parser.dart'; |
| 227 part 'http_session.dart'; |
| 228 part 'io_resource_info.dart'; |
| 229 part 'io_sink.dart'; |
| 230 part 'io_service.dart'; |
| 231 part 'link.dart'; |
| 232 part 'platform.dart'; |
| 233 part 'platform_impl.dart'; |
| 234 part 'process.dart'; |
| 235 part 'secure_server_socket.dart'; |
| 236 part 'secure_socket.dart'; |
| 237 part 'security_context.dart'; |
| 238 part 'service_object.dart'; |
| 239 part 'socket.dart'; |
| 240 part 'stdio.dart'; |
| 241 part 'string_transformer.dart'; |
| 242 part 'websocket.dart'; |
| 243 part 'websocket_impl.dart'; |
OLD | NEW |