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 CodedBufferReader extends CodedReader { |
| 6 PbByteBuffer _input; |
| 7 |
| 8 CodedBufferReader.fromBuffer(List<int> _buffer, [ |
| 9 int recursionLimit = CodedReader.DEFAULT_RECURSION_LIMIT, |
| 10 int sizeLimit = CodedReader.DEFAULT_SIZE_LIMIT]) : |
| 11 super(recursionLimit, sizeLimit), |
| 12 _input = new PbByteBuffer.fromList(_buffer); |
| 13 |
| 14 bool isAtEnd() => |
| 15 (_currentLimit != -1 && _bufferPos >= _currentLimit) || _input.isEmpty; |
| 16 |
| 17 void readGroup(int fieldNumber, Builder builder, |
| 18 ExtensionRegistry extensionRegistry) { |
| 19 if (_recursionDepth >= _recursionLimit) { |
| 20 throw InvalidProtocolBufferException.recursionLimitExceeded(); |
| 21 } |
| 22 ++_recursionDepth; |
| 23 builder.mergeFromCodedBufferReader(this); |
| 24 checkLastTagWas(WireFormat.makeTag(fieldNumber, |
| 25 WireFormat.WIRETYPE_END_GROUP)); |
| 26 --_recursionDepth; |
| 27 } |
| 28 |
| 29 void readUnknownFieldSetGroup(int fieldNumber, UnknownFieldSet_Builder builder
) { |
| 30 if (_recursionDepth >= _recursionLimit) { |
| 31 throw InvalidProtocolBufferException.recursionLimitExceeded(); |
| 32 } |
| 33 ++_recursionDepth; |
| 34 builder.mergeFromCodedBufferReader(this); |
| 35 checkLastTagWas(WireFormat.makeTag(fieldNumber, |
| 36 WireFormat.WIRETYPE_END_GROUP)); |
| 37 --_recursionDepth; |
| 38 } |
| 39 |
| 40 void readMessage(Builder builder, ExtensionRegistry extensionRegistry) { |
| 41 int length = readInt32(); |
| 42 if (_recursionDepth >= _recursionLimit) { |
| 43 throw InvalidProtocolBufferException.recursionLimitExceeded(); |
| 44 } |
| 45 int oldLimit = _pushLimit(length); |
| 46 ++_recursionDepth; |
| 47 builder.mergeFromCodedBufferReader(this); |
| 48 checkLastTagWas(0); |
| 49 --_recursionDepth; |
| 50 _popLimit(oldLimit); |
| 51 } |
| 52 |
| 53 int readEnum() => readInt32(); |
| 54 int readInt32() => PbCodec.toInt32(readRawVarint()); |
| 55 Dynamic readInt64() => PbCodec.toInt64(readRawVarint()); |
| 56 int readUint32() => PbCodec.toUint32(readRawVarint()); |
| 57 int readUint64() => PbCodec.toUint64(readRawVarint()); |
| 58 int readSint32() => PbCodec.toSint32(readRawVarint()); |
| 59 Dynamic readSint64() => PbCodec.toSint64(readRawVarint()); |
| 60 int readFixed32() => PbCodec.toFixed32(readRaw32()); |
| 61 Dynamic readFixed64() => PbCodec.toFixed64(readRaw64()); |
| 62 int readSfixed32() => PbCodec.toSfixed32(readRaw32()); |
| 63 Dynamic readSfixed64() => PbCodec.toSfixed64(readRaw64()); |
| 64 bool readBool() => PbCodec.toBool(readRawVarint()); |
| 65 List<int> readBytes() => readRawLengthDelimited(); |
| 66 String readString() => decodeUtf8(readRawLengthDelimited()); |
| 67 double readFloat() => PbCodec.toFloat(readRaw32()); |
| 68 double readDouble() => PbCodec.toDouble(readRaw64()); |
| 69 |
| 70 int readTag() { |
| 71 if (isAtEnd()) { |
| 72 _lastTag = 0; |
| 73 return 0; |
| 74 } |
| 75 |
| 76 _lastTag = readInt32(); |
| 77 if (WireFormat.getTagFieldNumber(_lastTag) == 0) { |
| 78 throw InvalidProtocolBufferException.invalidTag(); |
| 79 } |
| 80 return _lastTag; |
| 81 } |
| 82 |
| 83 List<int> readRawVarint() { |
| 84 List<int> rawBytes = []; |
| 85 rawBytes.add(_readRawByte()); |
| 86 while (rawBytes.length < 10 && (rawBytes.last() & 0x80) != 0) { |
| 87 int byte = _readRawByte(); |
| 88 if (byte == null) { |
| 89 throw InvalidProtocolBufferException.truncatedMessage(); |
| 90 } |
| 91 rawBytes.add(byte); |
| 92 } |
| 93 if ((rawBytes.last() & 0x80) != 0) { |
| 94 throw InvalidProtocolBufferException.malformedVarint(); |
| 95 } |
| 96 return rawBytes; |
| 97 } |
| 98 |
| 99 List<int> readRaw32() => readRawFixed(4); |
| 100 List<int> readRaw64() => readRawFixed(8); |
| 101 List<int> readRawFixed(int length) => _readRawBytes(length); |
| 102 |
| 103 List<int> readRawLengthDelimited() { |
| 104 int length = readInt32(); |
| 105 return _readRawBytes(length); |
| 106 } |
| 107 |
| 108 int _readRawByte() { |
| 109 _bufferPos++; |
| 110 _checkLimit(); |
| 111 int byte = _input.readByte(); |
| 112 if (byte == null) { |
| 113 throw InvalidProtocolBufferException.truncatedMessage(); |
| 114 } |
| 115 return byte; |
| 116 } |
| 117 |
| 118 List<int> _readRawBytes(int size) { |
| 119 _bufferPos += size; |
| 120 _checkLimit(); |
| 121 List<int> bytes = _input.readBytes(size); |
| 122 return bytes; |
| 123 } |
| 124 |
| 125 void skipField(int tag) { |
| 126 int number = tag >> 3; |
| 127 int wireType = tag & 0x7; |
| 128 switch (wireType) { |
| 129 case WireFormat.WIRETYPE_VARINT: |
| 130 readRawVarint(); |
| 131 return; |
| 132 case WireFormat.WIRETYPE_FIXED64: |
| 133 readRaw64(); |
| 134 return; |
| 135 case WireFormat.WIRETYPE_LENGTH_DELIMITED: |
| 136 readRawLengthDelimited(); |
| 137 return; |
| 138 case WireFormat.WIRETYPE_START_GROUP: |
| 139 UnknownFieldSet_Builder subBuilder = new UnknownFieldSet_Builder(); |
| 140 readUnknownFieldSetGroup(number, subBuilder); |
| 141 return; |
| 142 case WireFormat.WIRETYPE_END_GROUP: // should not happen, but ignore? |
| 143 return; |
| 144 case WireFormat.WIRETYPE_FIXED32: |
| 145 readRaw32(); |
| 146 return; |
| 147 } |
| 148 } |
| 149 |
| 150 void skipRawBytes(int number) { |
| 151 _readRawBytes(number); |
| 152 } |
| 153 } |
OLD | NEW |