Package anbxj
Class Channel_ProtobufOpaqueWrapper
java.lang.Object
anbxj.Channel_ProtobufOpaqueWrapper
Helper class for handling protobuf messages that contain an opaque payload field of type
bytes.
The protobuf message must have a field named "payload" of type bytes, with corresponding
setPayload(ByteString) and getPayload() methods.
This class uses reflection to access those methods, allowing generic handling of different message types that follow the same pattern. It can convert a serialized Java object (as a byte array) into a protobuf message wrapping that data, and extract the payload from a received protobuf message.
-
Constructor Summary
ConstructorsConstructorDescriptionChannel_ProtobufOpaqueWrapper(com.google.protobuf.Message defaultInstance) Constructs a helper for a specific protobuf message type. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]getPayload(com.google.protobuf.Message protoMsg) Extracts the opaque payload from a protobuf message of the configured type.com.google.protobuf.MessageparseFrom(byte[] data) Parses a protobuf message from its binary serialized form.com.google.protobuf.MessagetoProtobuf(byte[] javaSerialized) Wraps a serialized Java object (as a byte array) into a protobuf message of the configured type.
-
Constructor Details
-
Channel_ProtobufOpaqueWrapper
public Channel_ProtobufOpaqueWrapper(com.google.protobuf.Message defaultInstance) Constructs a helper for a specific protobuf message type.- Parameters:
defaultInstance- the default (empty) instance of the protobuf message type, used to obtain the parser and to reflectively access the builder methods.- Throws:
IllegalArgumentException- if the message class does not contain the requiredsetPayload(ByteString)andgetPayload()methods.
-
-
Method Details
-
toProtobuf
Wraps a serialized Java object (as a byte array) into a protobuf message of the configured type. The byte array becomes the value of thepayloadfield.- Parameters:
javaSerialized- the serialized Java object data to be wrapped.- Returns:
- a built protobuf message containing the given payload.
- Throws:
IOException- if reflection fails to construct the message or set the payload.
-
getPayload
Extracts the opaque payload from a protobuf message of the configured type.- Parameters:
protoMsg- a protobuf message (must be an instance of the type this helper was created for).- Returns:
- the payload as a byte array (the original serialized Java object data).
- Throws:
IOException- if reflection fails to invokegetPayload()on the message.
-
parseFrom
Parses a protobuf message from its binary serialized form.- Parameters:
data- the raw protobuf serialized bytes.- Returns:
- the parsed message.
- Throws:
IOException- if the data cannot be parsed.
-