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 /** |
| 6 * The PbReader reads bytes from some source, careful not to read more bytes |
| 7 * than requested on destructive reads. |
| 8 */ |
| 9 interface PbReader { |
| 10 /** |
| 11 * Returns whether more data may be available on this PbReader. |
| 12 */ |
| 13 bool get closed(); |
| 14 |
| 15 /** |
| 16 * Provides the position within the current PbReader. |
| 17 */ |
| 18 int get position(); |
| 19 |
| 20 /** |
| 21 * Reads a single byte and makes it available as a Future. |
| 22 */ |
| 23 Future<int> readByte(); |
| 24 |
| 25 /** |
| 26 * Reads the bytes requested and makes them available as a Future. |
| 27 * Unspecified byte count instructs the method to read to the end. |
| 28 */ |
| 29 Future<List<int>> readBytes([int count]); |
| 30 |
| 31 /** |
| 32 * Permits reads beyond current readBytes target for efficiency |
| 33 */ |
| 34 void setReadAhead([int count]); |
| 35 } |
| 36 |
| 37 class PbBufferReader { |
| 38 PbBufferReader(List<int> bytes) { |
| 39 _buffer = new PbByteBuffer(); |
| 40 _buffer.add(bytes); |
| 41 } |
| 42 |
| 43 bool get closed() => _buffer.isEmpty; |
| 44 |
| 45 int get position() => _position; |
| 46 |
| 47 Future<int> readByte() { |
| 48 int byte = _buffer.readByte(); |
| 49 if (byte != null) _position++; |
| 50 return new Future.immediate(byte); |
| 51 } |
| 52 |
| 53 Future<List<int>> readBytes([int count = -1]) { |
| 54 List<int> bytes = _buffer.readBytes(count); |
| 55 if (bytes != null) _position += bytes.length; |
| 56 return new Future.immediate(bytes); |
| 57 } |
| 58 |
| 59 void setReadAhead([int count = -1]) {} |
| 60 |
| 61 PbByteBuffer _buffer; |
| 62 int _position = 0; |
| 63 } |
OLD | NEW |