final JsonArray output = new JsonArray();
final SlaveInfo slave = e.getSlave();
int lineBuffer = 160;
final StringWriter out = new StringWriter();
final StringWriter err = new StringWriter();
WriterOutputStream stdout = new WriterOutputStream(out, slave.getCharset(), lineBuffer, false);
WriterOutputStream stderr = new WriterOutputStream(err, slave.getCharset(), lineBuffer, false);
for (IEvent evt : e.getEventStream()) {
try {
JsonObject marker;
switch (evt.getType()) {
case SUITE_FAILURE:
case TEST_IGNORED_ASSUMPTION:
case TEST_IGNORED:
case TEST_STARTED:
case TEST_FINISHED:
case TEST_FAILURE:
flushBoth(output, out, err, stdout, stderr);
marker = new JsonObject();
marker.addProperty("event", evt.getType().toString());
marker.add("description", context.serialize(((IDescribable) evt).getDescription()));
if (evt instanceof FailureEvent) {
marker.add("failure", context.serialize(((FailureEvent) evt).getFailure()));
}
output.add(marker);
break;
// Flush streams only if there's interwoven output between them.
case APPEND_STDOUT:
flush(APPEND_STDERR, output, stderr, err);
stdout.write(((IStreamEvent) evt).getChunk());
break;
case APPEND_STDERR:
flush(APPEND_STDOUT, output, stdout, out);
stderr.write(((IStreamEvent) evt).getChunk());
break;
}
} catch (IOException ex) {
// Ignore.
}