31 package com.google.protobuf;
42 import java.io.IOException;
43 import java.nio.ByteBuffer;
44 import java.util.List;
52 abstract class BinaryReader
implements Reader {
53 private static final int FIXED32_MULTIPLE_MASK = FIXED32_SIZE - 1;
54 private static final int FIXED64_MULTIPLE_MASK = FIXED64_SIZE - 1;
67 public static BinaryReader newInstance(ByteBuffer
buffer,
boolean bufferIsImmutable) {
70 return new SafeHeapReader(
buffer, bufferIsImmutable);
73 throw new IllegalArgumentException(
"Direct buffers not yet supported");
77 private BinaryReader() {}
80 public abstract int getTotalBytesRead();
83 public boolean shouldDiscardUnknownFields() {
103 initialPos =
pos = bytebuf.arrayOffset() + bytebuf.position();
104 limit = bytebuf.arrayOffset() + bytebuf.limit();
119 return Reader.READ_DONE;
123 return Reader.READ_DONE;
238 return readMessage(Protobuf.getInstance().schemaFor(clazz), extensionRegistry);
242 public <T>
T readMessageBySchemaWithCheck(
245 return readMessage(schema, extensionRegistry);
248 private <T>
T readMessage(Schema<T> schema, ExtensionRegistryLite extensionRegistry)
254 int prevLimit =
limit;
261 schema.mergeFrom(
message,
this, extensionRegistry);
264 if (
pos != newLimit) {
265 throw InvalidProtocolBufferException.parseFailure();
275 public <T>
T readGroup(Class<T> clazz, ExtensionRegistryLite extensionRegistry)
278 return readGroup(Protobuf.getInstance().schemaFor(clazz), extensionRegistry);
282 public <T>
T readGroupBySchemaWithCheck(
283 Schema<T> schema, ExtensionRegistryLite extensionRegistry)
throws IOException {
285 return readGroup(schema, extensionRegistry);
288 private <T>
T readGroup(Schema<T> schema, ExtensionRegistryLite extensionRegistry)
296 schema.mergeFrom(
message,
this, extensionRegistry);
300 throw InvalidProtocolBufferException.parseFailure();
364 if (
target instanceof DoubleArrayList) {
365 DoubleArrayList plist = (DoubleArrayList)
target;
370 final int fieldEndPos =
pos +
bytes;
371 while (
pos < fieldEndPos) {
384 if (nextTag !=
tag) {
399 final int fieldEndPos =
pos +
bytes;
400 while (
pos < fieldEndPos) {
413 if (nextTag !=
tag) {
428 if (
target instanceof FloatArrayList) {
429 FloatArrayList plist = (FloatArrayList)
target;
434 final int fieldEndPos =
pos +
bytes;
435 while (
pos < fieldEndPos) {
448 if (nextTag !=
tag) {
463 final int fieldEndPos =
pos +
bytes;
464 while (
pos < fieldEndPos) {
477 if (nextTag !=
tag) {
492 if (
target instanceof LongArrayList) {
493 LongArrayList plist = (LongArrayList)
target;
497 final int fieldEndPos =
pos +
bytes;
498 while (
pos < fieldEndPos) {
512 if (nextTag !=
tag) {
526 final int fieldEndPos =
pos +
bytes;
527 while (
pos < fieldEndPos) {
541 if (nextTag !=
tag) {
556 if (
target instanceof LongArrayList) {
557 LongArrayList plist = (LongArrayList)
target;
561 final int fieldEndPos =
pos +
bytes;
562 while (
pos < fieldEndPos) {
576 if (nextTag !=
tag) {
590 final int fieldEndPos =
pos +
bytes;
591 while (
pos < fieldEndPos) {
605 if (nextTag !=
tag) {
620 if (
target instanceof IntArrayList) {
621 IntArrayList plist = (IntArrayList)
target;
625 final int fieldEndPos =
pos +
bytes;
626 while (
pos < fieldEndPos) {
640 if (nextTag !=
tag) {
654 final int fieldEndPos =
pos +
bytes;
655 while (
pos < fieldEndPos) {
669 if (nextTag !=
tag) {
684 if (
target instanceof LongArrayList) {
685 LongArrayList plist = (LongArrayList)
target;
690 final int fieldEndPos =
pos +
bytes;
691 while (
pos < fieldEndPos) {
704 if (nextTag !=
tag) {
719 final int fieldEndPos =
pos +
bytes;
720 while (
pos < fieldEndPos) {
733 if (nextTag !=
tag) {
748 if (
target instanceof IntArrayList) {
749 IntArrayList plist = (IntArrayList)
target;
754 final int fieldEndPos =
pos +
bytes;
755 while (
pos < fieldEndPos) {
768 if (nextTag !=
tag) {
783 final int fieldEndPos =
pos +
bytes;
784 while (
pos < fieldEndPos) {
797 if (nextTag !=
tag) {
812 if (
target instanceof BooleanArrayList) {
813 BooleanArrayList plist = (BooleanArrayList)
target;
817 final int fieldEndPos =
pos +
bytes;
818 while (
pos < fieldEndPos) {
832 if (nextTag !=
tag) {
846 final int fieldEndPos =
pos +
bytes;
847 while (
pos < fieldEndPos) {
861 if (nextTag !=
tag) {
900 if (nextTag !=
tag) {
916 if (nextTag !=
tag) {
927 public <T>
void readMessageList(
930 final Schema<T> schema = Protobuf.getInstance().schemaFor(targetType);
931 readMessageList(
target, schema, extensionRegistry);
935 public <T>
void readMessageList(
941 final int listTag =
tag;
943 target.add(readMessage(schema, extensionRegistry));
950 if (nextTag != listTag) {
960 public <T>
void readGroupList(
961 List<T>
target, Class<T> targetType, ExtensionRegistryLite extensionRegistry)
963 final Schema<T> schema = Protobuf.getInstance().schemaFor(targetType);
964 readGroupList(
target, schema, extensionRegistry);
968 public <T>
void readGroupList(
969 List<T>
target, Schema<T> schema, ExtensionRegistryLite extensionRegistry)
972 throw InvalidProtocolBufferException.invalidWireType();
974 final int listTag =
tag;
976 target.add(readGroup(schema, extensionRegistry));
983 if (nextTag != listTag) {
1006 if (nextTag !=
tag) {
1017 if (
target instanceof IntArrayList) {
1018 IntArrayList plist = (IntArrayList)
target;
1022 final int fieldEndPos =
pos +
bytes;
1023 while (
pos < fieldEndPos) {
1036 if (nextTag !=
tag) {
1050 final int fieldEndPos =
pos +
bytes;
1051 while (
pos < fieldEndPos) {
1064 if (nextTag !=
tag) {
1079 if (
target instanceof IntArrayList) {
1080 IntArrayList plist = (IntArrayList)
target;
1084 final int fieldEndPos =
pos +
bytes;
1085 while (
pos < fieldEndPos) {
1098 if (nextTag !=
tag) {
1112 final int fieldEndPos =
pos +
bytes;
1113 while (
pos < fieldEndPos) {
1126 if (nextTag !=
tag) {
1141 if (
target instanceof IntArrayList) {
1142 IntArrayList plist = (IntArrayList)
target;
1147 final int fieldEndPos =
pos +
bytes;
1148 while (
pos < fieldEndPos) {
1161 if (nextTag !=
tag) {
1176 final int fieldEndPos =
pos +
bytes;
1177 while (
pos < fieldEndPos) {
1190 if (nextTag !=
tag) {
1205 if (
target instanceof LongArrayList) {
1206 LongArrayList plist = (LongArrayList)
target;
1211 final int fieldEndPos =
pos +
bytes;
1212 while (
pos < fieldEndPos) {
1225 if (nextTag !=
tag) {
1240 final int fieldEndPos =
pos +
bytes;
1241 while (
pos < fieldEndPos) {
1254 if (nextTag !=
tag) {
1269 if (
target instanceof IntArrayList) {
1270 IntArrayList plist = (IntArrayList)
target;
1274 final int fieldEndPos =
pos +
bytes;
1275 while (
pos < fieldEndPos) {
1288 if (nextTag !=
tag) {
1302 final int fieldEndPos =
pos +
bytes;
1303 while (
pos < fieldEndPos) {
1316 if (nextTag !=
tag) {
1331 if (
target instanceof LongArrayList) {
1332 LongArrayList plist = (LongArrayList)
target;
1336 final int fieldEndPos =
pos +
bytes;
1337 while (
pos < fieldEndPos) {
1350 if (nextTag !=
tag) {
1364 final int fieldEndPos =
pos +
bytes;
1365 while (
pos < fieldEndPos) {
1378 if (nextTag !=
tag) {
1391 @SuppressWarnings(
"unchecked")
1393 public <K, V>
void readMap(
1397 throws IOException {
1403 int prevLimit =
limit;
1412 if (
number == READ_DONE) {
1434 }
catch (InvalidProtocolBufferException.InvalidWireTypeException ignore) {
1437 throw new InvalidProtocolBufferException(
"Unable to parse map entry.");
1450 Class<?> messageType,
1452 throws IOException {
1453 switch (fieldType) {
1473 return readMessage(messageType, extensionRegistry);
1489 throw new RuntimeException(
"unsupported field type.");
1506 }
else if (
limit -
i < 9) {
1508 }
else if ((
x ^= (
buffer[
i++] << 7)) < 0) {
1510 }
else if ((
x ^= (
buffer[
i++] << 14)) >= 0) {
1511 x ^= (~0 << 7) ^ (~0 << 14);
1512 }
else if ((
x ^= (
buffer[
i++] << 21)) < 0) {
1513 x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21);
1517 x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21) ^ (~0 << 28);
1555 }
else if (
limit -
i < 9) {
1557 }
else if ((
y ^= (
buffer[
i++] << 7)) < 0) {
1559 }
else if ((
y ^= (
buffer[
i++] << 14)) >= 0) {
1560 x =
y ^ ((~0 << 7) ^ (~0 << 14));
1561 }
else if ((
y ^= (
buffer[
i++] << 21)) < 0) {
1562 x =
y ^ ((~0 << 7) ^ (~0 << 14) ^ (~0 << 21));
1563 }
else if ((
x =
y ^ ((
long)
buffer[
i++] << 28)) >= 0L) {
1564 x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28);
1565 }
else if ((
x ^= ((
long)
buffer[
i++] << 35)) < 0L) {
1566 x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35);
1567 }
else if ((
x ^= ((
long)
buffer[
i++] << 42)) >= 0L) {
1568 x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35) ^ (~0L << 42);
1569 }
else if ((
x ^= ((
long)
buffer[
i++] << 49)) < 0L) {
1601 for (
int shift = 0; shift < 64; shift += 7) {
1603 result |= (
long) (
b & 0x7F) << shift;
1604 if ((
b & 0x80) == 0) {
1631 pos =
p + FIXED32_SIZE;
1633 | ((
buffer[
p + 1] & 0xff) << 8)
1634 | ((
buffer[
p + 2] & 0xff) << 16)
1635 | ((
buffer[
p + 3] & 0xff) << 24));
1641 pos =
p + FIXED64_SIZE;
1643 | ((
buffer[
p + 1] & 0xffL) << 8)
1644 | ((
buffer[
p + 2] & 0xffL) << 16)
1645 | ((
buffer[
p + 3] & 0xffL) << 24)
1646 | ((
buffer[
p + 4] & 0xffL) << 32)
1647 | ((
buffer[
p + 5] & 0xffL) << 40)
1648 | ((
buffer[
p + 6] & 0xffL) << 48)
1649 | ((
buffer[
p + 7] & 0xffL) << 56));
1656 for (
int i = 0;
i < 10;
i++) {
1657 if (buffer[
p++] >= 0) {
1667 for (
int i = 0;
i < 10;
i++) {
1709 if ((
bytes & FIXED64_MULTIPLE_MASK) != 0) {
1717 if ((
bytes & FIXED32_MULTIPLE_MASK) != 0) {
1724 if (
pos != expectedPosition) {