/** * Merges the {@code message} from the {@link InputStream} with the supplied * {@code buf} to use. */ static <T> void mergeFrom(InputStream in, byte[] buf, T message, Schema<T> schema, boolean decodeNestedMessageAsGroup) throws IOException { final CodedInput input = new CodedInput(in, buf, decodeNestedMessageAsGroup); schema.mergeFrom(input, message); input.checkLastTagWas(0); }
/** Read an {@code sint32} field value from the stream. */ public int readSInt32() throws IOException { return decodeZigZag32(readRawVarint32()); }
/** Read an {@code sint64} field value from the stream. */ public long readSInt64() throws IOException { return decodeZigZag64(readRawVarint64()); }
@Override public <T> void handleUnknownField(int fieldNumber, Schema<T> schema) throws IOException { if (!messageReference) input.skipField(input.getLastTag()); }
/** * 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; }
switch (WireFormat.getTagWireType(tag)) { case WireFormat.WIRETYPE_VARINT: readInt32(); return true; case WireFormat.WIRETYPE_FIXED64: readRawLittleEndian64(); return true; case WireFormat.WIRETYPE_LENGTH_DELIMITED: skipRawBytes(readRawVarint32()); return true; case WireFormat.WIRETYPE_START_GROUP: skipMessage(); checkLastTagWas( WireFormat.makeTag(WireFormat.getTagFieldNumber(tag), WireFormat.WIRETYPE_END_GROUP)); return false; case WireFormat.WIRETYPE_FIXED32: readRawLittleEndian32(); return true; default:
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; }
/** * Create a new CodedInput wrapping the given InputStream. */ public static CodedInput newInstance(final InputStream input) { return new CodedInput(input, false); }
public <T> int readFieldNumber(Schema<T> schema) throws IOException { final int fieldNumber = input.readFieldNumber(schema); if(WireFormat.getTagWireType(input.getLastTag()) == WIRETYPE_REFERENCE) { // a reference. lastRef = input.readUInt32(); messageReference = true; } else { // always unset. messageReference = false; } return fieldNumber; }
/** * 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; }
/** Reads a message field value from the stream (using the {@code group} encoding). */ private <T> T mergeObjectEncodedAsGroup(T value, final Schema<T> schema) throws IOException { //if (recursionDepth >= recursionLimit) { // throw ProtobufException.recursionLimitExceeded(); //} //++recursionDepth; if(value == null) { value = schema.newMessage(); } schema.mergeFrom(this, value); if(!schema.isInitialized(value)) { throw new UninitializedMessageException(value, schema); } // handling is in #readFieldNumber checkLastTagWas(0); //--recursionDepth; return value; }
/** * Create a new CodedInput wrapping the given byte array. */ public static CodedInput newInstance(final byte[] buf) { return newInstance(buf, 0, buf.length); }
@SuppressWarnings("unchecked") public <T> T mergeObject(T value, Schema<T> schema) throws IOException { if(messageReference) { // a reference. return (T)references.get(lastRef); } lastSchema = (Schema<Object>)schema; if(value == null) value = schema.newMessage(); references.add(value); input.mergeObject(value, this); return value; }
final int len = size < 0x80 ? size : CodedInput.readRawVarint32(in, size); if(len != 0) final CodedInput input = new CodedInput(lin.limit(len), false); schema.mergeFrom(input, message); input.checkLastTagWas(0); continue;
/** * Create a new CodedInput wrapping the given byte array slice. */ public static CodedInput newInstance(final byte[] buf, final int off, final int len) { return new CodedInput(buf, off, len, false); }
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; }
throw new EOFException("mergeDelimitedFrom"); final int len = size < 0x80 ? size : CodedInput.readRawVarint32(in, size); final CodedInput input = new CodedInput(new LimitedInputStream(in, len), decodeNestedMessageAsGroup); schema.mergeFrom(input, message); input.checkLastTagWas(0); return len;
/** * Merges the {@code message} from the {@link InputStream} using the given {@code schema}. */ static <T> void mergeFrom(InputStream in, T message, Schema<T> schema, boolean decodeNestedMessageAsGroup) throws IOException { final CodedInput input = new CodedInput(in, decodeNestedMessageAsGroup); schema.mergeFrom(input, message); input.checkLastTagWas(0); }
/** * Creates a protostuff pipe from an {@link InputStream}. */ public static Pipe newPipe(final InputStream in) { final CodedInput codedInput = new CodedInput(in, true); return new Pipe() { protected Input begin(Pipe.Schema<?> pipeSchema) throws IOException { return codedInput; } protected void end(Pipe.Schema<?> pipeSchema, Input input, boolean cleanupOnly) throws IOException { if(cleanupOnly) return; assert input == codedInput; } }; }
final int len = 0 == (size & 0x80) ? size : CodedInput.readRawVarint32(in, size); final CodedInput input = new CodedInput(new LimitedInputStream((InputStream)in, len), decodeNestedMessageAsGroup); schema.mergeFrom(input, message); input.checkLastTagWas(0);