Class ByteArrayLengthHeaderSerializer
java.lang.Object
org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer
org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer
- All Implemented Interfaces:
Aware
,ApplicationEventPublisherAware
,Deserializer<byte[]>
,Serializer<byte[]>
Reads data in an InputStream to a byte[]; data must be preceded by
a binary length (network byte order, not included in resulting byte[]).
Writes a byte[] to an OutputStream after a binary length.
The length field contains the length of data following the length
field. (network byte order).
The default length field is a 4 byte signed integer. During deserialization,
negative values will be rejected.
Other options are an unsigned byte, and unsigned short.
For other header formats, override
readHeader(InputStream)
and
writeHeader(OutputStream, int)
.- Since:
- 2.0
- Author:
- Gary Russell, Artem Bilan
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Default length-header field, allows for data up to 2**31-1 bytes.static final int
A single unsigned byte, for data up to 255 bytes.static final int
An unsigned short, for data up to 2**16 bytes.Fields inherited from class org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer
DEFAULT_MAX_MESSAGE_SIZE, logger
-
Constructor Summary
ConstructorDescriptionConstruct the serializer usingHEADER_SIZE_INT
.ByteArrayLengthHeaderSerializer
(int headerSize) Construct the serializer using the supplied header size. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
deserialize
(InputStream inputStream) Read the header from the stream and then reads the provided length from the stream and returns the data in a byte[].Include the length of the header in addition to the payload.protected boolean
Return true if the length header value includes its own length.protected int
read
(InputStream inputStream, byte[] buffer, boolean header) Read data from the socket and puts the data in buffer.protected int
readHeader
(InputStream inputStream) Read the header and returns the length of the data part.void
serialize
(byte[] bytes, OutputStream outputStream) Write the byte[] to the output stream, preceded by a 4 byte length in network byte order (big endian).void
setInclusive
(boolean inclusive) Set to true to set the length header to include the length of the header in addition to the payload.protected void
writeHeader
(OutputStream outputStream, int length) Write the header, according to the header format.Methods inherited from class org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer
checkClosure, getMaxMessageSize, publishEvent, setApplicationEventPublisher, setMaxMessageSize
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.core.serializer.Deserializer
deserializeFromByteArray
Methods inherited from interface org.springframework.core.serializer.Serializer
serializeToByteArray
-
Field Details
-
HEADER_SIZE_INT
public static final int HEADER_SIZE_INTDefault length-header field, allows for data up to 2**31-1 bytes.- See Also:
-
HEADER_SIZE_UNSIGNED_SHORT
public static final int HEADER_SIZE_UNSIGNED_SHORTAn unsigned short, for data up to 2**16 bytes.- See Also:
-
HEADER_SIZE_UNSIGNED_BYTE
public static final int HEADER_SIZE_UNSIGNED_BYTEA single unsigned byte, for data up to 255 bytes.- See Also:
-
-
Constructor Details
-
ByteArrayLengthHeaderSerializer
public ByteArrayLengthHeaderSerializer()Construct the serializer usingHEADER_SIZE_INT
. -
ByteArrayLengthHeaderSerializer
public ByteArrayLengthHeaderSerializer(int headerSize) Construct the serializer using the supplied header size. Valid header sizes areHEADER_SIZE_INT
(default),HEADER_SIZE_UNSIGNED_BYTE
andHEADER_SIZE_UNSIGNED_SHORT
- Parameters:
headerSize
- The header size.
-
-
Method Details
-
isInclusive
protected boolean isInclusive()Return true if the length header value includes its own length.- Returns:
- true if the length includes the header length.
- Since:
- 5.2
-
setInclusive
public void setInclusive(boolean inclusive) Set to true to set the length header to include the length of the header in addition to the payload. Valid header sizes areHEADER_SIZE_INT
(default),HEADER_SIZE_UNSIGNED_BYTE
andHEADER_SIZE_UNSIGNED_SHORT
and 4, 1 and 2 will be added to the payload length respectively.- Parameters:
inclusive
- true to include the header length.- Since:
- 5.2
- See Also:
-
inclusive
Include the length of the header in addition to the payload. Valid header sizes areHEADER_SIZE_INT
(default),HEADER_SIZE_UNSIGNED_BYTE
andHEADER_SIZE_UNSIGNED_SHORT
and 4, 1 and 2 will be added to the payload length respectively. Fluent API form ofsetInclusive(boolean)
.- Returns:
- the serializer.
- Since:
- 5.2
- See Also:
-
deserialize
Read the header from the stream and then reads the provided length from the stream and returns the data in a byte[]. Throws an IOException if the length field exceeds the maxMessageSize. Throws aSoftEndOfStreamException
if the stream is closed between messages.- Parameters:
inputStream
- The input stream.- Throws:
IOException
- Any IOException.
-
serialize
Write the byte[] to the output stream, preceded by a 4 byte length in network byte order (big endian).- Parameters:
bytes
- The bytes.outputStream
- The output stream.- Throws:
IOException
-
read
Read data from the socket and puts the data in buffer. Blocks until buffer is full or a socket timeout occurs.- Parameters:
inputStream
- The input stream.buffer
- the buffer into which the data should be readheader
- true if we are reading the header- Returns:
< 0
if socket closed and not in the middle of a message- Throws:
IOException
- Any IOException.
-
writeHeader
Write the header, according to the header format.- Parameters:
outputStream
- The output stream.length
- The length.- Throws:
IOException
- Any IOException.
-
readHeader
Read the header and returns the length of the data part.- Parameters:
inputStream
- The input stream.- Returns:
- The length of the data part.
- Throws:
IOException
- Any IOException.SoftEndOfStreamException
- if socket closes before any length data read.
-