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 /** | 5 /** |
6 * File, socket, HTTP, and other I/O support for server applications. | 6 * File, socket, HTTP, and other I/O support for server applications. |
7 * | 7 * |
8 * The IO library is used for Dart server applications, | 8 * The I/O library is used for Dart server applications, |
9 * which run on a stand-alone Dart VM from the command line. | 9 * which run on a stand-alone Dart VM from the command line. |
10 * *This library does not work in browser based applications.* | 10 * *This library does not work in browser-based applications.* |
11 * | 11 * |
12 * This library allows you to work with files, directories, | 12 * This library allows you to work with files, directories, |
13 * sockets, processes, HTTP servers and clients, and more. | 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 * You can use the `start()` or `run()` method to run a process. | |
84 * Using `start()` allows you to interact with the process whereas | |
85 * using `run()` does not. | |
86 * For example, if you use `run()` instead of `start()` in the code snippet abov e, | |
87 * you cannot call `exitCode`. | |
Søren Gjesse
2014/01/02 09:49:18
Change the paragraph above to:
Using `start()` re
mem
2014/01/02 18:18:27
Done.
| |
88 * | |
89 * When using `start()`, | |
90 * you need to read all data coming on the stdout and stderr streams otherwise | |
91 * the system resources will not be freed. | |
92 * | |
93 * ## WebSocket | |
94 * | |
95 * A [WebSocket] is a two-way HTTP communication object that allows | |
96 * client and server applications to send data back and forth. | |
Søren Gjesse
2014/01/02 09:49:18
Consider changing the first sentence to:
The [Web
mem
2014/01/02 18:18:27
Done.
| |
97 * Use the WebSocket class in the dart:html library for web clients. | |
Søren Gjesse
2014/01/02 09:49:18
Add `s around dart:html.
mem
2014/01/02 18:18:27
Done.
| |
98 * | |
99 * A server binds to a host and port using [HttpServer] and listens for data. | |
100 * When data arrives, if the request uses the Web Socket protocol | |
101 * (the URI ends with '/ws'), | |
Søren Gjesse
2014/01/02 09:49:18
Consider changing the text above to:
A web socket
mem
2014/01/02 18:18:27
Done.
| |
102 * the server upgrades the request using [WebSocketTransformer] | |
103 * and listens for the data on the returned web socket. | |
104 * For example, here's a mini server that listens for 'ws' data | |
105 * on a WebSocket: | |
106 * | |
107 * runZoned(() { | |
108 * HttpServer.bind('127.0.0.1', 4040).then((server) { | |
109 * server.listen((HttpRequest req) { | |
110 * if (req.uri.path == '/ws') { | |
111 * WebSocketTransformer.upgrade(req).then((socket) { | |
112 * socket.listen(handleMsg); | |
113 * }); | |
114 * } | |
115 * }); | |
116 * }); | |
117 * }, | |
118 * onError: (e) => print("An error occurred.")); | |
119 * | |
120 * The client connects to the WebSocket using the `connect()` method | |
121 * and a URI that uses the Web Socket protocol. | |
122 * The the client can write to the WebSocket with the `add()` method. | |
123 * For example, | |
124 * | |
125 * WebSocket.connect('ws://127.0.0.1:4040/ws').then((socket) { | |
126 * socket.add('Hello, World!'); | |
127 * }); | |
128 * | |
129 * Check out the | |
130 * [dartiverse_search](https://code.google.com/p/dart/source/browse/branches/ble eding_edge/dart/samples/dartiverse_search) | |
131 * sample for a client/server pair that uses | |
132 * WebSockets to communicate. | |
133 * | |
134 * ## Socket and ServerSocket | |
135 * | |
136 * Clients and servers use [Socket]s to communicate using the TCP protocol. | |
137 * Use [ServerSocket] on the server side and [Socket] on the client. | |
138 * The server creates a socket using the `bind()` method and then listens for da ta | |
Søren Gjesse
2014/01/02 09:49:18
'creates a socket' -> 'creates a listening socket'
mem
2014/01/02 18:18:27
Done.
| |
139 * on the socket. For example: | |
140 * | |
141 * ServerSocket.bind('127.0.0.1', 4041) | |
142 * .then((serverSocket) { | |
143 * serverSocket.listen((socket) { | |
144 * socket.transform(UTF8.decoder).listen(print); | |
145 * }); | |
146 * }); | |
147 * | |
148 * A client connects to a Socket using the `connect()` method, | |
Søren Gjesse
2014/01/02 09:49:18
'connects to a Socket' -> 'connects a Socket'
mem
2014/01/02 18:18:27
Done.
| |
149 * which returns a Future. | |
150 * Using `write()`, `writeln()`, or `writeAll()` are the easiest ways to | |
151 * send data over the socket. | |
152 * For example: | |
153 * | |
154 * Socket.connect('127.0.0.1', 4041).then((socket) { | |
155 * socket.write('Hello, World!'); | |
156 * }); | |
157 * | |
Søren Gjesse
2014/01/02 09:49:18
Maybe add a short paragraph:
Besides [Socket] and
mem
2014/01/02 18:18:27
Done.
| |
158 * ## Standard output, error, and input streams | |
159 * | |
160 * This library provides the standard output, error, and input | |
161 * streams, named 'stdout', 'stderr', and 'stdin', respectively. | |
162 * | |
163 * The stdout and stderr streams are both [IOSink]s and have the same set | |
164 * of methods and properties. | |
165 * | |
166 * To write a string to 'stdout': | |
167 * | |
168 * stdout.writeln('Hello, World!'); | |
169 * | |
170 * To write a list of objects to 'stderr': | |
171 * | |
172 * stderr.writeAll([ 'That ', 'is ', 'an ', 'error.', '\n']); | |
173 * | |
174 * The standard input stream is a true [Stream], so it inherits | |
175 * properties and methods from the Stream class. | |
176 * | |
177 * To read text synchronously from the command line | |
178 * (the program blocks waiting for user to type information): | |
179 * | |
180 * String inputText = stdin.readLineSync(); | |
181 * | |
182 * ## Other resources | |
183 * | |
184 * For an introduction to I/O in Dart, see the | |
185 * [dart:io section of the library tour] | |
186 * (https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-da rtio---file-and-socket-io-for-command-line-apps). | |
187 * | |
188 * To learn more about I/O in Dart, refer to the | |
189 * [tutorial about writing command-line apps] | |
190 * (https://www.dartlang.org/docs/tutorials/io/). | |
14 */ | 191 */ |
15 library dart.io; | 192 library dart.io; |
16 | 193 |
17 import 'dart:async'; | 194 import 'dart:async'; |
18 import 'dart:_collection-dev'; | 195 import 'dart:_collection-dev'; |
19 import 'dart:collection' show HashMap, | 196 import 'dart:collection' show HashMap, |
20 HashSet, | 197 HashSet, |
21 LinkedList, | 198 LinkedList, |
22 LinkedListEntry; | 199 LinkedListEntry; |
23 import 'dart:convert'; | 200 import 'dart:convert'; |
(...skipping 24 matching lines...) Expand all Loading... | |
48 part 'platform_impl.dart'; | 225 part 'platform_impl.dart'; |
49 part 'process.dart'; | 226 part 'process.dart'; |
50 part 'socket.dart'; | 227 part 'socket.dart'; |
51 part 'stdio.dart'; | 228 part 'stdio.dart'; |
52 part 'string_transformer.dart'; | 229 part 'string_transformer.dart'; |
53 part 'timer_impl.dart'; | 230 part 'timer_impl.dart'; |
54 part 'secure_socket.dart'; | 231 part 'secure_socket.dart'; |
55 part 'secure_server_socket.dart'; | 232 part 'secure_server_socket.dart'; |
56 part 'websocket.dart'; | 233 part 'websocket.dart'; |
57 part 'websocket_impl.dart'; | 234 part 'websocket_impl.dart'; |
OLD | NEW |