Apache9 commented on code in PR #5059:
URL: https://github.com/apache/hbase/pull/5059#discussion_r1118237813
##########
hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java:
##########
@@ -3700,4 +3702,52 @@ public static ClusterStatusProtos.ServerTask
toServerTask(ServerTask task) {
.setStartTime(task.getStartTime()).setCompletionTime(task.getCompletionTime()).build();
}
+ /**
+ * Check whether this IPBE indicates EOF or not.
+ * <p/>
+ * We will check the exception message, if it is likely the one of
+ * InvalidProtocolBufferException.truncatedMessage, we will consider it as
EOF, otherwise not.
+ */
+ public static boolean isEOF(InvalidProtocolBufferException e) {
+ return e.getMessage().contains("input has been truncated");
+ }
+
+ /**
+ * This is a wrapper of the PB message's parseDelimitedFrom. The difference
is, if we can not
+ * determine whether there are enough bytes in stream, i.e, the available
method does not have a
+ * valid return value, we will try to read all the bytes to a byte array
first, and then parse the
+ * pb message with {@link Parser#parseFrom(byte[])} instead of call
+ * {@link Parser#parseDelimitedFrom(InputStream)} directly. This is because
even if the bytes are
+ * not enough bytes, {@link Parser#parseDelimitedFrom(InputStream)} could
still return without any
+ * errors but just leave us a partial PB message.
+ * @return The PB message if we can parse it successfully, otherwise there
will always be an
+ * exception thrown, will never return {@code null}.
+ */
+ public static <T extends Message> T parseDelimitedFrom(InputStream in,
Parser<T> parser)
+ throws IOException {
+ int firstByte = in.read();
+ if (firstByte < 0) {
+ throw new EOFException("EOF while reading message size");
+ }
+ int size = CodedInputStream.readRawVarint32(firstByte, in);
+ int available = in.available();
+ if (available > 0) {
+ if (available < size) {
+ throw new EOFException("Available bytes not enough for parsing PB
message, expect at least "
+ + size + " bytes, but only " + available + " bytes available");
+ }
+ // this piece of code is copied from GeneratedMessageV3.parseFrom
+ try {
+ return parser.parseFrom(ByteStreams.limit(in, size));
+ } catch (InvalidProtocolBufferException e) {
+ throw e.unwrapIOException();
Review Comment:
This piece of code is just copied from the protobuf code base, I think it is
OK to not logging here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]