Class ProtobufEncoder
java.lang.Object
io.micronaut.serde.LimitingStream
io.micronaut.serde.protobuf.ProtobufEncoder
- All Implemented Interfaces:
Encoder, KeysAwareEncoder, AutoCloseable
Protocol Buffers implementation of
Encoder.
Two things about the wire format shape this class:
- Tags are written lazily. Serde encodes the key before it knows whether the value is
null, but protobuf has no null — an absent field is simply not on the wire. So
encodeKey(String)only records the property, and the tag is written when a value actually arrives.encodeNull()discards the pending property instead. - Nested structures buffer. A nested message is length-prefixed, and its length is not
known until the structure is finished, so each nested message and each repeated field writes
into its own buffer and is copied into its parent by
finishStructure(). Those buffers are pooled on the root encoder and reused for the rest of the message.
- Since:
- 3.2
-
Nested Class Summary
Nested classes/interfaces inherited from class LimitingStream
LimitingStream.RemainingLimits -
Field Summary
Fields inherited from class LimitingStream
DEFAULT_LIMITS, DEFAULT_MAXIMUM_DEPTH -
Constructor Summary
ConstructorsConstructorDescriptionProtobufEncoder(LimitingStream.RemainingLimits remainingLimits) Create a root encoder. -
Method Summary
Modifier and TypeMethodDescriptionReturn an analysis of the current path.encodeArray(io.micronaut.core.type.Argument<?> type) Encodes an array.voidencodeBigDecimal(BigDecimal value) Encode a BigDecimal.voidencodeBigInteger(BigInteger value) Encode a BigInteger.voidencodeBinary(byte[] data) Encode the given binary data.voidencodeBoolean(boolean value) Encode a boolean.voidencodeByte(byte value) Encode a byte.voidencodeChar(char value) Encode a char.voidencodeDouble(double value) Encode a double.voidencodeFloat(float value) Encode a float.voidencodeInt(int value) Encode an int.voidEncode a key by index from the supplied key set.voidEncode a key.voidencodeLong(long value) Encode a long.voidEncodenull.encodeObject(io.micronaut.core.type.Argument<?> type) Encodes an object.voidencodeShort(short value) Encode a short.voidencodeString(String value) Encode a string.voidFinalize a previously created structure, like an array or object.byte[]Returns the encoded message.voidwriteTo(OutputStream outputStream) Write the encoded message to a stream.Methods inherited from class LimitingStream
childLimits, decreaseDepth, increaseDepth, limitsFromConfiguration, ourLimits
-
Constructor Details
-
ProtobufEncoder
Create a root encoder. A protobuf payload is a single message, so the root itself writes nothing — it exists to receive the top-levelencodeObject(Argument)call.- Parameters:
remainingLimits- The nesting limits
-
-
Method Details
-
toByteArray
public byte[] toByteArray()Returns the encoded message.- Returns:
- The bytes
-
writeTo
Write the encoded message to a stream.- Parameters:
outputStream- The stream- Throws:
IOException- If writing fails
-
encodeObject
Description copied from interface:EncoderEncodes an object.- Specified by:
encodeObjectin interfaceEncoder- Parameters:
type- The object type, nevernull- Returns:
- The encoder, never
null - Throws:
IOException- if an error occurs
-
encodeArray
Description copied from interface:EncoderEncodes an array.- Specified by:
encodeArrayin interfaceEncoder- Parameters:
type- The array type, nevernull- Returns:
- The encoder, never
null - Throws:
IOException- if an error occurs
-
finishStructure
Description copied from interface:EncoderFinalize a previously created structure, like an array or object.- Specified by:
finishStructurein interfaceEncoder- Throws:
IOException- If an error occurs
-
encodeKey
Description copied from interface:EncoderEncode a key.- Specified by:
encodeKeyin interfaceEncoder- Parameters:
key- The key, nevernull- Throws:
IOException- If an error occurs
-
encodeKey
Description copied from interface:KeysAwareEncoderEncode a key by index from the supplied key set.- Specified by:
encodeKeyin interfaceKeysAwareEncoder- Parameters:
keys- The keysindex- The key index- Throws:
IOException- If an unrecoverable error occurs
-
encodeString
Description copied from interface:EncoderEncode a string.- Specified by:
encodeStringin interfaceEncoder- Parameters:
value- The string, nevernull- Throws:
IOException- If an error occurs
-
encodeBoolean
Description copied from interface:EncoderEncode a boolean.- Specified by:
encodeBooleanin interfaceEncoder- Parameters:
value- The boolean- Throws:
IOException- If an error occurs
-
encodeByte
Description copied from interface:EncoderEncode a byte.- Specified by:
encodeBytein interfaceEncoder- Parameters:
value- The byte- Throws:
IOException- If an error occurs
-
encodeShort
Description copied from interface:EncoderEncode a short.- Specified by:
encodeShortin interfaceEncoder- Parameters:
value- The short- Throws:
IOException- If an error occurs
-
encodeChar
Description copied from interface:EncoderEncode a char.- Specified by:
encodeCharin interfaceEncoder- Parameters:
value- The char- Throws:
IOException- If an error occurs
-
encodeInt
Description copied from interface:EncoderEncode an int.- Specified by:
encodeIntin interfaceEncoder- Parameters:
value- The int- Throws:
IOException- If an error occurs
-
encodeLong
Description copied from interface:EncoderEncode a long.- Specified by:
encodeLongin interfaceEncoder- Parameters:
value- The long- Throws:
IOException- If an error occurs
-
encodeFloat
Description copied from interface:EncoderEncode a float.- Specified by:
encodeFloatin interfaceEncoder- Parameters:
value- The float- Throws:
IOException- If an error occurs
-
encodeDouble
Description copied from interface:EncoderEncode a double.- Specified by:
encodeDoublein interfaceEncoder- Parameters:
value- The double- Throws:
IOException- If an error occurs
-
encodeBigInteger
Description copied from interface:EncoderEncode a BigInteger.- Specified by:
encodeBigIntegerin interfaceEncoder- Parameters:
value- The BigInteger, nevernull- Throws:
IOException- If an error occurs
-
encodeBigDecimal
Description copied from interface:EncoderEncode a BigDecimal.- Specified by:
encodeBigDecimalin interfaceEncoder- Parameters:
value- The BigDecimal, nevernull- Throws:
IOException- If an error occurs
-
encodeBinary
Description copied from interface:EncoderEncode the given binary data. The shape of the data in the output is unspecified, the only requirement is that the equivalentDecoder.decodeBinary()must be able to parse to the same data.- Specified by:
encodeBinaryin interfaceEncoder- Parameters:
data- The input data- Throws:
IOException
-
encodeNull
Description copied from interface:EncoderEncodenull.- Specified by:
encodeNullin interfaceEncoder- Throws:
IOException- If an error occurs
-
currentPath
Description copied from interface:EncoderReturn an analysis of the current path.- Specified by:
currentPathin interfaceEncoder- Returns:
- The current path if known
-