| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 class _ChunkedInputStream implements ChunkedInputStream { |
| 6 _ChunkedInputStream(InputStream this._input, [int chunkSize]) |
| 7 : _chunkSize = chunkSize, _bufferList = new _BufferList() { |
| 8 if (_chunkSize === null) { |
| 9 _chunkSize = 0; |
| 10 } |
| 11 _input.closeHandler = _closeHandler; |
| 12 } |
| 13 |
| 14 List<int> read() { |
| 15 if (_closed) return null; |
| 16 var result = _bufferList.readBytes(_chunkSize); |
| 17 if (result == null) { |
| 18 _readData(); |
| 19 result = _bufferList.readBytes(_chunkSize); |
| 20 } |
| 21 if (result == null && _inputClosed) { |
| 22 if (_bufferList.length == 0) { |
| 23 result = null; |
| 24 } else { |
| 25 result = _bufferList.readBytes(_bufferList.length); |
| 26 } |
| 27 } |
| 28 _checkInstallDataHandler(); |
| 29 return result; |
| 30 } |
| 31 |
| 32 int get chunkSize() => _chunkSize; |
| 33 |
| 34 void set chunkSize(int chunkSize) { |
| 35 _chunkSize = chunkSize; |
| 36 _checkInstallDataHandler(); |
| 37 _checkScheduleCallback(); |
| 38 } |
| 39 |
| 40 bool get closed() => _closed; |
| 41 |
| 42 void set dataHandler(void callback()) { |
| 43 _clientDataHandler = callback; |
| 44 _checkInstallDataHandler(); |
| 45 } |
| 46 |
| 47 void set closeHandler(void callback()) { |
| 48 _clientCloseHandler = callback; |
| 49 } |
| 50 |
| 51 void _dataHandler() { |
| 52 _readData(); |
| 53 if (_bufferList.length >= _chunkSize && _clientDataHandler !== null) { |
| 54 _clientDataHandler(); |
| 55 } |
| 56 _checkScheduleCallback(); |
| 57 } |
| 58 |
| 59 void _readData() { |
| 60 List<int> data = _input.read(); |
| 61 if (data !== null) { |
| 62 _bufferList.add(data); |
| 63 } |
| 64 } |
| 65 |
| 66 void _closeHandler() { |
| 67 _inputClosed = true; |
| 68 if (_bufferList.length == 0 && _clientCloseHandler) { |
| 69 _clientCloseHandler(); |
| 70 _closed = true; |
| 71 } else { |
| 72 _checkScheduleCallback(); |
| 73 } |
| 74 } |
| 75 |
| 76 void _checkInstallDataHandler() { |
| 77 if (_clientDataHandler === null) { |
| 78 _input.dataHandler = null; |
| 79 } else { |
| 80 if (_bufferList.length < _chunkSize && !_inputClosed) { |
| 81 _input.dataHandler = _dataHandler; |
| 82 } else { |
| 83 _input.dataHandler = null; |
| 84 } |
| 85 } |
| 86 } |
| 87 |
| 88 void _checkScheduleCallback() { |
| 89 // TODO(sgjesse): Find a better way of scheduling callbacks from |
| 90 // the event loop. |
| 91 void issueDataCallback(Timer timer) { |
| 92 if (_clientDataHandler !== null) { |
| 93 _clientDataHandler(); |
| 94 _checkScheduleCallback(); |
| 95 } |
| 96 } |
| 97 |
| 98 void issueCloseCallback(Timer timer) { |
| 99 if (!_closed) { |
| 100 if (_clientCloseHandler !== null) _clientCloseHandler(); |
| 101 _closed = true; |
| 102 } |
| 103 } |
| 104 |
| 105 // Schedule data callback if enough data in buffer. |
| 106 if ((_bufferList.length >=_chunkSize || |
| 107 (_bufferList.length > 0 && _inputClosed)) && |
| 108 _clientDataHandler !== null) { |
| 109 new Timer(issueDataCallback, 0, false); |
| 110 } |
| 111 |
| 112 // Schedule close callback if no more data and input is closed. |
| 113 if (_bufferList.length == 0 && _inputClosed && !_closed) { |
| 114 new Timer(issueCloseCallback, 0, false); |
| 115 } |
| 116 } |
| 117 |
| 118 InputStream _input; |
| 119 _BufferList _bufferList; |
| 120 int _chunkSize; |
| 121 bool _inputClosed = false; // Is the underlying input stream closed? |
| 122 bool _closed = false; // Has the close handler been called?. |
| 123 var _clientDataHandler; |
| 124 var _clientCloseHandler; |
| 125 } |
| OLD | NEW |