Package org.springframework.core.codec
Class StringDecoder
java.lang.Object
org.springframework.core.codec.AbstractDecoder<T>
org.springframework.core.codec.AbstractDataBufferDecoder<String>
org.springframework.core.codec.StringDecoder
Decode from a data buffer stream to a
String stream, either splitting
or aggregating incoming data chunks to realign along newlines delimiters
and produce a stream of strings. This is useful for streaming but is also
necessary to ensure that multi-byte characters can be decoded correctly,
avoiding split-character issues. The default delimiters used by default are
\n and \r\n but that can be customized.- Since:
- 5.0
- Author:
- Sebastien Deleuze, Brian Clozel, Arjen Poutsma, Mark Paluch
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CharsetThe default charset to use, i.e.The default delimiter strings to use, i.e.Fields inherited from class org.springframework.core.codec.AbstractDecoder
logger -
Method Summary
Modifier and TypeMethodDescriptionstatic StringDecoderCreate aStringDecoderthat supports all MIME types.static StringDecoderallMimeTypes(List<String> delimiters, boolean stripDelimiter) Create aStringDecoderthat supports all MIME types.booleancanDecode(ResolvableType elementType, MimeType mimeType) Whether the decoder supports the given target element type and the MIME type of the source stream.reactor.core.publisher.Flux<String>decode(Publisher<DataBuffer> input, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) Decode aDataBufferinput stream into a Flux ofT.decode(DataBuffer dataBuffer, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) Decode a data buffer to an Object of type T.Return the configureddefaultCharset.voidsetDefaultCharset(Charset defaultCharset) Set the default character set to fall back on if the MimeType does not specify any.static StringDecoderCreate aStringDecoderfor"text/plain".static StringDecodertextPlainOnly(List<String> delimiters, boolean stripDelimiter) Create aStringDecoderfor"text/plain".Methods inherited from class org.springframework.core.codec.AbstractDataBufferDecoder
decodeDataBuffer, decodeToMono, getMaxInMemorySize, setMaxInMemorySizeMethods inherited from class org.springframework.core.codec.AbstractDecoder
getDecodableMimeTypes, getLogger, setLoggerMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.core.codec.Decoder
getDecodableMimeTypes
-
Field Details
-
DEFAULT_CHARSET
The default charset to use, i.e. "UTF-8". -
DEFAULT_DELIMITERS
The default delimiter strings to use, i.e.\r\nand\n.
-
-
Method Details
-
setDefaultCharset
Set the default character set to fall back on if the MimeType does not specify any.By default this is
UTF-8.- Parameters:
defaultCharset- the charset to fall back on- Since:
- 5.2.9
-
getDefaultCharset
Return the configureddefaultCharset.- Since:
- 5.2.9
-
canDecode
Description copied from interface:DecoderWhether the decoder supports the given target element type and the MIME type of the source stream.- Specified by:
canDecodein interfaceDecoder<String>- Overrides:
canDecodein classAbstractDecoder<String>- Parameters:
elementType- the target element type for the output streammimeType- the mime type associated with the stream to decode (can benullif not specified)- Returns:
trueif supported,falseotherwise
-
decode
public reactor.core.publisher.Flux<String> decode(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Description copied from interface:DecoderDecode aDataBufferinput stream into a Flux ofT.- Specified by:
decodein interfaceDecoder<String>- Overrides:
decodein classAbstractDataBufferDecoder<String>- Parameters:
input- theDataBufferinput stream to decodeelementType- the expected type of elements in the output stream; this type must have been previously passed to theDecoder.canDecode(org.springframework.core.ResolvableType, org.springframework.util.MimeType)method and it must have returnedtrue.mimeType- the MIME type associated with the input stream (optional)hints- additional information about how to do decode- Returns:
- the output stream with decoded elements
-
decode
public String decode(DataBuffer dataBuffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Description copied from interface:DecoderDecode 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- theDataBufferto decodeelementType- the expected output typemimeType- the MIME type associated with the datahints- additional information about how to do decode- Returns:
- the decoded value, possibly
null
-
textPlainOnly
Create aStringDecoderfor"text/plain". -
textPlainOnly
Create aStringDecoderfor"text/plain".- Parameters:
delimiters- delimiter strings to use to split the input streamstripDelimiter- whether to remove delimiters from the resulting input strings
-
allMimeTypes
Create aStringDecoderthat supports all MIME types. -
allMimeTypes
Create aStringDecoderthat supports all MIME types.- Parameters:
delimiters- delimiter strings to use to split the input streamstripDelimiter- whether to remove delimiters from the resulting input strings
-