static InvalidProtocolBufferException truncatedMessage() { return new InvalidProtocolBufferException("While parsing a protocol message, the input ended unexpectedly " + "in the middle of a field. This could mean either than the " + "input has been truncated or that an embedded message " + "misreported its own length."); }
/** * Verifies that the last call to readTag() returned the given tag value. * This is used to verify that a nested group ended with the correct end * tag. * * @throws InvalidProtocolBufferException * {@code value} does not match the last tag. */ public void checkLastTagWas(int value) throws InvalidProtocolBufferException { if (lastTag != value) { throw InvalidProtocolBufferException.invalidEndTag(); } }
public static Buffer readFrame(java.io.InputStream input) throws IOException { int length = 0; try { length = readRawVarint32(input); } catch (InvalidProtocolBufferException e) { if( e.isEOF() ) { throw new EOFException(); } else { throw e; } } byte[] data = new byte[length]; int pos = 0; while (pos < length) { int r = input.read(data, pos, length - pos); if (r < 0) { throw new InvalidProtocolBufferException("Input stream ended before a full message frame could be read."); } pos += r; } return new Buffer(data); }
/** Read a raw Varint from the stream. */ public long readRawVarint64() throws IOException { int shift = 0; long result = 0; while (shift < 64) { byte b = readRawByte(); result |= (long) (b & 0x7F) << shift; if ((b & 0x80) == 0) return result; shift += 7; } throw InvalidProtocolBufferException.malformedVarint(); }
/** * 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( pos >= limit ) { lastTag=0; return 0; } try { lastTag = readRawVarint32(); if (lastTag == 0) { // If we actually read zero, that's not a valid tag. throw InvalidProtocolBufferException.invalidTag(); } return lastTag; } catch (EOFException e) { lastTag=0; return 0; } }
/** * Reads and discards a single field, given its tag value. * * @return {@code false} if the tag is an endgroup tag, in which case * nothing is skipped. Otherwise, returns {@code true}. */ public boolean skipField(int tag) throws IOException { 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 true; case WireFormat.WIRETYPE_END_GROUP: return false; case WireFormat.WIRETYPE_FIXED32: readRawLittleEndian32(); return true; default: throw InvalidProtocolBufferException.invalidWireType(); } }
return result; throw InvalidProtocolBufferException.malformedVarint();
static InvalidProtocolBufferException invalidEndTag() { return new InvalidProtocolBufferException("Protocol message end-group tag did not match expected tag."); }
static InvalidProtocolBufferException invalidWireType() { return new InvalidProtocolBufferException("Protocol message tag had invalid wire type."); }
static InvalidProtocolBufferException recursionLimitExceeded() { return new InvalidProtocolBufferException("Protocol message had too many levels of nesting. May be malicious. " + "Use CodedInputStream.setRecursionLimit() to increase the depth limit."); }
static InvalidProtocolBufferException sizeLimitExceeded() { return new InvalidProtocolBufferException("Protocol message was too large. May be malicious. " + "Use CodedInputStream.setSizeLimit() to increase the size limit."); } }
static InvalidProtocolBufferException negativeSize() { return new InvalidProtocolBufferException("CodedInputStream encountered an embedded string or message " + "which claimed to have negative size."); }
static InvalidProtocolBufferException malformedVarint() { return new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint."); }
static InvalidProtocolBufferException invalidTag() { return new InvalidProtocolBufferException("Protocol message contained an invalid tag (zero)."); }
static protected byte readRawByte(InputStream is) throws IOException { int rc = is.read(); if (rc == -1) { throw new InvalidProtocolBufferException("While parsing a protocol message, the input ended unexpectedly " + "in the middle of a field. This could mean either than the " + "input has been truncated or that an embedded message " + "misreported its own length."); } return (byte) rc; }
static public byte readRawByte(InputStream is) throws IOException { int rc = is.read(); if (rc == -1) { throw new InvalidProtocolBufferException("While parsing a protocol message, the input ended unexpectedly " + "in the middle of a field. This could mean either than the " + "input has been truncated or that an embedded message " + "misreported its own length.", true); } return (byte) rc; }
/** * Converts this exception to an {@link InvalidProtocolBufferException}. * When a parsed message is missing required fields, this should be thrown * instead of {@code UninitializedMessageException}. */ public InvalidProtocolBufferException asInvalidProtocolBufferException() { return new InvalidProtocolBufferException(getMessage()); }
public T mergeFramed(InputStream input) throws IOException { int length = readRawVarint32(input); byte[] data = new byte[length]; int pos = 0; while (pos < length) { int r = input.read(data, pos, length - pos); if (r < 0) { throw new InvalidProtocolBufferException("Input stream ended before a full message frame could be read."); } pos += r; } return mergeUnframed(data); }
return result; throw new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint.");
return result; throw new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint.");