2 Gary:
Hi, how are you? I use your code with modifications:
Code:
public byte[] deserialize(InputStream inputStream) throws IOException {
try {
byte[] buffer = new byte[this.maxMessageSize];
int n = 0;
int bite;
if (LOG.isDebugEnabled()) {
LOG.debug("Some data arrived");
}
while (true) {
try {
bite = inputStream.read();
if (bite < 0 && n == 0) {
throw new SoftEndOfStreamException("Stream closed between payloads");
}
checkClosure(bite);
//if message has non zero elements - end of message
if (n > 0 && bite == '\n') {
break;
}
//skipping \n data, because some devices send \n as TCP connection "heat beats"
if (bite != '\n') {
buffer[n++] = (byte) bite;
if (n >= this.maxMessageSize) {
throw new IOException("CRLF not found before max message length: "
+ this.maxMessageSize);
}
}
} catch (SocketTimeoutException timeoutException) {
break; //on timeout we trying to continue with all already read data
}
}
//if n is ZERO - mean socket closed without any payload
if (n == 0) {
// HERE IS PROBLEM!!! What should i do to correctly say to Spring - no message exist, continue work without Exceptions
throw new SoftEndOfStreamException("No data are readed from channel");
}
byte[] assembledData = new byte[n];
System.arraycopy(buffer, 0, assembledData, 0, n);
LOG.debug("some data readed, size {}", n);
return assembledData;
} catch (RuntimeException re) {
LOG.error("error during data reading", re);
throw re;
} catch (IOException e) {
LOG.error("IOException during data reading", e);
throw e;
}
}