Я получил несколько хороших идей по предоставленной ссылке и в итоге написал небольшой класс для обработки замены переменных $ VAR $ в потоке. Для потомков:
public class ReplacingOutputStream extends OutputStream {
private static final int DOLLAR_SIGN = "$".codePointAt(0);
private static final int BACKSLASH = "\\".codePointAt(0);
private final OutputStream delegate;
private final Map<String, Object> replacementValues;
private int previous = Integer.MIN_VALUE;
private boolean replacing = false;
private ArrayList<Integer> replacement = new ArrayList<Integer>();
public ReplacingOutputStream(OutputStream delegate, Map<String, Object> replacementValues) {
this.delegate = delegate;
this.replacementValues = replacementValues;
}
public @Override void write(int b) throws IOException {
if (b == DOLLAR_SIGN && previous != BACKSLASH) {
if (replacing) {
doReplacement();
replacing = false;
} else {
replacing = true;
}
} else {
if (replacing) {
replacement.add(b);
} else {
delegate.write(b);
}
}
previous = b;
}
private void doReplacement() throws IOException {
StringBuilder sb = new StringBuilder();
for (Integer intval : replacement) {
sb.append(Character.toChars(intval));
}
replacement.clear();
String oldValue = sb.toString();
Object _newValue = replacementValues.get(oldValue);
if (_newValue == null) {
throw new RuntimeException("Could not find replacement variable for value '"+oldValue+"'.");
}
String newValue = _newValue.toString();
for (int i=0; i < newValue.length(); ++i) {
int value = newValue.codePointAt(i);
delegate.write(value);
}
}
}