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 // Interface for decoders decoding binary data into string data. The | 5 // Interface for decoders decoding binary data into string data. The |
6 // decoder keeps track of line breaks during decoding. | 6 // decoder keeps track of line breaks during decoding. |
7 interface _StringDecoder { | 7 interface _StringDecoder { |
8 // Add more binary data to be decoded. The ownership of the buffer | 8 // Add more binary data to be decoded. The ownership of the buffer |
9 // is transfered to the decoder and the caller most not modify it any more. | 9 // is transfered to the decoder and the caller most not modify it any more. |
10 int write(List<int> buffer); | 10 int write(List<int> buffer); |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 } | 220 } |
221 if (_encoding == "UTF-8") { | 221 if (_encoding == "UTF-8") { |
222 _decoder = new _UTF8Decoder(); | 222 _decoder = new _UTF8Decoder(); |
223 } else if (_encoding == "ISO-8859-1") { | 223 } else if (_encoding == "ISO-8859-1") { |
224 _decoder = new _Latin1Decoder(); | 224 _decoder = new _Latin1Decoder(); |
225 } else if (_encoding == "ASCII") { | 225 } else if (_encoding == "ASCII") { |
226 _decoder = new _AsciiDecoder(); | 226 _decoder = new _AsciiDecoder(); |
227 } else { | 227 } else { |
228 throw new StreamException("Unsupported encoding $_encoding"); | 228 throw new StreamException("Unsupported encoding $_encoding"); |
229 } | 229 } |
230 _input.dataHandler = _dataHandler; | 230 _input.onData = _onData; |
231 _input.closeHandler = _closeHandler; | 231 _input.onClosed = _onClosed; |
232 } | 232 } |
233 | 233 |
234 String read() { | 234 String read() { |
235 String result = _decoder.decoded; | 235 String result = _decoder.decoded; |
236 _checkInstallDataHandler(); | 236 _checkInstallDataHandler(); |
237 return result; | 237 return result; |
238 } | 238 } |
239 | 239 |
240 String readLine() { | 240 String readLine() { |
241 String decodedLine = _decoder.decodedLine; | 241 String decodedLine = _decoder.decodedLine; |
242 if (decodedLine == null) { | 242 if (decodedLine == null) { |
243 if (_inputClosed) { | 243 if (_inputClosed) { |
244 // Last line might not have a line separator. | 244 // Last line might not have a line separator. |
245 decodedLine = _decoder.decoded; | 245 decodedLine = _decoder.decoded; |
246 if (decodedLine != null && | 246 if (decodedLine != null && |
247 decodedLine[decodedLine.length - 1] == '\r') { | 247 decodedLine[decodedLine.length - 1] == '\r') { |
248 decodedLine = decodedLine.substring(0, decodedLine.length - 1); | 248 decodedLine = decodedLine.substring(0, decodedLine.length - 1); |
249 } | 249 } |
250 } | 250 } |
251 } | 251 } |
252 _checkInstallDataHandler(); | 252 _checkInstallDataHandler(); |
253 return decodedLine; | 253 return decodedLine; |
254 } | 254 } |
255 | 255 |
256 String get encoding() => _encoding; | 256 String get encoding() => _encoding; |
257 | 257 |
258 bool get closed() => _inputClosed && _decoder.isEmpty(); | 258 bool get closed() => _inputClosed && _decoder.isEmpty(); |
259 | 259 |
260 void set dataHandler(void callback()) { | 260 void set onData(void callback()) { |
261 _clientDataHandler = callback; | 261 _clientDataHandler = callback; |
262 _clientLineHandler = null; | 262 _clientLineHandler = null; |
263 _checkInstallDataHandler(); | 263 _checkInstallDataHandler(); |
264 _checkScheduleCallback(); | 264 _checkScheduleCallback(); |
265 } | 265 } |
266 | 266 |
267 void set lineHandler(void callback()) { | 267 void set onLine(void callback()) { |
268 _clientLineHandler = callback; | 268 _clientLineHandler = callback; |
269 _clientDataHandler = null; | 269 _clientDataHandler = null; |
270 _checkInstallDataHandler(); | 270 _checkInstallDataHandler(); |
271 _checkScheduleCallback(); | 271 _checkScheduleCallback(); |
272 } | 272 } |
273 | 273 |
274 void set closeHandler(void callback()) { | 274 void set onClosed(void callback()) { |
275 _clientCloseHandler = callback; | 275 _clientCloseHandler = callback; |
276 } | 276 } |
277 | 277 |
278 void set errorHandler(void callback()) { | 278 void set onError(void callback()) { |
279 _input.errorHandler = callback; | 279 _input.errorHandler = callback; |
280 } | 280 } |
281 | 281 |
282 void _dataHandler() { | 282 void _onData() { |
283 _readData(); | 283 _readData(); |
284 if (!_decoder.isEmpty() && _clientDataHandler !== null) { | 284 if (!_decoder.isEmpty() && _clientDataHandler !== null) { |
285 _clientDataHandler(); | 285 _clientDataHandler(); |
286 } | 286 } |
287 if (_decoder.lineBreaks > 0 && _clientLineHandler !== null) { | 287 if (_decoder.lineBreaks > 0 && _clientLineHandler !== null) { |
288 _clientLineHandler(); | 288 _clientLineHandler(); |
289 } | 289 } |
290 _checkScheduleCallback(); | 290 _checkScheduleCallback(); |
291 _checkInstallDataHandler(); | 291 _checkInstallDataHandler(); |
292 } | 292 } |
293 | 293 |
294 void _closeHandler() { | 294 void _onClosed() { |
295 _inputClosed = true; | 295 _inputClosed = true; |
296 if (_decoder.isEmpty() && _clientCloseHandler != null) { | 296 if (_decoder.isEmpty() && _clientCloseHandler != null) { |
297 _clientCloseHandler(); | 297 _clientCloseHandler(); |
298 _closed = true; | 298 _closed = true; |
299 } else { | 299 } else { |
300 _checkScheduleCallback(); | 300 _checkScheduleCallback(); |
301 } | 301 } |
302 } | 302 } |
303 | 303 |
304 void _readData() { | 304 void _readData() { |
305 List<int> data = _input.read(); | 305 List<int> data = _input.read(); |
306 if (data !== null) { | 306 if (data !== null) { |
307 _decoder.write(data); | 307 _decoder.write(data); |
308 } | 308 } |
309 } | 309 } |
310 | 310 |
311 void _checkInstallDataHandler() { | 311 void _checkInstallDataHandler() { |
312 if (_inputClosed || | 312 if (_inputClosed || |
313 (_clientDataHandler === null && _clientLineHandler === null)) { | 313 (_clientDataHandler === null && _clientLineHandler === null)) { |
314 _input.dataHandler = null; | 314 _input.onData = null; |
315 } else if (_clientDataHandler !== null) { | 315 } else if (_clientDataHandler !== null) { |
316 if (_decoder.isEmpty()) { | 316 if (_decoder.isEmpty()) { |
317 _input.dataHandler = _dataHandler; | 317 _input.onData = _onData; |
318 } else { | 318 } else { |
319 _input.dataHandler = null; | 319 _input.onData = null; |
320 } | 320 } |
321 } else { | 321 } else { |
322 assert(_clientLineHandler !== null); | 322 assert(_clientLineHandler !== null); |
323 if (_decoder.lineBreaks == 0) { | 323 if (_decoder.lineBreaks == 0) { |
324 _input.dataHandler = _dataHandler; | 324 _input.onData = _onData; |
325 } else { | 325 } else { |
326 _input.dataHandler = null; | 326 _input.onData = null; |
327 } | 327 } |
328 } | 328 } |
329 } | 329 } |
330 | 330 |
331 // TODO(sgjesse): Find a better way of scheduling callbacks from | 331 // TODO(sgjesse): Find a better way of scheduling callbacks from |
332 // the event loop. | 332 // the event loop. |
333 void _checkScheduleCallback() { | 333 void _checkScheduleCallback() { |
334 void issueDataCallback(Timer timer) { | 334 void issueDataCallback(Timer timer) { |
335 _scheduledDataCallback = null; | 335 _scheduledDataCallback = null; |
336 if (_clientDataHandler !== null) { | 336 if (_clientDataHandler !== null) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 bool _inputClosed = false; // Is the underlying input stream closed? | 391 bool _inputClosed = false; // Is the underlying input stream closed? |
392 bool _closed = false; // Is this stream closed. | 392 bool _closed = false; // Is this stream closed. |
393 bool _eof = false; // Has all data been read from the decoder? | 393 bool _eof = false; // Has all data been read from the decoder? |
394 Timer _scheduledDataCallback; | 394 Timer _scheduledDataCallback; |
395 Timer _scheduledLineCallback; | 395 Timer _scheduledLineCallback; |
396 Timer _scheduledCloseCallback; | 396 Timer _scheduledCloseCallback; |
397 Function _clientDataHandler; | 397 Function _clientDataHandler; |
398 Function _clientLineHandler; | 398 Function _clientLineHandler; |
399 Function _clientCloseHandler; | 399 Function _clientCloseHandler; |
400 } | 400 } |
OLD | NEW |