| OLD | NEW |
| 1 // Copyright (c) 2011, 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 class _BaseDataInputStream { | 5 class _BaseDataInputStream { |
| 6 abstract int available(); | 6 abstract int available(); |
| 7 | 7 |
| 8 List<int> read([int len]) { | 8 List<int> read([int len]) { |
| 9 if (_closeCallbackCalled) return null; | 9 if (_closeCallbackCalled) return null; |
| 10 int bytesToRead = available(); | 10 int bytesToRead = available(); |
| 11 if (bytesToRead == 0) { | 11 if (bytesToRead == 0) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 29 if (len < 0) throw new StreamException("Illegal length $len"); | 29 if (len < 0) throw new StreamException("Illegal length $len"); |
| 30 int bytesToRead = Math.min(len, available()); | 30 int bytesToRead = Math.min(len, available()); |
| 31 return _readInto(buffer, offset, bytesToRead); | 31 return _readInto(buffer, offset, bytesToRead); |
| 32 } | 32 } |
| 33 | 33 |
| 34 void pipe(OutputStream output, [bool close = true]) { | 34 void pipe(OutputStream output, [bool close = true]) { |
| 35 _pipe(this, output, close: close); | 35 _pipe(this, output, close: close); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void close() { | 38 void close() { |
| 39 if (_scheduledDataCallback != null) _scheduledDataCallback.cancel(); | 39 if (_scheduledDataCallback != null) { |
| 40 _scheduledDataCallback.cancel(); |
| 41 } |
| 40 _close(); | 42 _close(); |
| 41 _checkScheduleCallbacks(); | 43 _checkScheduleCallbacks(); |
| 42 } | 44 } |
| 43 | 45 |
| 44 bool get closed() => _closeCallbackCalled; | 46 bool get closed() => _closeCallbackCalled; |
| 45 | 47 |
| 46 void set dataHandler(void callback()) { | 48 void set dataHandler(void callback()) { |
| 47 _clientDataHandler = callback; | 49 _clientDataHandler = callback; |
| 48 _checkScheduleCallbacks(); | 50 _checkScheduleCallbacks(); |
| 49 } | 51 } |
| 50 | 52 |
| 51 void set closeHandler(void callback()) { | 53 void set closeHandler(void callback()) { |
| 52 _clientCloseHandler = callback; | 54 _clientCloseHandler = callback; |
| 53 _checkScheduleCallbacks(); | 55 _checkScheduleCallbacks(); |
| 54 } | 56 } |
| 55 | 57 |
| 56 void set errorHandler(void callback()) { | 58 void set errorHandler(void callback()) { |
| 57 // No errors emitted by default. | 59 _clientErrorHandler = callback; |
| 58 } | 60 } |
| 59 | 61 |
| 60 abstract List<int> _read(int bytesToRead); | 62 abstract List<int> _read(int bytesToRead); |
| 61 | 63 |
| 62 void _checkScheduleCallbacks() { | 64 void _checkScheduleCallbacks() { |
| 63 void issueDataCallback(Timer timer) { | 65 void issueDataCallback(Timer timer) { |
| 64 _scheduledDataCallback = null; | 66 _scheduledDataCallback = null; |
| 65 if (_clientDataHandler !== null) { | 67 if (_clientDataHandler !== null) { |
| 66 _clientDataHandler(); | 68 _clientDataHandler(); |
| 67 _checkScheduleCallbacks(); | 69 _checkScheduleCallbacks(); |
| 68 } | 70 } |
| 69 } | 71 } |
| 70 | 72 |
| 71 void issueCloseCallback(Timer timer) { | 73 void issueCloseCallback(Timer timer) { |
| 72 _scheduledCloseCallback = null; | 74 _scheduledCloseCallback = null; |
| 73 if (_clientCloseHandler !== null) _clientCloseHandler(); | 75 if (_clientCloseHandler !== null) _clientCloseHandler(); |
| 74 } | 76 } |
| 75 | 77 |
| 76 // Schedule data callback if there is more data to read. Schedule | 78 // Schedule data callback if there is more data to read. Schedule |
| 77 // close callback once when all data has been read. Only schedule | 79 // close callback once when all data has been read. Only schedule |
| 78 // a new callback if the previous one has actually been called. | 80 // a new callback if the previous one has actually been called. |
| 79 if (!_closeCallbackCalled) { | 81 if (!_closeCallbackCalled) { |
| 80 if (available() > 0) { | 82 if (available() > 0) { |
| 81 if (_scheduledDataCallback == null) { | 83 if (_scheduledDataCallback == null) { |
| 82 _scheduledDataCallback = new Timer(issueDataCallback, 0); | 84 _scheduledDataCallback = new Timer(issueDataCallback, 0); |
| 83 } | 85 } |
| 84 } else if (_streamMarkedClosed && !_closeCallbackCalled) { | 86 } else if (_streamMarkedClosed && !_closeCallbackCalled) { |
| 87 if (_scheduledDataCallback != null) { |
| 88 _scheduledDataCallback.cancel(); |
| 89 } |
| 85 _close(); | 90 _close(); |
| 86 _scheduledCloseCallback = new Timer(issueCloseCallback, 0); | 91 _scheduledCloseCallback = new Timer(issueCloseCallback, 0); |
| 87 _closeCallbackCalled = true; | 92 _closeCallbackCalled = true; |
| 88 } | 93 } |
| 89 } | 94 } |
| 90 } | 95 } |
| 91 | 96 |
| 92 // When this is set to true the stream is marked closed. When a | 97 // When this is set to true the stream is marked closed. When a |
| 93 // stream is marked closed no more data can arrive and the value | 98 // stream is marked closed no more data can arrive and the value |
| 94 // from available is now all remaining data. If this is true and the | 99 // from available is now all remaining data. If this is true and the |
| 95 // value of available is zero the close handler is called. | 100 // value of available is zero the close handler is called. |
| 96 bool _streamMarkedClosed = false; | 101 bool _streamMarkedClosed = false; |
| 97 | 102 |
| 98 // When this is set to true the close callback has been called and | 103 // When this is set to true the close callback has been called and |
| 99 // the stream is fully closed. | 104 // the stream is fully closed. |
| 100 bool _closeCallbackCalled = false; | 105 bool _closeCallbackCalled = false; |
| 101 | 106 |
| 102 Timer _scheduledDataCallback; | 107 Timer _scheduledDataCallback; |
| 103 Timer _scheduledCloseCallback; | 108 Timer _scheduledCloseCallback; |
| 104 Function _clientDataHandler; | 109 Function _clientDataHandler; |
| 105 Function _clientCloseHandler; | 110 Function _clientCloseHandler; |
| 111 Function _clientErrorHandler; |
| 106 } | 112 } |
| 107 | 113 |
| 108 | 114 |
| 109 void _pipe(InputStream input, OutputStream output, [bool close]) { | 115 void _pipe(InputStream input, OutputStream output, [bool close]) { |
| 110 Function pipeDataHandler; | 116 Function pipeDataHandler; |
| 111 Function pipeCloseHandler; | 117 Function pipeCloseHandler; |
| 112 Function pipeNoPendingWriteHandler; | 118 Function pipeNoPendingWriteHandler; |
| 113 | 119 |
| 114 Function _inputCloseHandler; | 120 Function _inputCloseHandler; |
| 115 | 121 |
| 116 pipeDataHandler = () { | 122 pipeDataHandler = () { |
| 117 List<int> data; | 123 List<int> data; |
| 118 while ((data = input.read()) !== null) { | 124 while ((data = input.read()) !== null) { |
| 119 if (!output.write(data)) { | 125 if (!output.write(data)) { |
| 120 input.dataHandler = null; | 126 input.dataHandler = null; |
| 121 output.noPendingWriteHandler = pipeNoPendingWriteHandler; | 127 output.noPendingWriteHandler = pipeNoPendingWriteHandler; |
| 122 break; | 128 break; |
| 123 } | 129 } |
| 124 } | 130 } |
| 125 }; | 131 }; |
| 126 | 132 |
| 127 pipeCloseHandler = () { | 133 pipeCloseHandler = () { |
| 128 if (close) output.close(); | 134 if (close) output.close(); |
| 129 if (_inputCloseHandler !== null) _inputCloseHandler(); | 135 if (_inputCloseHandler !== null) { |
| 136 _inputCloseHandler(); |
| 137 } |
| 130 }; | 138 }; |
| 131 | 139 |
| 132 pipeNoPendingWriteHandler = () { | 140 pipeNoPendingWriteHandler = () { |
| 133 input.dataHandler = pipeDataHandler; | 141 input.dataHandler = pipeDataHandler; |
| 134 output.noPendingWriteHandler = null; | 142 output.noPendingWriteHandler = null; |
| 135 }; | 143 }; |
| 136 | 144 |
| 137 _inputCloseHandler = input._clientCloseHandler; | 145 _inputCloseHandler = input._clientCloseHandler; |
| 138 input.dataHandler = pipeDataHandler; | 146 input.dataHandler = pipeDataHandler; |
| 139 input.closeHandler = pipeCloseHandler; | 147 input.closeHandler = pipeCloseHandler; |
| 140 output.noPendingWriteHandler = null; | 148 output.noPendingWriteHandler = null; |
| 141 } | 149 } |
| 142 | 150 |
| OLD | NEW |