1 /// Licensed to the Apache Software Foundation (ASF) under one
2 /// or more contributor license agreements. See the NOTICE file
3 /// distributed with this work for additional information
4 /// regarding copyright ownership. The ASF licenses this file
5 /// to you under the Apache License, Version 2.0 (the
6 /// "License"); you may not use this file except in compliance
7 /// with the License. You may obtain a copy of the License at
9 /// http://www.apache.org/licenses/LICENSE-2.0
11 /// Unless required by applicable law or agreed to in writing,
12 /// software distributed under the License is distributed on an
13 /// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 /// KIND, either express or implied. See the License for the
15 /// specific language governing permissions and limitations
16 /// under the License.
20 /// Buffered implementation of [TTransport].
21 class TBufferedTransport extends TTransport {
22 final List<int> _writeBuffer = [];
23 Iterator<int> _readIterator;
25 Uint8List consumeWriteBuffer() {
26 Uint8List buffer = new Uint8List.fromList(_writeBuffer);
31 void _setReadBuffer(Uint8List readBuffer) {
32 _readIterator = readBuffer != null ? readBuffer.iterator : null;
35 void _reset({bool isOpen: false}) {
41 bool get hasReadData => _readIterator != null;
44 bool get isOpen => _isOpen;
50 Future close() async {
51 _reset(isOpen: false);
54 int read(Uint8List buffer, int offset, int length) {
56 throw new ArgumentError.notNull("buffer");
59 if (offset + length > buffer.length) {
60 throw new ArgumentError("The range exceeds the buffer length");
63 if (_readIterator == null || length <= 0) {
68 while (i < length && _readIterator.moveNext()) {
69 buffer[offset + i] = _readIterator.current;
73 // cleanup iterator when we've reached the end
74 if (_readIterator.current == null) {
81 void write(Uint8List buffer, int offset, int length) {
83 throw new ArgumentError.notNull("buffer");
86 if (offset + length > buffer.length) {
87 throw new ArgumentError("The range exceeds the buffer length");
90 _writeBuffer.addAll(buffer.sublist(offset, offset + length));
94 _readIterator = consumeWriteBuffer().iterator;
96 return new Future.value();