3 import java.io.IOException;
5 /** Fixing the problem caused by mixing subclassing and delegation in
6 * the <code>java.io.BufferedWriter</code>
8 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
10 // BEGIN: writer.final.2.0
11 public final class Writer implements Appendable {
12 private final Impl impl;
13 private final ImplSeq seq;
15 private Writer(Impl impl, ImplSeq seq) {
19 public final void write(int c) throws IOException {
21 char[] arr = {(char) c};
22 impl.write(arr, 0, 1);
24 seq.write(new CharSeq(c));
28 public final void write(char cbuf[]) throws IOException {
30 impl.write(cbuf, 0, cbuf.length);
32 seq.write(new CharSeq(cbuf, 0, cbuf.length));
35 public final void write(char cbuf[], int off, int len)
38 impl.write(cbuf, off, len);
40 seq.write(new CharSeq(cbuf, off, len));
43 public final void write(String str) throws IOException {
45 impl.write(str, 0, str.length());
50 public final void write(String str, int off, int len)
53 impl.write(str, off, len);
55 seq.write(str.subSequence(off, off + len));
59 public final void flush() throws IOException {
66 public final void close() throws IOException {
76 public final Writer append(CharSequence csq) throws IOException {
78 String s = csq == null ? "null" : csq.toString();
79 impl.write(s, 0, s.length());
86 public final Writer append(CharSequence csq, int start, int end)
88 return append(csq.subSequence(start, end));
91 public final Writer append(char c) throws IOException {
96 public static Writer create(Impl impl) {
97 return new Writer(impl, null);
100 public static Writer create(ImplSeq impl) {
101 return new Writer(null, impl);
104 public static Writer create(final java.io.Writer w) {
105 return new Writer(null, new ImplSeq() {
106 public void write(CharSequence seq) throws IOException {
110 public void close() throws IOException {
114 public void flush() throws IOException {
120 public static Writer createBuffered(final Writer out) {
121 return create(new SimpleBuffer(out));
125 public static interface Impl {
126 public void close() throws IOException;
127 public void flush() throws IOException;
128 public void write(String str, int off, int len) throws IOException;
129 public void write(char[] arr, int off, int len) throws IOException;
131 public static interface ImplSeq {
132 public void close() throws IOException;
133 public void flush() throws IOException;
134 public void write(CharSequence seq) throws IOException;
137 // END: writer.final.2.0