20. Stream Support

20.1 Introduction

In many cases application data is obtained from a stream. It is not recommended to send a reference to a Stream as a message payload to a consumer. Instead messages are created from data that is read from an input stream and message payloads are written to an output stream one by one.

20.2 Reading from streams

Spring Integration provides two adapters for streams. Both ByteStreamReadingMessageSource and
CharacterStreamReadingMessageSource implement MessageSource.
By configuring one of these within a channel-adapter element, the polling period can be configured,
and the Message Bus can automatically detect and schedule them. The byte stream version requires an
InputStream, and the character stream version requires a Reader as
the single constructor argument. The ByteStreamReadingMessageSource also accepts the 'bytesPerMessage'
property to determine how many bytes it will attempt to read into each Message. The
default value is 1024

20.3 Writing to streams

For target streams, there are also two implementations: ByteStreamWritingMessageHandler and
CharacterStreamWritingMessageHandler. Each requires a single constructor argument -
OutputStream for byte streams or Writer for character streams,
and each provides a second constructor that adds the optional 'bufferSize'. Since both of these
ultimately implement the MessageHandler interface, they can be referenced from a
channel-adapter configuration as described in more detail in
Section 2.2, “Channel Adapter”.