Class WrappedMessage
1. The Protocol Buffers encoding format does not contain any description of the message type that follows next in the
data stream, unlike for example the Java serialization format which provides information about classes that are saved
in a Serialization stream in the form of class descriptors which contain the fully qualified name of the class being
serialized. The Protocol Buffers client is expected to know what message type he is expecting to read from the
stream. This knowledge exists in most cases, statically, so this encoding scheme saves a lot of space by not
including redundant type descriptors in the stream by default. For all other use cases where data types are dynamic
you are on your own, but WrappedMessage is here to help you.
2. The Protocol Buffer wire format is also not self-delimiting, so when reading a message we see just a stream of
fields and we are not able to determine when the fields of the current message end and the next message starts. The
protocol assumes that the whole contents of the stream is to be interpreted as a single message. If that's not the
case, then the user must provide his own way of delimiting messages either by using message start/stop markers or by
prefixing each message with its size or any other equivalent mechanism. WrappedMessage relies on an
int32 size prefix.
So wherever you cannot statically decide what message type you'll be using and need to defer this until runtime, just
use WrappedMessage.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordprivate static final recordprivate static interface -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String(package private) static final BaseMarshaller<WrappedMessage> Marshaller for WrappedMessage.static final StringFull path of the message-wrapping.proto resource file in classpath.static final intThe type id of WrappedMessage itself.static final StringThe fully qualified Protobuf type name of this message.private final ObjectThe wrapped object or (boxed) primitive.static final intA wrapped bool.static final intA wrapped byte (marshalled as int32).static final intA wrapped bytes.static final intA wrapped char (marshalled as int32).static final intstatic final intThe (optional) number of repeated elements.static final intstatic final intstatic final intA wrapped java.util.Date (marshalled as int64).static final intDeprecated.static final intDeprecated.UseWRAPPED_TYPE_IDinstead.static final intDeprecated.UseWRAPPED_TYPE_IDinstead.static final intA wrapped double.static final intA flag indicating and empty/null message.static final intThe enum value.static final intA wrapped fixed32.static final intA wrapped fixed64.static final intA wrapped float.static final intThe nanoseconds of the java.time.Instant.static final intA wrapped java.time.Instant (marshalled as int64 (seconds) and an int32 (nanos)).static final intA wrapped int32.static final intA wrapped int64.static final intA byte array containing the encoded message.static final intA wrapped sfixed32.static final intA wrapped sfixed64.static final intA wrapped short (marshalled as int32).static final intA wrapped sint32.static final intA wrapped sint64.static final intA wrapped string.static final intThe (optional) numeric type id of the wrapped message or enum.static final intThe name of the fully qualified message or enum type name, when the wrapped object is a message or enum.static final intA wrapped uint32.static final intA wrapped uint64. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleangetValue()Returns the wrapped value, which is either a primitive, an enum, or a message.inthashCode()private static intmapTypeIdIn(int typeId, ImmutableSerializationContext ctx) Map type id to new value during reading, to support schema evolution.private static intmapTypeIdOut(String typeName, ImmutableSerializationContext ctx) Map type id to old value, during writing, to support schema evolution.(package private) static <T> Tread(ImmutableSerializationContext ctx, TagReader in) private static ObjectreadContainer(ImmutableSerializationContext ctx, TagReader in, int tag) private static <E> Eprivate static voidreadContainerWithoutWrappedElements(BaseMarshaller<?> containerMarshaller, int containerSize, Object container, ImmutableSerializationContext ctx, TagReader in) private static voidreadContainerWithWrappedElements(BaseMarshaller<?> containerMarshaller, int containerSize, Object container, ImmutableSerializationContext ctx, TagReader in) private static <T> TreadCustomObject(int tag, ImmutableSerializationContext ctx, TagReader in) private static <T> TreadMessage(ImmutableSerializationContext ctx, TagReader in, boolean nulls) toString()private static <T> WrappedMessage.ValueOrTag<T> tryReadPrimitive(TagReader in, boolean nulls) private static booleantryWritePrimitive(TagWriter out, Object t, boolean nulls) (package private) static voidwrite(ImmutableSerializationContext ctx, TagWriter out, Object t) private static voidwriteContainer(ImmutableSerializationContext ctx, TagWriter out, BaseMarshallerDelegate marshallerDelegate, Object container) private static voidwriteContainerElementWrapped(ImmutableSerializationContext ctx, TagWriter out, ByteArrayOutputStreamEx buffer, Object e) private static voidwriteContainerWithoutWrappingElements(BaseMarshaller containerMarshaller, int containerSize, Object container, ImmutableSerializationContext ctx, TagWriter out) private static voidwriteContainerWrappingElements(BaseMarshaller containerMarshaller, int containerSize, Object container, ImmutableSerializationContext ctx, TagWriter out, ByteArrayOutputStreamEx buffer) private static <T> voidwriteCustomObject(ImmutableSerializationContext ctx, TagWriter out, T t) private static voidwriteMessage(ImmutableSerializationContext ctx, TagWriter out, Object t, boolean nulls)
-
Field Details
-
PROTOBUF_TYPE_NAME
The fully qualified Protobuf type name of this message. This type is defined in message-wrapping.proto.- See Also:
-
PROTOBUF_TYPE_ID
public static final int PROTOBUF_TYPE_IDThe type id of WrappedMessage itself.- See Also:
-
PROTO_FILE
Full path of the message-wrapping.proto resource file in classpath.- See Also:
-
WRAPPED_DOUBLE
public static final int WRAPPED_DOUBLEA wrapped double.- See Also:
-
WRAPPED_FLOAT
public static final int WRAPPED_FLOATA wrapped float.- See Also:
-
WRAPPED_INT64
public static final int WRAPPED_INT64A wrapped int64.- See Also:
-
WRAPPED_UINT64
public static final int WRAPPED_UINT64A wrapped uint64.- See Also:
-
WRAPPED_INT32
public static final int WRAPPED_INT32A wrapped int32.- See Also:
-
WRAPPED_FIXED64
public static final int WRAPPED_FIXED64A wrapped fixed64.- See Also:
-
WRAPPED_FIXED32
public static final int WRAPPED_FIXED32A wrapped fixed32.- See Also:
-
WRAPPED_BOOL
public static final int WRAPPED_BOOLA wrapped bool.- See Also:
-
WRAPPED_STRING
public static final int WRAPPED_STRINGA wrapped string.- See Also:
-
WRAPPED_CHAR
public static final int WRAPPED_CHARA wrapped char (marshalled as int32).- See Also:
-
WRAPPED_SHORT
public static final int WRAPPED_SHORTA wrapped short (marshalled as int32).- See Also:
-
WRAPPED_BYTE
public static final int WRAPPED_BYTEA wrapped byte (marshalled as int32).- See Also:
-
WRAPPED_DATE_MILLIS
public static final int WRAPPED_DATE_MILLISA wrapped java.util.Date (marshalled as int64).- See Also:
-
WRAPPED_INSTANT_SECONDS
public static final int WRAPPED_INSTANT_SECONDSA wrapped java.time.Instant (marshalled as int64 (seconds) and an int32 (nanos)).- See Also:
-
WRAPPED_INSTANT_NANOS
public static final int WRAPPED_INSTANT_NANOSThe nanoseconds of the java.time.Instant.- See Also:
-
WRAPPED_BYTES
public static final int WRAPPED_BYTESA wrapped bytes.- See Also:
-
WRAPPED_UINT32
public static final int WRAPPED_UINT32A wrapped uint32.- See Also:
-
WRAPPED_SFIXED32
public static final int WRAPPED_SFIXED32A wrapped sfixed32.- See Also:
-
WRAPPED_SFIXED64
public static final int WRAPPED_SFIXED64A wrapped sfixed64.- See Also:
-
WRAPPED_SINT32
public static final int WRAPPED_SINT32A wrapped sint32.- See Also:
-
WRAPPED_SINT64
public static final int WRAPPED_SINT64A wrapped sint64.- See Also:
-
WRAPPED_TYPE_NAME
public static final int WRAPPED_TYPE_NAMEThe name of the fully qualified message or enum type name, when the wrapped object is a message or enum.- See Also:
-
WRAPPED_DESCRIPTOR_FULL_NAME
Deprecated.UseWRAPPED_TYPE_NAMEinstead. This will be removed in ver. 5.- See Also:
-
WRAPPED_MESSAGE
public static final int WRAPPED_MESSAGEA byte array containing the encoded message.- See Also:
-
WRAPPED_ENUM
public static final int WRAPPED_ENUMThe enum value.- See Also:
-
WRAPPED_TYPE_ID
public static final int WRAPPED_TYPE_IDThe (optional) numeric type id of the wrapped message or enum. This is an alternative toWRAPPED_TYPE_NAME.- See Also:
-
WRAPPED_DESCRIPTOR_TYPE_ID
Deprecated.UseWRAPPED_TYPE_IDinstead. This will be removed in ver. 5.- See Also:
-
WRAPPED_DESCRIPTOR_ID
Deprecated.UseWRAPPED_TYPE_IDinstead. This will be removed in ver. 5.- See Also:
-
WRAPPED_EMPTY
public static final int WRAPPED_EMPTYA flag indicating and empty/null message.- See Also:
-
WRAPPED_CONTAINER_SIZE
public static final int WRAPPED_CONTAINER_SIZEThe (optional) number of repeated elements.- See Also:
-
WRAPPED_CONTAINER_TYPE_NAME
public static final int WRAPPED_CONTAINER_TYPE_NAME- See Also:
-
WRAPPED_CONTAINER_TYPE_ID
public static final int WRAPPED_CONTAINER_TYPE_ID- See Also:
-
WRAPPED_CONTAINER_MESSAGE
public static final int WRAPPED_CONTAINER_MESSAGE- See Also:
-
CONTAINER_SIZE_CONTEXT_PARAM
- See Also:
-
value
The wrapped object or (boxed) primitive. Can also be an array or Collection. -
MARSHALLER
Marshaller for WrappedMessage. This marshaller is not meant to handle unknown fields at the top level as they are very unlikely to ever appear. The handling of unknown fields for the inner message type will work as usual.
-
-
Constructor Details
-
WrappedMessage
-
-
Method Details
-
getValue
Returns the wrapped value, which is either a primitive, an enum, or a message. The value can benullalso. -
write
- Throws:
IOException
-
writeMessage
private static void writeMessage(ImmutableSerializationContext ctx, TagWriter out, Object t, boolean nulls) throws IOException - Throws:
IOException
-
tryWritePrimitive
- Throws:
IOException
-
writeCustomObject
private static <T> void writeCustomObject(ImmutableSerializationContext ctx, TagWriter out, T t) throws IOException - Throws:
IOException
-
writeContainer
private static void writeContainer(ImmutableSerializationContext ctx, TagWriter out, BaseMarshallerDelegate marshallerDelegate, Object container) throws IOException - Throws:
IOException
-
writeContainerWrappingElements
private static void writeContainerWrappingElements(BaseMarshaller containerMarshaller, int containerSize, Object container, ImmutableSerializationContext ctx, TagWriter out, ByteArrayOutputStreamEx buffer) throws IOException - Throws:
IOException
-
writeContainerWithoutWrappingElements
private static void writeContainerWithoutWrappingElements(BaseMarshaller containerMarshaller, int containerSize, Object container, ImmutableSerializationContext ctx, TagWriter out) throws IOException - Throws:
IOException
-
writeContainerElementWrapped
private static void writeContainerElementWrapped(ImmutableSerializationContext ctx, TagWriter out, ByteArrayOutputStreamEx buffer, Object e) throws IOException - Throws:
IOException
-
read
- Throws:
IOException
-
readMessage
private static <T> T readMessage(ImmutableSerializationContext ctx, TagReader in, boolean nulls) throws IOException - Throws:
IOException
-
tryReadPrimitive
private static <T> WrappedMessage.ValueOrTag<T> tryReadPrimitive(TagReader in, boolean nulls) throws IOException - Throws:
IOException
-
readCustomObject
private static <T> T readCustomObject(int tag, ImmutableSerializationContext ctx, TagReader in) throws IOException - Throws:
IOException
-
readContainer
private static Object readContainer(ImmutableSerializationContext ctx, TagReader in, int tag) throws IOException - Throws:
IOException
-
readContainerWithWrappedElements
private static void readContainerWithWrappedElements(BaseMarshaller<?> containerMarshaller, int containerSize, Object container, ImmutableSerializationContext ctx, TagReader in) throws IOException - Throws:
IOException
-
readContainerWithoutWrappedElements
private static void readContainerWithoutWrappedElements(BaseMarshaller<?> containerMarshaller, int containerSize, Object container, ImmutableSerializationContext ctx, TagReader in) throws IOException - Throws:
IOException
-
readContainerElementWrapped
private static <E> E readContainerElementWrapped(ImmutableSerializationContext ctx, TagReader in) throws IOException - Throws:
IOException
-
mapTypeIdIn
Map type id to new value during reading, to support schema evolution. -
mapTypeIdOut
Map type id to old value, during writing, to support schema evolution. -
equals
-
hashCode
-
toString
-
WRAPPED_TYPE_NAMEinstead.