| 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 ListInputStream extends _BaseDataInputStream implements InputStream { | 5 /** |
| 6 ListInputStream(List<int> this._buffer) { | 6 * [ListInputStream] makes it possible to use the [InputStream] |
| 7 _streamMarkedClosed = true; | 7 * interface to stream over data that is received in chunks as lists |
| 8 } | 8 * of integers. |
| 9 * |
| 10 * When a new list of integers is received it can be written to the |
| 11 * [ListInputStream] using the [write] method. The [markEndOfStream] |
| 12 * method must be called when the last data has been written to the |
| 13 * [ListInputStream]. |
| 14 */ |
| 15 interface ListInputStream extends InputStream default _ListInputStream { |
| 16 /** |
| 17 * Create an empty [ListInputStream] to which data can be written |
| 18 * using the [write] method. |
| 19 */ |
| 20 ListInputStream(); |
| 9 | 21 |
| 10 int available() => _buffer.length - _offset; | 22 /** |
| 23 * Write more data to be streamed over to the [ListInputStream]. |
| 24 */ |
| 25 void write(List<int> data); |
| 11 | 26 |
| 12 List<int> _read(int bytesToRead) { | 27 /** |
| 13 if (_offset == 0 && bytesToRead == _buffer.length) { | 28 * Notify the [ListInputStream] that no more data will be written to |
| 14 _offset = _buffer.length; | 29 * it. |
| 15 return _buffer; | 30 */ |
| 16 } else { | 31 void markEndOfStream(); |
| 17 List<int> result = _buffer.getRange(_offset, bytesToRead); | |
| 18 _offset += bytesToRead; | |
| 19 return result; | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 int _readInto(List<int> buffer, int offset, int bytesToRead) { | |
| 24 buffer.setRange(offset, bytesToRead, _buffer, _offset); | |
| 25 _offset += bytesToRead; | |
| 26 return bytesToRead; | |
| 27 } | |
| 28 | |
| 29 void _close() { | |
| 30 _offset = _buffer.length; | |
| 31 } | |
| 32 | |
| 33 List<int> _buffer; | |
| 34 int _offset = 0; | |
| 35 } | 32 } |
| 36 | 33 |
| 37 | 34 |
| 38 class DynamicListInputStream | 35 /** |
| 39 extends _BaseDataInputStream implements InputStream { | 36 * [ListOutputStream] makes it possible to use the [OutputStream] |
| 40 DynamicListInputStream() : _bufferList = new _BufferList(); | 37 * interface to write data to a [List] of integers. |
| 38 */ |
| 39 interface ListOutputStream extends OutputStream default _ListOutputStream { |
| 40 /** |
| 41 * Create a [ListOutputStream]. |
| 42 */ |
| 43 ListOutputStream(); |
| 41 | 44 |
| 42 int available() => _bufferList.length; | 45 /** |
| 43 | 46 * Get the contents written to the [ListOutputStream]. |
| 44 void write(List<int> data) { | 47 */ |
| 45 if (_streamMarkedClosed) { | 48 List<int> contents(); |
| 46 throw new StreamException.streamClosed(); | |
| 47 } | |
| 48 _bufferList.add(data); | |
| 49 _checkScheduleCallbacks(); | |
| 50 } | |
| 51 | |
| 52 void markEndOfStream() { | |
| 53 _streamMarkedClosed = true; | |
| 54 _checkScheduleCallbacks(); | |
| 55 } | |
| 56 | |
| 57 List<int> _read(int bytesToRead) { | |
| 58 return _bufferList.readBytes(bytesToRead); | |
| 59 } | |
| 60 | |
| 61 int _readInto(List<int> buffer, int offset, int bytesToRead) { | |
| 62 List<int> tmp = _bufferList.readBytes(byteToRead); | |
| 63 buffer.setRange(offset, bytesToRead, tmp, _offset); | |
| 64 return bytesToRead; | |
| 65 } | |
| 66 | |
| 67 void _close() { | |
| 68 _streamMarkedClosed = true; | |
| 69 _bufferList.clear(); | |
| 70 } | |
| 71 | |
| 72 _BufferList _bufferList; | |
| 73 } | 49 } |
| 74 | |
| 75 | |
| 76 class ListOutputStream implements OutputStream { | |
| 77 ListOutputStream() : _bufferList = new _BufferList(); | |
| 78 | |
| 79 bool write(List<int> buffer, [bool copyBuffer = false]) { | |
| 80 if (_streamMarkedClosed) throw new StreamException.streamClosed(); | |
| 81 if (copyBuffer) { | |
| 82 _bufferList.add(buffer.getRange(0, buffer.length)); | |
| 83 } else { | |
| 84 _bufferList.add(buffer); | |
| 85 } | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { | |
| 90 if (_streamMarkedClosed) throw new StreamException.streamClosed(); | |
| 91 _bufferList.add( | |
| 92 buffer.getRange(offset, (len == null) ? buffer.length - offset : len)); | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 void close() { | |
| 97 if (_streamMarkedClosed) throw new StreamException.streamClosed(); | |
| 98 _streamMarkedClosed = true; | |
| 99 } | |
| 100 | |
| 101 void destroy() { | |
| 102 close(); | |
| 103 } | |
| 104 | |
| 105 void set noPendingWriteHandler(void callback()) { | |
| 106 _clientNoPendingWriteHandler = callback; | |
| 107 _checkScheduleCallbacks(); | |
| 108 } | |
| 109 | |
| 110 void set closeHandler(void callback()) { | |
| 111 _clientCloseHandler = callback; | |
| 112 } | |
| 113 | |
| 114 void set errorHandler(void callback()) { | |
| 115 // No errors emitted. | |
| 116 } | |
| 117 | |
| 118 List<int> contents() => _bufferList.readBytes(_bufferList.length); | |
| 119 | |
| 120 void _checkScheduleCallbacks() { | |
| 121 void issueNoPendingWriteCallback(Timer timer) { | |
| 122 _scheduledNoPendingWriteCallback = null; | |
| 123 if (_clientNoPendingWriteHandler !== null) { | |
| 124 _clientNoPendingWriteHandler(); | |
| 125 _checkScheduleCallbacks(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void issueCloseCallback(Timer timer) { | |
| 130 _scheduledCloseCallback = null; | |
| 131 if (_clientCloseHandler !== null) _clientCloseHandler(); | |
| 132 } | |
| 133 | |
| 134 // Schedule no pending callback if there is a callback set as this | |
| 135 // output stream does not wait for any transmission. Schedule | |
| 136 // close callback once when the stream is closed. Only schedule a | |
| 137 // new callback if the previous one has actually been called. | |
| 138 if (!_closeCallbackCalled) { | |
| 139 if (!_streamMarkedClosed) { | |
| 140 if (_clientNoPendingWriteHandler != null && | |
| 141 _scheduledNoPendingWriteCallback == null) { | |
| 142 _scheduledNoPendingWriteCallback = | |
| 143 new Timer(issueNoPendingWriteCallback, 0); | |
| 144 } | |
| 145 } else if (_clientCloseHandler != null && | |
| 146 _streamMarkedClosed && | |
| 147 !_closeCallbackCalled) { | |
| 148 _scheduledCloseCallback = new Timer(issueCloseCallback, 0); | |
| 149 _closeCallbackCalled = true; | |
| 150 } | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 _BufferList _bufferList; | |
| 155 bool _streamMarkedClosed = false; | |
| 156 bool _closeCallbackCalled = false; | |
| 157 Timer _scheduledNoPendingWriteCallback; | |
| 158 Timer _scheduledCloseCallback; | |
| 159 Function _clientNoPendingWriteHandler; | |
| 160 Function _clientCloseHandler; | |
| 161 } | |
| OLD | NEW |