Package anbxj

Class Channel_ProtobufOpaqueWrapper

java.lang.Object
anbxj.Channel_ProtobufOpaqueWrapper

public final class Channel_ProtobufOpaqueWrapper extends Object
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

    Constructors
    Constructor
    Description
    Channel_ProtobufOpaqueWrapper(com.google.protobuf.Message defaultInstance)
    Constructs a helper for a specific protobuf message type.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    getPayload(com.google.protobuf.Message protoMsg)
    Extracts the opaque payload from a protobuf message of the configured type.
    com.google.protobuf.Message
    parseFrom(byte[] data)
    Parses a protobuf message from its binary serialized form.
    com.google.protobuf.Message
    toProtobuf(byte[] javaSerialized)
    Wraps a serialized Java object (as a byte array) into a protobuf message of the configured type.

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 required setPayload(ByteString) and getPayload() methods.
  • Method Details

    • toProtobuf

      public com.google.protobuf.Message toProtobuf(byte[] javaSerialized) throws IOException
      Wraps a serialized Java object (as a byte array) into a protobuf message of the configured type. The byte array becomes the value of the payload field.
      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

      public byte[] getPayload(com.google.protobuf.Message protoMsg) throws IOException
      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 invoke getPayload() on the message.
    • parseFrom

      public com.google.protobuf.Message parseFrom(byte[] data) throws IOException
      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.