Chromium Code Reviews| 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 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 || _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) 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 = 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 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 void issueDataCallback(Timer timer) { | 102 void issueDataCallback(Timer timer) { |
| 103 _scheduledDataCallback = null; | 103 _scheduledDataCallback = null; |
| 104 if (_clientDataHandler !== null) { | 104 if (_clientDataHandler !== null) { |
| 105 _clientDataHandler(); | 105 _clientDataHandler(); |
| 106 _checkScheduleCallbacks(); | 106 _checkScheduleCallbacks(); |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | 109 |
| 110 void issueCloseCallback(Timer timer) { | 110 void issueCloseCallback(Timer timer) { |
| 111 _scheduledCloseCallback = null; | 111 _scheduledCloseCallback = null; |
| 112 _closeCallbackCalled = true; | |
| 112 if (_clientCloseHandler !== null) _clientCloseHandler(); | 113 if (_clientCloseHandler !== null) _clientCloseHandler(); |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Schedule data callback if there is more data to read. Schedule | 116 // Schedule data callback if there is more data to read. Schedule |
| 116 // close callback once when all data has been read. Only schedule | 117 // close callback once when all data has been read. Only schedule |
| 117 // a new callback if the previous one has actually been called. | 118 // a new callback if the previous one has actually been called. |
| 118 if (!_closeCallbackCalled) { | 119 if (!_closeCallbackCalled) { |
| 119 if (available() > 0) { | 120 if (available() > 0) { |
| 120 if (_scheduledDataCallback == null) { | 121 if (_scheduledDataCallback == null) { |
| 121 _scheduledDataCallback = new Timer(0, issueDataCallback); | 122 _scheduledDataCallback = new Timer(0, issueDataCallback); |
| 122 } | 123 } |
| 123 } else if (_streamMarkedClosed && !_closeCallbackCalled) { | 124 } else if (_streamMarkedClosed && |
| 125 _scheduledCloseCallback == null) { | |
|
Anders Johnsen
2012/06/14 11:24:07
Can this be on one line?
Bill Hesse
2012/06/14 11:41:00
Yes.
| |
| 124 _cancelScheduledDataCallback(); | 126 _cancelScheduledDataCallback(); |
| 125 _close(); | 127 _close(); |
| 126 _scheduledCloseCallback = new Timer(0, issueCloseCallback); | 128 _scheduledCloseCallback = new Timer(0, issueCloseCallback); |
| 127 _closeCallbackCalled = true; | |
| 128 } | 129 } |
| 129 } | 130 } |
| 130 } | 131 } |
| 131 | 132 |
| 132 // When this is set to true the stream is marked closed. When a | 133 // When this is set to true the stream is marked closed. When a |
| 133 // stream is marked closed no more data can arrive and the value | 134 // stream is marked closed no more data can arrive and the value |
| 134 // from available is now all remaining data. If this is true and the | 135 // from available is now all remaining data. If this is true and the |
| 135 // value of available is zero the close handler is called. | 136 // value of available is zero the close handler is called. |
| 136 bool _streamMarkedClosed = false; | 137 bool _streamMarkedClosed = false; |
| 137 | 138 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 void _reportError(e) { | 203 void _reportError(e) { |
| 203 if (_onError != null) { | 204 if (_onError != null) { |
| 204 _onError(e); | 205 _onError(e); |
| 205 } else { | 206 } else { |
| 206 throw e; | 207 throw e; |
| 207 } | 208 } |
| 208 } | 209 } |
| 209 | 210 |
| 210 Function _onError; | 211 Function _onError; |
| 211 } | 212 } |
| OLD | NEW |