No; I meant make your custom serializer a bit smarter than the normal ones, and on the outbound side, serialize like the raw serializer...
Code:
public class CustomSerializer extends AbstractByteArraySerializer {
private static final byte ENQ = 0x05;
private static final byte EOT = 0x04;
/**
* Reads the data in the inputstream to a byte[]. Data must be terminated
* by CRLF (\r\n). Throws a {@link SoftEndOfStreamException} if the stream
* is closed immediately after the \r\n (i.e. no data is in the process of
* being read).
* Deserialization is identical to CRLF, but has
* special handling for Initial ENQ and Final EOT messages;
* Serialization is raw - no modification of payload.
*/
public byte[] deserialize(InputStream inputStream) throws IOException {
byte[] buffer = new byte[this.maxMessageSize];
int n = 0;
int bite;
if (logger.isDebugEnabled()) {
logger.debug("Available to read:" + inputStream.available());
}
while (true) {
bite = inputStream.read();
if (bite < 0 && n == 0) {
throw new SoftEndOfStreamException("Stream closed between payloads");
}
checkClosure(bite);
if (n > 0 && bite == '\n' && buffer[n-1] == '\r') {
break;
}
buffer[n++] = (byte) bite;
// handle special 1 byte messages that don't terminate with CRLF.
if (n == 1 && (bite == ENQ || bite == EOT)) {
n++; // need to increment so we don't shave off the last byte below
break;
}
if (n >= this.maxMessageSize) {
throw new IOException("CRLF not found before max message length: "
+ this.maxMessageSize);
}
};
byte[] assembledData = new byte[n-1];
System.arraycopy(buffer, 0, assembledData, 0, n-1);
return assembledData;
}
/**
* Writes the byte[] to the stream.
*/
public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
outputStream.write(bytes);
outputStream.flush();
}
}
That way, your module will receive an initial message with a payload 0x05, it must fully construct the reply (ACK = 0x06) and the serializer will simply write it to the socket; subsequent messages will be sent to the module (minus the terminating CRLF). Again, you reply with ACKs; finally you will receive a message with just 0x04 (EOT), to which you just reply an empty buffer (no data).
Hope that clears it up.