public class StringDecoder extends AbstractDataBufferDecoder<java.lang.String>
String
stream. Before decoding, this decoder
realigns the incoming data buffers so that each buffer ends with a newline.
This is to make sure that multibyte characters are decoded properly, and do not cross buffer
boundaries. The default delimiters (\n
, \r\n
)can be customized.
Partially inspired by Netty's DelimiterBasedFrameDecoder
.
CharSequenceEncoder
Modifier and Type | Field and Description |
---|---|
static java.nio.charset.Charset |
DEFAULT_CHARSET
The default charset to use, i.e.
|
static java.util.List<java.lang.String> |
DEFAULT_DELIMITERS
The default delimiter strings to use, i.e.
|
private java.util.List<java.lang.String> |
delimiters |
private static DataBuffer |
END_FRAME |
private boolean |
stripDelimiter |
Modifier | Constructor and Description |
---|---|
private |
StringDecoder(java.util.List<java.lang.String> delimiters,
boolean stripDelimiter,
MimeType... mimeTypes) |
Modifier and Type | Method and Description |
---|---|
static StringDecoder |
allMimeTypes()
Create a
StringDecoder that supports all MIME types. |
static StringDecoder |
allMimeTypes(boolean ignored)
Deprecated.
as of Spring 5.0.4, in favor of
allMimeTypes() or
allMimeTypes(List, boolean) . |
static StringDecoder |
allMimeTypes(java.util.List<java.lang.String> delimiters,
boolean stripDelimiter)
Create a
StringDecoder that supports all MIME types. |
boolean |
canDecode(ResolvableType elementType,
MimeType mimeType)
Whether the decoder supports the given target element type and the MIME
type of the source stream.
|
<any> |
decode(<any> inputStream,
ResolvableType elementType,
MimeType mimeType,
java.util.Map<java.lang.String,java.lang.Object> hints)
Decode a
DataBuffer input stream into a Flux of T . |
protected java.lang.String |
decodeDataBuffer(DataBuffer dataBuffer,
ResolvableType elementType,
MimeType mimeType,
java.util.Map<java.lang.String,java.lang.Object> hints)
How to decode a
DataBuffer to the target element type. |
private static java.nio.charset.Charset |
getCharset(MimeType mimeType) |
private java.util.List<byte[]> |
getDelimiterBytes(MimeType mimeType) |
private static int |
indexOf(DataBuffer dataBuffer,
byte[] delimiter)
Finds the given delimiter in the given data buffer.
|
private static boolean |
isEndFrame(DataBuffer dataBuffer)
Checks whether the given buffer is
END_FRAME . |
private static <any> |
joinUntilEndFrame(java.util.List<DataBuffer> dataBuffers)
Joins the given list of buffers into a single buffer.
|
private <any> |
splitOnDelimiter(DataBuffer dataBuffer,
java.util.List<byte[]> delimiterBytes)
Splits the given data buffer on delimiter boundaries.
|
static StringDecoder |
textPlainOnly()
Create a
StringDecoder for "text/plain" . |
static StringDecoder |
textPlainOnly(boolean ignored)
Deprecated.
as of Spring 5.0.4, in favor of
textPlainOnly() or
textPlainOnly(List, boolean) . |
static StringDecoder |
textPlainOnly(java.util.List<java.lang.String> delimiters,
boolean stripDelimiter)
Create a
StringDecoder for "text/plain" . |
decodeToMono
getDecodableMimeTypes
private static final DataBuffer END_FRAME
public static final java.nio.charset.Charset DEFAULT_CHARSET
public static final java.util.List<java.lang.String> DEFAULT_DELIMITERS
\n
and \r\n
.private final java.util.List<java.lang.String> delimiters
private final boolean stripDelimiter
private StringDecoder(java.util.List<java.lang.String> delimiters, boolean stripDelimiter, MimeType... mimeTypes)
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType)
Decoder
canDecode
in interface Decoder<java.lang.String>
canDecode
in class AbstractDecoder<java.lang.String>
elementType
- the target element type for the output streammimeType
- the mime type associated with the stream to decode
(can be null
if not specified)true
if supported, false
otherwisepublic <any> decode(<any> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable java.util.Map<java.lang.String,java.lang.Object> hints)
Decoder
DataBuffer
input stream into a Flux of T
.decode
in interface Decoder<java.lang.String>
decode
in class AbstractDataBufferDecoder<java.lang.String>
inputStream
- the DataBuffer
input stream to decodeelementType
- the expected type of elements in the output stream;
this type must have been previously passed to the Decoder.canDecode(org.springframework.core.ResolvableType, org.springframework.util.MimeType)
method and it must have returned true
.mimeType
- the MIME type associated with the input stream (optional)hints
- additional information about how to do encodeprivate <any> splitOnDelimiter(DataBuffer dataBuffer, java.util.List<byte[]> delimiterBytes)
END_FRAME
buffer after each delimiter.private static int indexOf(DataBuffer dataBuffer, byte[] delimiter)
private static boolean isEndFrame(DataBuffer dataBuffer)
END_FRAME
.private static <any> joinUntilEndFrame(java.util.List<DataBuffer> dataBuffers)
protected java.lang.String decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable java.util.Map<java.lang.String,java.lang.Object> hints)
AbstractDataBufferDecoder
DataBuffer
to the target element type.decodeDataBuffer
in class AbstractDataBufferDecoder<java.lang.String>
@Deprecated public static StringDecoder textPlainOnly(boolean ignored)
textPlainOnly()
or
textPlainOnly(List, boolean)
.StringDecoder
for "text/plain"
.ignored
- ignoredpublic static StringDecoder textPlainOnly()
StringDecoder
for "text/plain"
.public static StringDecoder textPlainOnly(java.util.List<java.lang.String> delimiters, boolean stripDelimiter)
StringDecoder
for "text/plain"
.@Deprecated public static StringDecoder allMimeTypes(boolean ignored)
allMimeTypes()
or
allMimeTypes(List, boolean)
.StringDecoder
that supports all MIME types.ignored
- ignoredpublic static StringDecoder allMimeTypes()
StringDecoder
that supports all MIME types.public static StringDecoder allMimeTypes(java.util.List<java.lang.String> delimiters, boolean stripDelimiter)
StringDecoder
that supports all MIME types.