String frMsg = header + frBody;
String esBody = "¿Cómo estás?";
String esMsg = header + esBody;
// defaults to UTF-8
MultiportSyslogHandler handler = new MultiportSyslogHandler(
1000, 10, new ChannelProcessor(new ReplicatingChannelSelector()),
new SourceCounter("test"), "port",
new ThreadSafeDecoder(Charsets.UTF_8),
new ConcurrentHashMap<Integer, ThreadSafeDecoder>(),
null);
ParsedBuffer parsedBuf = new ParsedBuffer();
parsedBuf.incomplete = false;
// should be able to encode/decode any of these messages in UTF-8 or ISO
String[] bodies = { enBody, esBody, frBody };
String[] msgs = { enMsg, esMsg, frMsg };
Charset[] charsets = { Charsets.UTF_8, Charsets.ISO_8859_1 };
for (Charset charset : charsets) {
for (int i = 0; i < msgs.length; i++) {
String msg = msgs[i];
String body = bodies[i];
parsedBuf.buffer = IoBuffer.wrap(msg.getBytes(charset));
Event evt = handler.parseEvent(parsedBuf, charset.newDecoder());
String result = new String(evt.getBody(), charset);
// this doesn't work with non-UTF-8 chars... not sure why...
Assert.assertEquals(charset + " parse error: " + msg, body, result);
Assert.assertNull(
evt.getHeaders().get(SyslogUtils.EVENT_STATUS));
}
}
// Construct an invalid UTF-8 sequence.
// The parser should still generate an Event, but mark it as INVALID.
byte[] badUtf8Seq = enMsg.getBytes(Charsets.ISO_8859_1);
int badMsgLen = badUtf8Seq.length;
badUtf8Seq[badMsgLen - 2] = (byte)0xFE; // valid ISO-8859-1, invalid UTF-8
badUtf8Seq[badMsgLen - 1] = (byte)0xFF; // valid ISO-8859-1, invalid UTF-8
parsedBuf.buffer = IoBuffer.wrap(badUtf8Seq);
Event evt = handler.parseEvent(parsedBuf, Charsets.UTF_8.newDecoder());
Assert.assertEquals("event body: " +
new String(evt.getBody(), Charsets.ISO_8859_1) +
" and my default charset = " + Charset.defaultCharset() +
" with event = " + evt,
SyslogUtils.SyslogStatus.INVALID.getSyslogStatus(),