Class NoCloseWriter

java.lang.Object
java.io.Writer
org.apache.juneau.commons.io.NoCloseWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class NoCloseWriter extends Writer
A wrapper around a Writer that prevents the underlying writer from being closed.

This class wraps an existing Writer and intercepts the close() method, making it a no-op (except for flushing). All other operations are delegated to the underlying writer. This is useful when you need to pass a writer to code that will close it, but you want to keep the underlying writer open for further use.

Features:
  • Prevents closing - close() flushes but doesn't close the underlying writer
  • Transparent delegation - all other operations pass through to the wrapped writer
  • Useful for resource management - allows multiple consumers without premature closing
Use Cases:
  • Passing writers to APIs that close them, but you need to keep the writer open
  • Multiple operations on the same writer where intermediate operations might close it
  • Resource management scenarios where you control the writer lifecycle
  • Wrapping system writers that should not be closed
Usage:

// Wrap a writer that should not be closed FileWriter fw = new FileWriter("output.txt"); NoCloseWriter wrapper = new NoCloseWriter(fw); // Pass to code that might close it someMethod(wrapper); // May call close(), but fw remains open // Continue using the original writer fw.write("more data"); fw.close(); // Close when actually done

Important Notes:
  • The close() method flushes the writer but does not close the underlying writer
  • You are responsible for closing the underlying writer when you're done with it
  • This wrapper does not prevent resource leaks - ensure the underlying writer is eventually closed
See Also: