/** * Returns a Writer that sends all output to the given {@link Appendable} target. Closing the * writer will close the target if it is {@link Closeable}, and flushing the writer will flush the * target if it is {@link java.io.Flushable}. * * @param target the object to which output will be sent * @return a new Writer object, unless target is a Writer, in which case the target is returned */ public static Writer asWriter(Appendable target) { if (target instanceof Writer) { return (Writer) target; } return new AppendableWriter(target); } }
@Override public Writer append(char c) throws IOException { checkNotClosed(); target.append(c); return this; }
@Override public void flush() throws IOException { checkNotClosed(); if (target instanceof Flushable) { ((Flushable) target).flush(); } }
@Override public void write(int c) throws IOException { checkNotClosed(); target.append((char) c); }
@Override public void write(char cbuf[], int off, int len) throws IOException { checkNotClosed(); // It turns out that creating a new String is usually as fast, or faster // than wrapping cbuf in a light-weight CharSequence. target.append(new String(cbuf, off, len)); }
@Override public void write(@Nullable String str, int off, int len) throws IOException { checkNotClosed(); // tricky: append takes start, end pair... target.append(str, off, off + len); }
@Override public Writer append(@Nullable CharSequence charSeq, int start, int end) throws IOException { checkNotClosed(); target.append(charSeq, start, end); return this; }
@Override public void write(@Nullable String str) throws IOException { checkNotClosed(); target.append(str); }
@Override public Writer append(@Nullable CharSequence charSeq) throws IOException { checkNotClosed(); target.append(charSeq); return this; }