/** Read an {@code int32} field value from the stream. */ public int readInt32() throws IOException { return readRawVarint32(); }
/** * Read an enum field value from the stream. Caller is responsible * for converting the numeric value to an actual enum. */ public int readEnum() throws IOException { return readRawVarint32(); }
/** Read a {@code bool} field value from the stream. */ public boolean readBool() throws IOException { return readRawVarint32() != 0; }
/** Read a {@code uint32} field value from the stream. */ public int readUInt32() throws IOException { return readRawVarint32(); }
/** Read an {@code sint32} field value from the stream. */ public int readSInt32() throws IOException { return decodeZigZag32(readRawVarint32()); }
public byte[] readByteArray() throws IOException { final int size = readRawVarint32(); if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final byte[] copy = new byte[size]; System.arraycopy(buffer, bufferPos, copy, 0, size); bufferPos += size; return copy; } else { // Slow path: Build a byte array first then copy it. return readRawBytes(size); } }
/** * Reads a varint from the input one byte at a time, so that it does not * read any bytes after the end of the varint. If you simply wrapped the * stream in a CodedInput and used {@link #readRawVarint32(InputStream)} * then you would probably end up reading past the end of the varint since * CodedInput buffers its input. */ static int readRawVarint32(final InputStream input) throws IOException { final int firstByte = input.read(); if (firstByte == -1) { throw ProtobufException.truncatedMessage(); } if ((firstByte & 0x80) == 0) { return firstByte; } return readRawVarint32(input, firstByte); }
/** * Attempt to read a field tag, returning zero if we have reached EOF. * Protocol message parsers use this to read tags, since a protocol message * may legally end wherever a tag occurs, and zero is not a valid tag number. */ public int readTag() throws IOException { if (isAtEnd()) { lastTag = 0; return 0; } final int tag = readRawVarint32(); if (tag >>> TAG_TYPE_BITS == 0) { // If we actually read zero, that's not a valid tag. throw ProtobufException.invalidTag(); } lastTag = tag; return tag; }
public void transferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated) throws IOException { final int size = readRawVarint32(); if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer output.writeByteRange(utf8String, fieldNumber, buffer, bufferPos, size, repeated); bufferPos += size; } else { // Slow path: Build a byte array first then copy it. output.writeByteRange(utf8String, fieldNumber, readRawBytes(size), 0, size, repeated); } }
/** * Parses the {@code messages} (delimited) from the {@link InputStream} * using the given {@code schema}. * * @return the list containing the messages. */ public static <T> List<T> parseListFrom(final InputStream in, final Schema<T> schema) throws IOException { int size = in.read(); if(size == -1) return Collections.emptyList(); if(size > 0x7f) size = CodedInput.readRawVarint32(in, size); final ArrayList<T> list = new ArrayList<T>(size); final CodedInput input = new CodedInput(in, true); for(int i = 0; i < size; i++) { final T message = schema.newMessage(); list.add(message); schema.mergeFrom(input, message); input.checkLastTagWas(0); } assert in.read() == -1; return list; }
public <T> int readFieldNumber(Schema<T> schema) throws IOException { if (isAtEnd()) { lastTag = 0; return 0; } final int tag = readRawVarint32(); final int fieldNumber = tag >>> TAG_TYPE_BITS; if (fieldNumber == 0) { if(decodeNestedMessageAsGroup && WIRETYPE_TAIL_DELIMITER == (tag & TAG_TYPE_MASK)) { // protostuff's tail delimiter for streaming // 2 options: length-delimited or tail-delimited. lastTag = 0; return 0; } // If we actually read zero, that's not a valid tag. throw ProtobufException.invalidTag(); } if(decodeNestedMessageAsGroup && WIRETYPE_END_GROUP == (tag & TAG_TYPE_MASK)) { lastTag = 0; return 0; } lastTag = tag; return fieldNumber; }
/** Read a {@code string} field value from the stream. */ public String readString() throws IOException { final int size = readRawVarint32(); if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final String result = STRING.deser(buffer, bufferPos, size); bufferPos += size; return result; } else { // Slow path: Build a byte array first then copy it. return STRING.deser(readRawBytes(size)); } }
/** Read a {@code bytes} field value from the stream. */ public ByteString readBytes() throws IOException { final int size = readRawVarint32(); if (size == 0) { return ByteString.EMPTY; } if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final ByteString result = ByteString.copyFrom(buffer, bufferPos, size); bufferPos += size; return result; } else { // Slow path: Build a byte array first then copy it. //return ByteString.copyFrom(readRawBytes(size)); return ByteString.wrap(readRawBytes(size)); } }
throw new EOFException("mergeDelimitedFrom"); final int len = size < 0x80 ? size : CodedInput.readRawVarint32(in, size);
throw new EOFException("mergeDelimitedFrom"); final int len = size < 0x80 ? size : CodedInput.readRawVarint32(in, size);
public <T> T mergeObject(T value, final Schema<T> schema) throws IOException { if(decodeNestedMessageAsGroup) return mergeObjectEncodedAsGroup(value, schema); final int length = readRawVarint32(); //if (recursionDepth >= recursionLimit) { // throw ProtobufException.recursionLimitExceeded(); //} final int oldLimit = pushLimit(length); //++recursionDepth; if(value == null) { value = schema.newMessage(); } schema.mergeFrom(this, value); if(!schema.isInitialized(value)) { throw new UninitializedMessageException(value, schema); } checkLastTagWas(0); //--recursionDepth; popLimit(oldLimit); return value; }
final int len = 0 == (size & 0x80) ? size : CodedInput.readRawVarint32(in, size);
final int len = size < 0x80 ? size : CodedInput.readRawVarint32(in, size);
return true; case WireFormat.WIRETYPE_LENGTH_DELIMITED: skipRawBytes(readRawVarint32()); return true; case WireFormat.WIRETYPE_START_GROUP:
throw ProtobufException.truncatedMessage(); final int len = size < 0x80 ? size : CodedInput.readRawVarint32(in, size);