BranchingWriter extends FilterWriter { @Override public void write(int c) throws IOException { if (branchActive) { trunk.write(c); branch.write(c); } else super.write(c); } BranchingWriter(Writer out); @Override void write(int c); @Override void write(char[] cbuf, int off, int len); @Override void write(String str, int off, int len); @Override void flush(); @Override void close(); void writeTrunk(String s); void writeBranch(String s); void closeUsingBranch();  }
@Test public void testSimpleWrite() throws IOException { StringWriter sw = new StringWriter(); String testString = "abc"; try (Writer writer = new BranchingWriter(sw)) { writer.write(testString); } assertEquals(testString, sw.toString()); }