| 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 abstract class _BaseDataInputStream { | 5 abstract class _BaseDataInputStream { |
| 6 int available(); | 6 int available(); |
| 7 | 7 |
| 8 List<int> read([int len]) { | 8 List<int> read([int len]) { |
| 9 if (_closeCallbackCalled || _scheduledCloseCallback != null) return null; | 9 if (_closeCallbackCalled || _scheduledCloseCallback != null) return null; |
| 10 int bytesToRead = available(); | 10 int bytesToRead = available(); |
| 11 if (bytesToRead == 0) { | 11 if (bytesToRead == 0) { |
| 12 _checkScheduleCallbacks(); | 12 _checkScheduleCallbacks(); |
| 13 return null; | 13 return null; |
| 14 } | 14 } |
| 15 if (len !== null) { | 15 if (len != null) { |
| 16 if (len <= 0) { | 16 if (len <= 0) { |
| 17 throw new StreamException("Illegal length $len"); | 17 throw new StreamException("Illegal length $len"); |
| 18 } else if (bytesToRead > len) { | 18 } else if (bytesToRead > len) { |
| 19 bytesToRead = len; | 19 bytesToRead = len; |
| 20 } | 20 } |
| 21 } | 21 } |
| 22 return _read(bytesToRead); | 22 return _read(bytesToRead); |
| 23 } | 23 } |
| 24 | 24 |
| 25 int readInto(List<int> buffer, [int offset = 0, int len]) { | 25 int readInto(List<int> buffer, [int offset = 0, int len]) { |
| 26 if (_closeCallbackCalled || _scheduledCloseCallback != null) return 0; | 26 if (_closeCallbackCalled || _scheduledCloseCallback != null) return 0; |
| 27 if (len === null) len = buffer.length; | 27 if (len == null) len = buffer.length; |
| 28 if (offset < 0) throw new StreamException("Illegal offset $offset"); | 28 if (offset < 0) throw new StreamException("Illegal offset $offset"); |
| 29 if (len < 0) throw new StreamException("Illegal length $len"); | 29 if (len < 0) throw new StreamException("Illegal length $len"); |
| 30 int bytesToRead = min(len, available()); | 30 int bytesToRead = 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 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 64 throw e; | 64 throw e; |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 | 67 |
| 68 List<int> _read(int bytesToRead); | 68 List<int> _read(int bytesToRead); |
| 69 | 69 |
| 70 void _dataReceived() { | 70 void _dataReceived() { |
| 71 // More data has been received asynchronously. Perform the data | 71 // More data has been received asynchronously. Perform the data |
| 72 // handler callback now. | 72 // handler callback now. |
| 73 _cancelScheduledDataCallback(); | 73 _cancelScheduledDataCallback(); |
| 74 if (_clientDataHandler !== null) { | 74 if (_clientDataHandler != null) { |
| 75 _clientDataHandler(); | 75 _clientDataHandler(); |
| 76 } | 76 } |
| 77 _checkScheduleCallbacks(); | 77 _checkScheduleCallbacks(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void _closeReceived() { | 80 void _closeReceived() { |
| 81 // Close indication has been received asynchronously. Perform the | 81 // Close indication has been received asynchronously. Perform the |
| 82 // close callback now if all data has been delivered. | 82 // close callback now if all data has been delivered. |
| 83 _streamMarkedClosed = true; | 83 _streamMarkedClosed = true; |
| 84 if (available() == 0) { | 84 if (available() == 0) { |
| 85 _closeCallbackCalled = true; | 85 _closeCallbackCalled = true; |
| 86 if (_clientCloseHandler !== null) _clientCloseHandler(); | 86 if (_clientCloseHandler != null) _clientCloseHandler(); |
| 87 } else { | 87 } else { |
| 88 _checkScheduleCallbacks(); | 88 _checkScheduleCallbacks(); |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 void _cancelScheduledDataCallback() { | 92 void _cancelScheduledDataCallback() { |
| 93 if (_scheduledDataCallback != null) { | 93 if (_scheduledDataCallback != null) { |
| 94 _scheduledDataCallback.cancel(); | 94 _scheduledDataCallback.cancel(); |
| 95 _scheduledDataCallback = null; | 95 _scheduledDataCallback = null; |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 void _checkScheduleCallbacks() { | 99 void _checkScheduleCallbacks() { |
| 100 void issueDataCallback(Timer timer) { | 100 void issueDataCallback(Timer timer) { |
| 101 _scheduledDataCallback = null; | 101 _scheduledDataCallback = null; |
| 102 if (_clientDataHandler !== null) { | 102 if (_clientDataHandler != null) { |
| 103 _clientDataHandler(); | 103 _clientDataHandler(); |
| 104 _checkScheduleCallbacks(); | 104 _checkScheduleCallbacks(); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 void issueCloseCallback(Timer timer) { | 108 void issueCloseCallback(Timer timer) { |
| 109 _scheduledCloseCallback = null; | 109 _scheduledCloseCallback = null; |
| 110 _closeCallbackCalled = true; | 110 _closeCallbackCalled = true; |
| 111 if (_clientCloseHandler !== null) _clientCloseHandler(); | 111 if (_clientCloseHandler != null) _clientCloseHandler(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 // Schedule data callback if there is more data to read. Schedule | 114 // Schedule data callback if there is more data to read. Schedule |
| 115 // close callback once when all data has been read. Only schedule | 115 // close callback once when all data has been read. Only schedule |
| 116 // a new callback if the previous one has actually been called. | 116 // a new callback if the previous one has actually been called. |
| 117 if (!_closeCallbackCalled) { | 117 if (!_closeCallbackCalled) { |
| 118 if (available() > 0) { | 118 if (available() > 0) { |
| 119 if (_scheduledDataCallback == null) { | 119 if (_scheduledDataCallback == null) { |
| 120 _scheduledDataCallback = new Timer(0, issueDataCallback); | 120 _scheduledDataCallback = new Timer(0, issueDataCallback); |
| 121 } | 121 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 147 | 147 |
| 148 void _pipe(InputStream input, OutputStream output, {bool close}) { | 148 void _pipe(InputStream input, OutputStream output, {bool close}) { |
| 149 Function pipeDataHandler; | 149 Function pipeDataHandler; |
| 150 Function pipeCloseHandler; | 150 Function pipeCloseHandler; |
| 151 Function pipeNoPendingWriteHandler; | 151 Function pipeNoPendingWriteHandler; |
| 152 | 152 |
| 153 Function _inputCloseHandler; | 153 Function _inputCloseHandler; |
| 154 | 154 |
| 155 pipeDataHandler = () { | 155 pipeDataHandler = () { |
| 156 List<int> data; | 156 List<int> data; |
| 157 while ((data = input.read()) !== null) { | 157 while ((data = input.read()) != null) { |
| 158 if (!output.write(data)) { | 158 if (!output.write(data)) { |
| 159 input.onData = null; | 159 input.onData = null; |
| 160 output.onNoPendingWrites = pipeNoPendingWriteHandler; | 160 output.onNoPendingWrites = pipeNoPendingWriteHandler; |
| 161 break; | 161 break; |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 }; | 164 }; |
| 165 | 165 |
| 166 pipeCloseHandler = () { | 166 pipeCloseHandler = () { |
| 167 if (close) output.close(); | 167 if (close) output.close(); |
| 168 if (_inputCloseHandler !== null) { | 168 if (_inputCloseHandler != null) { |
| 169 _inputCloseHandler(); | 169 _inputCloseHandler(); |
| 170 } | 170 } |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 pipeNoPendingWriteHandler = () { | 173 pipeNoPendingWriteHandler = () { |
| 174 input.onData = pipeDataHandler; | 174 input.onData = pipeDataHandler; |
| 175 output.onNoPendingWrites = null; | 175 output.onNoPendingWrites = null; |
| 176 }; | 176 }; |
| 177 | 177 |
| 178 _inputCloseHandler = input._clientCloseHandler; | 178 _inputCloseHandler = input._clientCloseHandler; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 200 void _reportError(e) { | 200 void _reportError(e) { |
| 201 if (_onError != null) { | 201 if (_onError != null) { |
| 202 _onError(e); | 202 _onError(e); |
| 203 } else { | 203 } else { |
| 204 throw e; | 204 throw e; |
| 205 } | 205 } |
| 206 } | 206 } |
| 207 | 207 |
| 208 Function _onError; | 208 Function _onError; |
| 209 } | 209 } |
| OLD | NEW |