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[]>
public class ByteArrayLengthHeaderSerializer extends AbstractByteArraySerializer
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
Fields Modifier and Type Field Description static int
HEADER_SIZE_INT
Default length-header field, allows for data up to 2**31-1 bytes.static int
HEADER_SIZE_UNSIGNED_BYTE
A single unsigned byte, for data up to 255 bytes.static int
HEADER_SIZE_UNSIGNED_SHORT
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
Constructors Constructor Description ByteArrayLengthHeaderSerializer()
Construct the serializer usingHEADER_SIZE_INT
ByteArrayLengthHeaderSerializer(int headerSize)
Construct the serializer using the supplied header size. -
Method Summary
Modifier and Type Method Description byte[]
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[].ByteArrayLengthHeaderSerializer
inclusive()
Include the length of the header in addition to the payload.protected boolean
isInclusive()
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:
- Constant Field Values
-
HEADER_SIZE_UNSIGNED_SHORT
public static final int HEADER_SIZE_UNSIGNED_SHORTAn unsigned short, for data up to 2**16 bytes.- See Also:
- Constant Field Values
-
HEADER_SIZE_UNSIGNED_BYTE
public static final int HEADER_SIZE_UNSIGNED_BYTEA single unsigned byte, for data up to 255 bytes.- See Also:
- Constant Field Values
-
-
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()
-
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:
setInclusive(boolean)
-
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.
-