Spring 集成 - TCP 出站通道在每行末尾添加不需要的 CRLF

Spring Integration - TCP Outbound Channel adds unwanted CRLF at the end of each line

我正在使用以下代码读取大文件并逐个通过 TCP 连接缓冲区发送文件内容。在每次发送结束时,TCP 通道都会添加一个 CRLF 字符。我不希望它出现在结果中,除非我添加它。

        final int BUFFER_SIZE = 65536;
    long bytesToSkip = 0;
    byte[] buffer = new byte[BUFFER_SIZE];

    try (RandomAccessFile rand = new RandomAccessFile(new File(requestModel.getFilePath()), "r");
    ) {

        rand.seek(bytesToSkip);
        while ((read = rand.read(buffer)) != -1) {

            MessageBuilder mb = MessageBuilder.withPayload(buffer).setHeaderIfAbsent(IpHeaders.CONNECTION_ID, connectionId);
            outMsgChannel.send(mb.build())

            buffer = new byte[BUFFER_SIZE];
        }
    }
    catch(Exceptions .............. 

添加新行的示例输出。 (两个缓冲区都很大。我只提到了每个缓冲区末尾导致问题的行)

包含缓冲区一

A quick brown fox jumps over the lazy dog

一只敏捷的棕色狐狸跳过 懒狗

A quick brown fox jumps over the

缓冲区二包含

lazy dog

如果没有不需要的 CRLF,那么我将不会在输出中遇到单行分成两行的问题。我只想在文件有的地方有新行。

the documentation

TCP is a streaming protocol; this means that some structure has to be provided to data transported over TCP, so the receiver can demarcate the data into discrete messages. Connection factories are configured to use (de)serializers to convert between the message payload and the bits that are sent over TCP. This is accomplished by providing a deserializer and serializer for inbound and outbound messages respectively. A number of standard (de)serializers are provided.

The ByteArrayCrlfSerializer, converts a byte array to a stream of bytes followed by carriage return and linefeed characters (\r\n). This is the default (de)serializer and can be used with telnet as a client, for example.

...

您需要一些方法来知道消息何时完成 - 底层网络可能会将您的消息打包,以便以块的形式接收。

ByteArrayRawSerializer 不向消息添加任何字符;它可能会满足您的需求。在读取端使用时,它使用套接字 EOF 来指示消息完成。