Top Description Inners Fields Constructors Methods
org.springframework.http.codec.protobuf

public Class ProtobufDecoder

extends ProtobufCodecSupport
implements Decoder<Message>
Class Inheritance
All Implemented Interfaces
org.springframework.core.codec.Decoder
Imports
java.io.IOException, java.lang.reflect.Method, java.nio.ByteBuffer, java.util.ArrayList, .List, .Map, java.util.concurrent.ConcurrentMap, java.util.function.Function, com.google.protobuf.CodedInputStream, .ExtensionRegistry, .Message, org.reactivestreams.Publisher, reactor.core.publisher.Flux, .Mono, org.springframework.core.ResolvableType, org.springframework.core.codec.Decoder, .DecodingException, org.springframework.core.io.buffer.DataBuffer, .DataBufferLimitException, .DataBufferUtils, org.springframework.lang.Nullable, org.springframework.util.Assert, .ConcurrentReferenceHashMap, .MimeType

A Decoder that reads com.google.protobuf.Messages using Google Protocol Buffers.

Flux deserialized via decode(Publisher, ResolvableType, MimeType, Map) are expected to use delimited Protobuf messages with the size of each message specified before the message itself. Single values deserialized via decodeToMono(Publisher, ResolvableType, MimeType, Map) are expected to use regular Protobuf message format (without the size prepended before the message).

Notice that default instance of Protobuf message produces empty byte array, so Mono.just(Msg.getDefaultInstance()) sent over the network will be deserialized as an empty Mono.

To generate Message Java classes, you need to install the protoc binary.

This decoder requires Protobuf 3 or higher, and supports "application/x-protobuf" and "application/octet-stream" with the official "com.google.protobuf:protobuf-java" library.

Author
Sebastien Deleuze
Since
5.1
See Also
ProtobufEncoder

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
protected static final int
DEFAULT_MESSAGE_MAX_SIZE

The default max size for aggregating messages.

private final ExtensionRegistry
private int
private static final ConcurrentMap<Class<?>, Method>
Inherited from org.springframework.http.codec.protobuf.ProtobufCodecSupport:
DELIMITED_KEYDELIMITED_VALUEMIME_TYPES

Constructor Summary

AccessConstructor and Description
public
ProtobufDecoder()

Construct a new ProtobufDecoder.

public
ProtobufDecoder(ExtensionRegistry
a message extension registry
extensionRegistry
)

Construct a new ProtobufDecoder with an initializer that allows the registration of message extensions.

Method Summary

Modifier and TypeMethod and Description
public boolean
canDecode(ResolvableType
the target element type for the output stream
elementType
,
MimeType
the mime type associated with the stream to decode (can be null if not specified)
mimeType
)

Implements org.springframework.core.codec.Decoder.canDecode.

Whether the decoder supports the given target element type and the MIME type of the source stream.
public Flux<Message>
decode(Publisher<DataBuffer>
the DataBuffer input stream to decode
inputStream
,
ResolvableType
the expected type of elements in the output stream; this type must have been previously passed to the canDecode method and it must have returned true.
elementType
,
MimeType
the MIME type associated with the input stream (optional)
mimeType
,
Map<String, Object>
additional information about how to do decode
hints
)

Implements org.springframework.core.codec.Decoder.decode.

Decode a DataBuffer input stream into a Flux of T.
public Message
decode(DataBuffer
the DataBuffer to decode
dataBuffer
,
ResolvableType
the expected output type
targetType
,
MimeType
the MIME type associated with the data
mimeType
,
Map<String, Object>
additional information about how to do decode
hints
)

Overrides default org.springframework.core.codec.Decoder.decode.

Decode a data buffer to an Object of type T.
public Mono<Message>
decodeToMono(Publisher<DataBuffer>
the DataBuffer input stream to decode
inputStream
,
ResolvableType
the expected type of elements in the output stream; this type must have been previously passed to the canDecode method and it must have returned true.
elementType
,
MimeType
the MIME type associated with the input stream (optional)
mimeType
,
Map<String, Object>
additional information about how to do decode
hints
)

