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 /** | 5 /** |
| 6 * Input is read from a given input stream. Such an input stream can | 6 * Input streams are used to read data sequentially from some data |
| 7 * be an endpoint, e.g., a socket or a file, or another input stream. | 7 * source. All input streams are non-blocking. They each have a number |
| 8 * Multiple input streams can be chained together to operate collaboratively | 8 * of read calls which will always return without any IO related |
| 9 * on a given input. | 9 * blocking. If the requested data is not available a read call will |
| 10 * return [:null:]. All input streams have one or more handlers which | |
| 11 * will trigger when data is available. | |
| 12 * | |
| 13 * The following example shows a data handler in an ordinary input | |
| 14 * stream which will be called when some data is available and a call | |
| 15 * to read will not return [:null:]. | |
| 16 * | |
| 17 * [: | |
| 18 * InputStream input = ... | |
| 19 * input.dataHandler = () { | |
| 20 * var data = input.read(); | |
| 21 * ... | |
| 22 * }; | |
| 23 * :] | |
| 24 * | |
| 25 * If for some reason the data from an input stream cannot be handled | |
| 26 * by the application immediately setting the data handler to [:null:] | |
| 27 * will avoid further callbacks until it is set to a functions | |
|
Mads Ager (google)
2011/12/14 13:36:34
a functions -> a function
Søren Gjesse
2011/12/14 13:41:12
Done.
| |
| 28 * again. While the data handler is not active system flow control | |
| 29 * will be used to avoid buffering more data than needed. | |
| 30 * | |
| 31 * Always set up appropriate handlers when using input streams. | |
| 32 */ | |
| 33 | |
| 34 /** | |
| 35 * Basic input stream which supplies binary data. | |
| 10 */ | 36 */ |
| 11 interface InputStream { | 37 interface InputStream { |
| 12 /** | 38 /** |
| 13 * Reads data from the stream. Returns a system allocated buffer | 39 * Reads data from the stream. Returns a system allocated buffer |
| 14 * with up to [len] bytes. If no value is passed for [len] all | 40 * with up to [len] bytes. If no value is passed for [len] all |
| 15 * available data will be returned. If no data is available null will | 41 * available data will be returned. If no data is available null will |
| 16 * be returned. | 42 * be returned. |
| 17 */ | 43 */ |
| 18 List<int> read([int len]); | 44 List<int> read([int len]); |
| 19 | 45 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 48 void set closeHandler(void callback()); | 74 void set closeHandler(void callback()); |
| 49 | 75 |
| 50 /** | 76 /** |
| 51 * Sets the handler that gets called when the underlying | 77 * Sets the handler that gets called when the underlying |
| 52 * communication channel gets into some kind of error situation. | 78 * communication channel gets into some kind of error situation. |
| 53 */ | 79 */ |
| 54 void set errorHandler(void callback()); | 80 void set errorHandler(void callback()); |
| 55 } | 81 } |
| 56 | 82 |
| 57 | 83 |
| 84 /** | |
| 85 * A string input stream wraps a basic input stream and supplies | |
| 86 * string data. This data can be read either as string chunks or as | |
| 87 * lines separated by line termination character sequences. | |
| 88 */ | |
| 58 interface StringInputStream factory _StringInputStream { | 89 interface StringInputStream factory _StringInputStream { |
| 59 /** | 90 /** |
| 60 * Decodes a binary input stream into characters using the specified | 91 * Decodes a binary input stream into characters using the specified |
| 61 * encoding. | 92 * encoding. |
| 62 */ | 93 */ |
| 63 StringInputStream(InputStream input, [String encoding]); | 94 StringInputStream(InputStream input, [String encoding]); |
| 64 | 95 |
| 65 /** | 96 /** |
| 66 * Reads as many characters as is available from the stream. If no data is | 97 * Reads as many characters as is available from the stream. If no data is |
| 67 * available null will be returned. | 98 * available null will be returned. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 void set closeHandler(void callback()); | 139 void set closeHandler(void callback()); |
| 109 | 140 |
| 110 /** | 141 /** |
| 111 * Sets the handler that gets called when the underlying | 142 * Sets the handler that gets called when the underlying |
| 112 * communication channel gets into some kind of error situation. | 143 * communication channel gets into some kind of error situation. |
| 113 */ | 144 */ |
| 114 void set errorHandler(void callback()); | 145 void set errorHandler(void callback()); |
| 115 } | 146 } |
| 116 | 147 |
| 117 | 148 |
| 149 /** | |
| 150 * A chunked input stream wraps a basic input stream and supplies | |
| 151 * binary data in configurable chunk sizes. | |
| 152 */ | |
| 118 interface ChunkedInputStream factory _ChunkedInputStream { | 153 interface ChunkedInputStream factory _ChunkedInputStream { |
| 119 /** | 154 /** |
| 120 * Adds buffering to an input stream and provide the ability to read | 155 * Adds buffering to an input stream and provide the ability to read |
| 121 * the data in known size chunks. | 156 * the data in known size chunks. |
| 122 */ | 157 */ |
| 123 ChunkedInputStream(InputStream input, [int chunkSize]); | 158 ChunkedInputStream(InputStream input, [int chunkSize]); |
| 124 | 159 |
| 125 /** | 160 /** |
| 126 * Reads [chunkSize] bytes from the stream. If [chunkSize] bytes are | 161 * Reads [chunkSize] bytes from the stream. If [chunkSize] bytes are |
| 127 * not currently available null is returned. When the stream is | 162 * not currently available null is returned. When the stream is |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 */ | 199 */ |
| 165 void set errorHandler(void callback()); | 200 void set errorHandler(void callback()); |
| 166 } | 201 } |
| 167 | 202 |
| 168 | 203 |
| 169 class StreamException implements Exception { | 204 class StreamException implements Exception { |
| 170 const StreamException([String this.message = ""]); | 205 const StreamException([String this.message = ""]); |
| 171 String toString() => "StreamException: $message"; | 206 String toString() => "StreamException: $message"; |
| 172 final String message; | 207 final String message; |
| 173 } | 208 } |
| OLD | NEW |