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 * Basic input stream which supplies binary data. | |
7 * | |
8 * Input streams are used to read data sequentially from some data | |
9 * source. All input streams are non-blocking. They each have a number | |
10 * of read calls which will always return without any IO related | |
11 * blocking. If the requested data is not available a read call will | |
12 * return `null`. All input streams have one or more handlers which | |
13 * will trigger when data is available. | |
14 * | |
15 * The following example shows a data handler in an ordinary input | |
16 * stream which will be called when some data is available and a call | |
17 * to read will not return `null`. | |
18 * | |
19 * InputStream input = ... | |
20 * input.onData = () { | |
21 * var data = input.read(); | |
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 function | |
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 abstract class InputStream { | |
35 /** | |
36 * Reads data from the stream. Returns a system allocated buffer | |
37 * with up to [len] bytes. If no value is passed for [len] all | |
38 * available data will be returned. If no data is available null will | |
39 * be returned. | |
40 */ | |
41 List<int> read([int len]); | |
42 | |
43 /** | |
44 * Reads up to [len] bytes into buffer [buffer] starting at offset | |
45 * [offset]. Returns the number of bytes actually read which might | |
46 * be zero. If [offset] is not specified 0 is used. If [len] is not | |
47 * specified the length of [buffer] is used. | |
48 */ | |
49 int readInto(List<int> buffer, [int offset, int len]); | |
50 | |
51 /** | |
52 * Returns the number of bytes available for immediate reading. | |
53 */ | |
54 int available(); | |
55 | |
56 /** | |
57 * Pipe the content of this input stream directly to the output | |
58 * stream [output]. The default behavior is to close the output when | |
59 * all the data from the input stream have been written. Specifying | |
60 * `false` for the optional argument [close] keeps the output | |
61 * stream open after writing all data from the input stream. | |
62 */ | |
63 void pipe(OutputStream output, [bool close = true]); | |
64 | |
65 /** | |
66 * Close the underlying communication channel to avoid getting any | |
67 * more data. In normal situations, where all data is read from the | |
68 * stream until the close handler is called, calling [close] is not | |
69 * required. When [close] is used the close handler will still be | |
70 * called. | |
71 */ | |
72 void close(); | |
73 | |
74 /** | |
75 * Returns whether the stream is closed. There will be no more data | |
76 * to read. | |
77 */ | |
78 bool get closed; | |
79 | |
80 /** | |
81 * Sets the handler that gets called when data is available. | |
82 */ | |
83 void set onData(void callback()); | |
84 | |
85 /** | |
86 * Sets the handler that gets called when there will be no more data | |
87 * available in the stream. | |
88 */ | |
89 void set onClosed(void callback()); | |
90 | |
91 /** | |
92 * Sets the handler that gets called when the underlying | |
93 * communication channel gets into some kind of error situation. | |
94 */ | |
95 void set onError(void callback(e)); | |
96 } | |
97 | |
98 | |
99 /** | |
100 * String encodings. | |
101 */ | |
102 class Encoding { | |
103 static const Encoding UTF_8 = const Encoding._internal("UTF-8"); | |
104 static const Encoding ISO_8859_1 = const Encoding._internal("ISO-8859-1"); | |
105 static const Encoding ASCII = const Encoding._internal("ASCII"); | |
106 const Encoding._internal(String this.name); | |
107 final String name; | |
108 } | |
109 | |
110 | |
111 /** | |
112 * A string input stream wraps a basic input stream and supplies | |
113 * string data. This data can be read either as string chunks or as | |
114 * lines separated by line termination character sequences. | |
115 */ | |
116 abstract class StringInputStream { | |
117 /** | |
118 * Decodes a binary input stream into characters using the specified | |
119 * encoding. | |
120 */ | |
121 factory StringInputStream(InputStream input, | |
122 [Encoding encoding = Encoding.UTF_8]) { | |
123 return new _StringInputStream(input, encoding); | |
124 } | |
125 | |
126 /** | |
127 * Reads up to [len] characters from the stream. if [len] is not | |
128 * specified reads as many characters as is available from the | |
129 * stream. If no data is available null will be returned. | |
130 */ | |
131 String read([int len]); | |
132 | |
133 /** | |
134 * Reads the next line from the stream. The line ending characters | |
135 * will not be part of the returned string. If a full line is not | |
136 * available null will be returned. | |
137 */ | |
138 String readLine(); | |
139 | |
140 /** | |
141 * Returns the number of characters available for immediate | |
142 * reading. Note that this includes all characters that will be in | |
143 * the String returned from [read] this includes line breaking | |
144 * characters. If [readLine] is used for reading one can observe | |
145 * less characters being returned as the line breaking characters | |
146 * are discarded. | |
147 */ | |
148 int available(); | |
149 | |
150 /** | |
151 * Returns whether the stream has been closed. There might still be | |
152 * more data to read. | |
153 */ | |
154 bool get closed; | |
155 | |
156 /** | |
157 * Returns the encoding used to decode the binary data into characters. | |
158 */ | |
159 Encoding get encoding; | |
160 | |
161 /** | |
162 * Sets the handler that gets called when data is available. The two | |
163 * handlers [onData] and [onLine] are mutually exclusive | |
164 * and setting one will remove the other. | |
165 */ | |
166 void set onData(void callback()); | |
167 | |
168 /** | |
169 * Sets the handler that gets called when a line is available. The | |
170 * two handlers [onData] and [onLine] are mutually | |
171 * exclusive and setting one will remove the other. | |
172 */ | |
173 void set onLine(void callback()); | |
174 | |
175 /** | |
176 * Sets the handler that gets called when there will be no more data | |
177 * available in the stream. | |
178 */ | |
179 void set onClosed(void callback()); | |
180 | |
181 /** | |
182 * Sets the handler that gets called when the underlying | |
183 * communication channel gets into some kind of error situation. | |
184 */ | |
185 void set onError(void callback(e)); | |
186 } | |
187 | |
188 | |
189 /** | |
190 * A chunked input stream wraps a basic input stream and supplies | |
191 * binary data in configurable chunk sizes. | |
192 */ | |
193 abstract class ChunkedInputStream { | |
194 /** | |
195 * Adds buffering to an input stream and provide the ability to read | |
196 * the data in known size chunks. | |
197 */ | |
198 factory ChunkedInputStream(InputStream input, [int chunkSize = 0]) { | |
199 return new _ChunkedInputStream(input, chunkSize); | |
200 } | |
201 | |
202 /** | |
203 * Reads [chunkSize] bytes from the stream. If [chunkSize] bytes are | |
204 * not currently available null is returned. When the stream is | |
205 * closed the last call can return with less than [chunkSize] bytes. | |
206 */ | |
207 List<int> read(); | |
208 | |
209 /** | |
210 * Returns whether the stream has been closed. There might still be | |
211 * more data to read. | |
212 */ | |
213 bool get closed; | |
214 | |
215 /** | |
216 * Returns the chunk size used by this stream. | |
217 */ | |
218 int get chunkSize; | |
219 | |
220 /** | |
221 * Sets the chunk size used by this stream. | |
222 */ | |
223 void set chunkSize(int chunkSize); | |
224 | |
225 /** | |
226 * Sets the handler that gets called when at least [chunkSize] bytes | |
227 * of data is available or the underlying stream has been closed and | |
228 * there is still unread data. | |
229 */ | |
230 void set onData(void callback()); | |
231 | |
232 /** | |
233 * Sets the handler that gets called when there will be no more data | |
234 * available in the stream. | |
235 */ | |
236 void set onClosed(void callback()); | |
237 | |
238 /** | |
239 * Sets the handler that gets called when the underlying | |
240 * communication channel gets into some kind of error situation. | |
241 */ | |
242 void set onError(void callback(e)); | |
243 } | |
244 | |
245 | |
246 class StreamException implements Exception { | |
247 const StreamException([String this.message = ""]); | |
248 const StreamException.streamClosed() : message = "Stream closed"; | |
249 String toString() => "StreamException: $message"; | |
250 final String message; | |
251 } | |
OLD | NEW |