I am reading large files using the following code and sending the file content over a TCP connection buffer by buffer. At the end of each send the TCP channel adds a CRLF character. I don't want this to appear in the results unless I add it.
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 ..............
Sample output where new line is added. (Both the buffers are huge. I have mentioned only the lines causing problems at the end of each buffer)
Buffer One contained
A quick brown fox jumps over the lazy dog
A quick brown fox jumps over the lazy dog
A quick brown fox jumps over the
Buffer Two Contains
lazy dog
If there is no unwanted CRLF then I will not get the issue of single line splitting in to two in output.. I want to have new lines only where the file has.
See 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....
You need some way to know when the message is complete - the underlying network might packetize your message so it is received in chunks.
The ByteArrayRawSerializer
adds no characters to the message; it might satisfy your needs. When used on the reading side, it uses the socket EOF to indicate the message is complete.