@Override public OutputStream openOutputStream() throws IOException { modify(); if (delegate != null) { return delegate.first().openOutputStream(); } else { return new ByteArrayOutputStream() { boolean closed; @Override public void close() throws IOException { // prevent work when close() called multiple times if (closed) { return; } super.close(); closed = true; content = toByteArray(); writer.maybeFlush(); } }; } }