ByteArrayOutputStream () { return new ByteArrayOutputStream() { private boolean isClosed = false; private synchronized boolean markClosed() { return !isClosed && (isClosed = true); } @Override public void close() throws IOException { if (markClosed()) { try { super.close(); } finally { myContent = new BinaryContent(buf, 0, size()); if (myContext != null) { myContext.consumeOutputFile(OutputFileObject.this); } } } } }; }