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 is read from a given input stream. Such an input stream can |
7 * be an endpoint, e.g., a socket or a file, or another input stream. | 7 * be an endpoint, e.g., a socket or a file, or another input stream. |
8 * Multiple input streams can be chained together to operate collaboratively | 8 * Multiple input streams can be chained together to operate collaboratively |
9 * on a given input. | 9 * on a given input. |
10 */ | 10 */ |
11 interface InputStream { | 11 interface InputStream { |
12 /** | 12 /** |
13 * Reads [len] bytes into [buffer] buffer starting at [offset] offset. | 13 * Reads [len] bytes into [buffer] buffer starting at [offset] offset. |
14 * [callback] callback is invoked on completion unless it is null. | 14 * [callback] callback is invoked on completion unless it is null. |
15 */ | 15 */ |
16 bool read(List<int> buffer, int offset, int len, void callback()); | 16 bool read(List<int> buffer, int offset, int len, void callback()); |
17 | 17 |
18 /** | 18 /** |
19 * Reads data from the stream into a buffer until a given [pattern] occurs and | 19 * Reads data from the stream into a buffer until a given [pattern] occurs and |
20 * hands that buffer over as an input to the registered [callback]. | 20 * hands that buffer over as an input to the registered [callback]. |
21 * The callback is not invoked if a read error occurs. | 21 * The callback is not invoked if a read error occurs. |
22 */ | 22 */ |
23 void readUntil(List<int> pattern, void callback(List<int> buffer)); | 23 void readUntil(List<int> pattern, void callback(List<int> buffer)); |
24 } | 24 } |
| 25 |
| 26 |
| 27 interface InputStream2 { |
| 28 /** |
| 29 * Reads as much data as is available from the stream. If no data is |
| 30 * available null will be returned. |
| 31 */ |
| 32 List<int> read(); |
| 33 |
| 34 /** |
| 35 * The data handler gets called when data is available. |
| 36 */ |
| 37 void set dataHandler(void callback()); |
| 38 |
| 39 /** |
| 40 * The close handler gets called when the underlying communication |
| 41 * channel is closed and no more data will become available. Not all |
| 42 * types of communication channels will emit close events. |
| 43 */ |
| 44 void set closeHandler(void callback()); |
| 45 |
| 46 /** |
| 47 * The error handler gets called when the underlying communication |
| 48 * channel gets into some kind of error situation. |
| 49 */ |
| 50 void set errorHandler(void callback(int error)); |
| 51 } |
| 52 |
| 53 |
| 54 interface StringInputStream factory _StringInputStream { |
| 55 /** |
| 56 * Decodes a binary input stream into characters using the specified |
| 57 * encoding. |
| 58 */ |
| 59 StringInputStream(InputStream2 input, [String encoding]); |
| 60 |
| 61 /** |
| 62 * Reads as many characters as is available from the stream. If no data is |
| 63 * available null will be returned. |
| 64 */ |
| 65 String read(); |
| 66 |
| 67 /** |
| 68 * Returns the encoding used to decode the binary data into characters. |
| 69 */ |
| 70 String get encoding(); |
| 71 |
| 72 /** |
| 73 * The data handler gets called when data is available. |
| 74 */ |
| 75 void set dataHandler(void callback()); |
| 76 } |
| 77 |
| 78 |
| 79 class StreamException implements Exception { |
| 80 const StreamException([String this.message = ""]); |
| 81 String toString() => "StreamException: $message"; |
| 82 |
| 83 /* |
| 84 * Contains the exception message. |
| 85 */ |
| 86 final String message; |
| 87 } |
OLD | NEW |