Implements org.springframework.core.codec.Decoder.decodeToMono.

Decode a DataBuffer input stream into a Mono of T.
public List<MimeType>
getDecodableMimeTypes()

Implements org.springframework.core.codec.Decoder.getDecodableMimeTypes.

Return the list of MIME types supported by this Decoder.
public int
getMaxMessageSize()

Return the configured message size limit.

private static Message.Builder
getMessageBuilder(Class<?> clazz)

Create a new Message.Builder instance for the given class.

public void
setMaxMessageSize(int
the max size per message, or -1 for unlimited
maxMessageSize
)

The max size allowed per message.

Inherited from org.springframework.http.codec.protobuf.ProtobufCodecSupport:
getMimeTypessupportsMimeType

Field Detail

DEFAULT_MESSAGE_MAX_SIZEback to summary
protected static final int DEFAULT_MESSAGE_MAX_SIZE

The default max size for aggregating messages.

extensionRegistryback to summary
private final ExtensionRegistry extensionRegistry
maxMessageSizeback to summary
private int maxMessageSize
methodCacheback to summary
private static final ConcurrentMap<Class<?>, Method> methodCache

Constructor Detail

ProtobufDecoderback to summary
public ProtobufDecoder()

Construct a new ProtobufDecoder.

ProtobufDecoderback to summary
public ProtobufDecoder(ExtensionRegistry extensionRegistry)

Construct a new ProtobufDecoder with an initializer that allows the registration of message extensions.

Parameters
extensionRegistry:ExtensionRegistry

a message extension registry

Method Detail

canDecodeback to summary
public boolean canDecode(ResolvableType elementType, MimeType mimeType)

Implements org.springframework.core.codec.Decoder.canDecode.

Doc from org.springframework.core.codec.Decoder.canDecode.

Whether the decoder supports the given target element type and the MIME type of the source stream.

Parameters
elementType:ResolvableType

the target element type for the output stream

mimeType:MimeType

the mime type associated with the stream to decode (can be null if not specified)

Returns:boolean

true if supported, false otherwise

Annotations
@Override
decodeback to summary
public Flux<Message> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints)

Implements org.springframework.core.codec.Decoder.decode.

Doc from org.springframework.core.codec.Decoder.decode.

Decode a DataBuffer input stream into a Flux of T.

Parameters
inputStream:Publisher<DataBuffer>

the DataBuffer input stream to decode

elementType:ResolvableType

the expected type of elements in the output stream; this type must have been previously passed to the canDecode method and it must have returned true.

mimeType:MimeType

the MIME type associated with the input stream (optional)

hints:Map<String, Object>

additional information about how to do decode

Returns:Flux<Message>

the output stream with decoded elements

Annotations
@Override
decodeback to summary
public Message decode(DataBuffer dataBuffer, ResolvableType targetType, MimeType mimeType, Map<String, Object> hints) throws DecodingException

Overrides default org.springframework.core.codec.Decoder.decode.

Doc from org.springframework.core.codec.Decoder.decode.

Decode a data buffer to an Object of type T. This is useful for scenarios, that distinct messages (or events) are decoded and handled individually, in fully aggregated form.

Parameters
dataBuffer:DataBuffer

the DataBuffer to decode

targetType:ResolvableType

the expected output type

mimeType:MimeType

the MIME type associated with the data

hints:Map<String, Object>

additional information about how to do decode

Returns:Message

the decoded value, possibly null

Annotations
@Override
decodeToMonoback to summary
public Mono<Message> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints)

Implements org.springframework.core.codec.Decoder.decodeToMono.

Doc from org.springframework.core.codec.Decoder.decodeToMono.

Decode a DataBuffer input stream into a Mono of T.

Parameters
inputStream:Publisher<DataBuffer>

the DataBuffer input stream to decode

elementType:ResolvableType

the expected type of elements in the output stream; this type must have been previously passed to the canDecode method and it must have returned true.

mimeType:MimeType

the MIME type associated with the input stream (optional)

hints:Map<String, Object>

additional information about how to do decode

Returns:Mono<Message>

the output stream with the decoded element

Annotations
@Override
getDecodableMimeTypesback to summary
public List<MimeType> getDecodableMimeTypes()

Implements org.springframework.core.codec.Decoder.getDecodableMimeTypes.

Doc from org.springframework.core.codec.Decoder.getDecodableMimeTypes.

Return the list of MIME types supported by this Decoder. The list may not apply to every possible target element type and calls to this method should typically be guarded via canDecode(elementType, null). The list may also exclude MIME types supported only for a specific element type. Alternatively, use getDecodableMimeTypes(ResolvableType) for a more precise list.

Returns:List<MimeType>

the list of supported MIME types

Annotations
@Override
getMaxMessageSizeback to summary
public int getMaxMessageSize()

Return the configured message size limit.

Since
5.1.11
getMessageBuilderback to summary
private static Message.Builder getMessageBuilder(Class<?> clazz) throws Exception

Create a new Message.Builder instance for the given class.

This method uses a ConcurrentHashMap for caching method lookups.

setMaxMessageSizeback to summary
public void setMaxMessageSize(int maxMessageSize)

The max size allowed per message.

By default, this is set to 256K.

Parameters
maxMessageSize:int

the max size per message, or -1 for unlimited

org.springframework.http.codec.protobuf back to summary

private Class ProtobufDecoder.MessageDecoderFunction

extends Object
implements Function<DataBuffer, Iterable<? extends Message>>
Class Inheritance
  • java.lang.Object
  • org.springframework.http.codec.protobuf.ProtobufDecoder.MessageDecoderFunction
All Implemented Interfaces
java.util.function.Function

Field Summary

Modifier and TypeField and Description
private final ResolvableType
private final int
private int
private int
private DataBuffer

Constructor Summary

AccessConstructor and Description
public
MessageDecoderFunction(ResolvableType elementType, int maxMessageSize)

Method Summary

Modifier and TypeMethod and Description
public Iterable<? extends Message>
apply(DataBuffer
the function argument
input
)

Implements java.util.function.Function.apply.

Applies this function to the given argument.
public void
private boolean

Returns:

true when the message size is parsed successfully, false when the message size is truncated
readMessageSize
(DataBuffer input)

Parse message size as a varint from the input stream, updating messageBytesToRead and offset fields if needed to allow processing of upcoming chunks.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

elementTypeback to summary
private final ResolvableType elementType
maxMessageSizeback to summary
private final int maxMessageSize
messageBytesToReadback to summary
private int messageBytesToRead
offsetback to summary
private int offset
outputback to summary
private DataBuffer output
Annotations
@Nullable

Constructor Detail

MessageDecoderFunctionback to summary
public MessageDecoderFunction(ResolvableType elementType, int maxMessageSize)

Method Detail

applyback to summary
public Iterable<? extends Message> apply(DataBuffer input)

Implements java.util.function.Function.apply.

Doc from java.util.function.Function.apply.

Applies this function to the given argument.

Parameters
input:DataBuffer

the function argument

Returns:Iterable<? extends Message>

the function result

Annotations
@Override
discardback to summary
public void discard()
readMessageSizeback to summary
private boolean readMessageSize(DataBuffer input)

Parse message size as a varint from the input stream, updating messageBytesToRead and offset fields if needed to allow processing of upcoming chunks. Inspired from CodedInputStream#readRawVarint32(int, java.io.InputStream)

Returns:boolean

true when the message size is parsed successfully, false when the message size is truncated

See Also
Base 128 Varints