diff --git a/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java index 157ba297cc..9d0db71396 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java @@ -18,6 +18,7 @@ package org.springframework.http.codec.protobuf; import java.io.IOException; import java.lang.reflect.Method; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -42,25 +43,28 @@ import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.MimeType; /** - * A {@code Decoder} that reads {@link com.google.protobuf.Message}s - * using Google Protocol Buffers. + * A {@code Decoder} that reads {@link com.google.protobuf.Message}s using + * Google Protocol Buffers. * *

Flux deserialized via * {@link #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 {@link #decodeToMono(Publisher, ResolvableType, MimeType, Map)} are expected to use - * regular Protobuf message format (without the size prepended before the message). + * + * delimited Protobuf messages with the size of each message specified before + * the message itself. Single values deserialized via + * {@link #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 - * {@code Mono.just(Msg.getDefaultInstance())} sent over the network will be deserialized - * as an empty {@link Mono}. + *

Notice that default instance of Protobuf message produces empty byte + * array, so {@code Mono.just(Msg.getDefaultInstance())} sent over the network + * will be deserialized as an empty {@link Mono}. * - *

To generate {@code Message} Java classes, you need to install the {@code protoc} binary. + *

To generate {@code Message} Java classes, you need to install the + * {@code protoc} binary. * *

This decoder requires Protobuf 3 or higher, and supports - * {@code "application/x-protobuf"} and {@code "application/octet-stream"} with the official - * {@code "com.google.protobuf:protobuf-java"} library. + * {@code "application/x-protobuf"} and {@code "application/octet-stream"} with + * the official {@code "com.google.protobuf:protobuf-java"} library. * * @author Sébastien Deleuze * @since 5.1 @@ -68,9 +72,7 @@ import org.springframework.util.MimeType; */ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder { - /** - * The default max size for aggregating messages. - */ + /** The default max size for aggregating messages. */ protected static final int DEFAULT_MESSAGE_MAX_SIZE = 64 * 1024; private static final ConcurrentMap, Method> methodCache = new ConcurrentReferenceHashMap<>(); @@ -123,7 +125,8 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder { try { Message.Builder builder = getMessageBuilder(elementType.toClass()); - builder.mergeFrom(CodedInputStream.newInstance(dataBuffer.asByteBuffer()), this.extensionRegistry); + ByteBuffer buffer = dataBuffer.asByteBuffer(); + builder.mergeFrom(CodedInputStream.newInstance(buffer), this.extensionRegistry); return builder.build(); } catch (IOException ex) { @@ -131,7 +134,8 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder apply(DataBuffer input) { try { @@ -189,8 +195,9 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder this.maxMessageSize) { throw new DecodingException( - "The number of bytes to read parsed in the incoming stream (" + - this.messageBytesToRead + ") exceeds the configured limit (" + this.maxMessageSize + ")"); + "The number of bytes to read from the incoming stream " + + "(" + this.messageBytesToRead + ") exceeds " + + "the configured limit (" + this.maxMessageSize + ")"); } this.output = input.factory().allocateBuffer(this.messageBytesToRead); } @@ -206,7 +213,8 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder