writer.write(interaction.typesInitialText().getBytes());
writer.flush();
output.add(interaction.typesInitialText());
}
Sentence sentence = new SentenceImpl();
// we have an extra check to figure out whether EOF was reached - using last expected response
while (!reachedEOF && (i = reader.read()) != -1) {
// add the character
sentence.append((char) i);
boolean shouldTerminate = interaction.shouldTerminate(sentence);
String answer = interaction.repliesTo(sentence);
// sentence was not empty, reply
if (answer != null) {
sentence.append(answer);
writer.flush();
writer.write(answer.getBytes());
writer.flush();
}
if (shouldTerminate) {
runningProcess.markAsFinished();
runningProcess.terminate();
}
reachedEOF = runningProcess.isMarkedAsFinished();
// save and print output
if (sentence.isFinished()) {
sentence.trim();
log.log(Level.FINEST, "({0}): {1}", new Object[] { result.processName(), sentence });
output.add(sentence.toString());
// propagate output/error to user
if (interaction.shouldOutput(sentence)) {
System.out.println(interaction.transform(sentence));
}
if (interaction.shouldOutputToErr(sentence)) {
System.err.println(interaction.transform(sentence));
}
sentence.reset();
}
}
// handle last line
if (!sentence.isEmpty()) {
log.log(Level.FINEST, "{0} outputs: {1}", new Object[] { result.processName(), sentence });
output.add(sentence.toString());
// propagate output/error to user
if (interaction.shouldOutput(sentence)) {
System.out.println(interaction.transform(sentence));
}
if (interaction.shouldOutputToErr(sentence)) {