/** * Error-preserving writer. * *

This is needed to work around the problem that {@link PrintWriter} throws * away error information. It can be interposed between a real {@link Writer} * and the surrounding formatting class to allow exceptions from the former to * be retrieved even if the latter doesn't expose them. */ class ReliableWriter extends Writer { /** Contained writer. */ private Writer child; /** Last reported error. */ private IOException error; /** * Construct a new reliable writer. * * @param child Writer to wrap */ ReliableWriter(Writer child) { this.child = child; } /** * Get the last error. * * @return The last error thrown by the wrapped writer */ public IOException getError() { return error; } public void close() throws IOException { try { child.close(); } catch(IOException e) { error = e; throw(e); } } public void flush() throws IOException { try { child.flush(); } catch(IOException e) { error = e; throw(e); } } public void write(char[] cbuf) throws IOException { try { child.write(cbuf); } catch(IOException e) { error = e; throw(e); } } public void write(char[] cbuf, int off, int len) throws IOException { try { child.write(cbuf, off, len); } catch(IOException e) { error = e; throw(e); } } public void write(int c) throws IOException { try { child.write(c); } catch(IOException e) { error = e; throw(e); } } public void write(String str) throws IOException { try { child.write(str); } catch(IOException e) { error = e; throw(e); } } public void write(String str, int off, int len) throws IOException { try { child.write(str, off, len); } catch(IOException e) { error = e; throw(e); } } }