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 class _BaseDataInputStream { | |
| 6 abstract int available(); | |
| 7 | |
| 8 List<int> read([int len]) { | |
| 9 if (_closeCallbackCalled) return null; | |
| 10 int bytesToRead = available(); | |
| 11 if (bytesToRead == 0) { | |
| 12 _checkScheduleCallbacks(); | |
| 13 return null; | |
| 14 } | |
| 15 if (len !== null) { | |
| 16 if (len <= 0) { | |
| 17 throw new StreamException("Illegal length $len"); | |
| 18 } else if (bytesToRead > len) { | |
| 19 bytesToRead = len; | |
| 20 } | |
| 21 } | |
| 22 return _read(bytesToRead); | |
| 23 } | |
| 24 | |
| 25 int readInto(List<int> buffer, [int offset = 0, int len]) { | |
| 26 if (_closeCallbackCalled) return null; | |
| 27 if (len === null) len = buffer.length; | |
| 28 if (offset < 0) throw new StreamException("Illegal offset $offset"); | |
| 29 if (len < 0) throw new StreamException("Illegal length $len"); | |
| 30 int bytesToRead = Math.min(len, available()); | |
| 31 return _readInto(buffer, offset, bytesToRead); | |
| 32 } | |
| 33 | |
| 34 bool get closed() => _closeCallbackCalled; | |
| 35 | |
| 36 void set dataHandler(void callback()) { | |
| 37 _clientDataHandler = callback; | |
| 38 _checkScheduleCallbacks(); | |
| 39 } | |
| 40 | |
| 41 void set closeHandler(void callback()) { | |
| 42 _clientCloseHandler = callback; | |
| 43 _checkScheduleCallbacks(); | |
| 44 } | |
| 45 | |
| 46 void set errorHandler(void callback()) { | |
| 47 // No errors emitted by default. | |
| 48 } | |
| 49 | |
| 50 abstract List<int> _read(int bytesToRead); | |
| 51 | |
| 52 void _checkScheduleCallbacks() { | |
| 53 // TODO(sgjesse): Find a better way of scheduling callbacks from | |
|
Mads Ager (google)
2011/12/19 15:04:18
This comment used to be about the Timer with a 0ms
Søren Gjesse
2011/12/20 09:45:15
Done.
| |
| 54 // the event loop. | |
| 55 void issueDataCallback(Timer timer) { | |
| 56 _scheduledDataCallback = null; | |
| 57 if (_clientDataHandler !== null) { | |
| 58 _clientDataHandler(); | |
| 59 _checkScheduleCallbacks(); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void issueCloseCallback(Timer timer) { | |
| 64 _scheduledCloseCallback = null; | |
| 65 if (_clientCloseHandler !== null) _clientCloseHandler(); | |
| 66 } | |
| 67 | |
| 68 // Schedule data callback if there is more data to read. Schedule | |
| 69 // close callback once when all data has been read. Only schedule | |
| 70 // a new callback if the previous one has actually been called. | |
| 71 if (!_closeCallbackCalled) { | |
| 72 if (available() > 0) { | |
| 73 if (_scheduledDataCallback == null) { | |
| 74 _scheduledDataCallback = new Timer(issueDataCallback, 0); | |
| 75 } | |
| 76 } else if (_streamMarkedClosed && !_closeCallbackCalled) { | |
| 77 _scheduledCloseCallback = new Timer(issueCloseCallback, 0); | |
| 78 _closeCallbackCalled = true; | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 // Stream is marked closed available is now all remaining data. | |
|
Mads Ager (google)
2011/12/19 15:04:18
Comment not completely understood. I think you sho
Søren Gjesse
2011/12/20 09:45:15
Done.
| |
| 84 bool _streamMarkedClosed = false; | |
| 85 | |
| 86 // The close callback has now been called and stream is fully closed. | |
|
Mads Ager (google)
2011/12/19 15:04:18
and stream -> and the stream
Søren Gjesse
2011/12/20 09:45:15
Done.
| |
| 87 bool _closeCallbackCalled = false; | |
| 88 | |
| 89 Timer _scheduledDataCallback; | |
| 90 Timer _scheduledCloseCallback; | |
| 91 var _clientDataHandler; | |
| 92 var _clientCloseHandler; | |
| 93 } | |
| 94 | |
| 95 | |
| 5 void _pipe(InputStream input, OutputStream output, [bool close]) { | 96 void _pipe(InputStream input, OutputStream output, [bool close]) { |
| 6 Function pipeDataHandler; | 97 Function pipeDataHandler; |
| 7 Function pipeCloseHandler; | 98 Function pipeCloseHandler; |
| 8 Function pipeNoPendingWriteHandler; | 99 Function pipeNoPendingWriteHandler; |
| 9 | 100 |
| 10 Function _inputCloseHandler; | 101 Function _inputCloseHandler; |
| 11 | 102 |
| 12 pipeDataHandler = () { | 103 pipeDataHandler = () { |
| 13 List<int> data; | 104 List<int> data; |
| 14 while ((data = input.read()) !== null) { | 105 while ((data = input.read()) !== null) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 29 input.dataHandler = pipeDataHandler; | 120 input.dataHandler = pipeDataHandler; |
| 30 output.noPendingWriteHandler = null; | 121 output.noPendingWriteHandler = null; |
| 31 }; | 122 }; |
| 32 | 123 |
| 33 _inputCloseHandler = input._clientCloseHandler; | 124 _inputCloseHandler = input._clientCloseHandler; |
| 34 input.dataHandler = pipeDataHandler; | 125 input.dataHandler = pipeDataHandler; |
| 35 input.closeHandler = pipeCloseHandler; | 126 input.closeHandler = pipeCloseHandler; |
| 36 output.noPendingWriteHandler = null; | 127 output.noPendingWriteHandler = null; |
| 37 } | 128 } |
| 38 | 129 |
| OLD | NEW |