jtulach@65
|
1 |
package api;
|
jtulach@65
|
2 |
|
jtulach@65
|
3 |
import java.io.IOException;
|
jtulach@65
|
4 |
|
jtulach@65
|
5 |
/** Fixing the problem caused by mixing subclassing and delegation in
|
jtulach@65
|
6 |
* the <code>java.io.BufferedWriter</code>
|
jtulach@65
|
7 |
*
|
jtulach@65
|
8 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@65
|
9 |
*/
|
jtulach@68
|
10 |
// BEGIN: writer.final.2.0
|
jtulach@67
|
11 |
public final class Writer implements Appendable {
|
jtulach@65
|
12 |
private final Impl impl;
|
jtulach@66
|
13 |
private final ImplSeq seq;
|
jtulach@65
|
14 |
|
jtulach@66
|
15 |
private Writer(Impl impl, ImplSeq seq) {
|
jtulach@65
|
16 |
this.impl = impl;
|
jtulach@66
|
17 |
this.seq = seq;
|
jtulach@65
|
18 |
}
|
jtulach@65
|
19 |
public final void write(int c) throws IOException {
|
jtulach@66
|
20 |
if (impl != null) {
|
jtulach@66
|
21 |
char[] arr = {(char) c};
|
jtulach@66
|
22 |
impl.write(arr, 0, 1);
|
jtulach@66
|
23 |
} else {
|
jtulach@66
|
24 |
seq.write(new CharSeq(c));
|
jtulach@66
|
25 |
}
|
jtulach@65
|
26 |
}
|
jtulach@65
|
27 |
|
jtulach@65
|
28 |
public final void write(char cbuf[]) throws IOException {
|
jtulach@66
|
29 |
if (impl != null) {
|
jtulach@66
|
30 |
impl.write(cbuf, 0, cbuf.length);
|
jtulach@66
|
31 |
} else {
|
jtulach@66
|
32 |
seq.write(new CharSeq(cbuf, 0, cbuf.length));
|
jtulach@66
|
33 |
}
|
jtulach@65
|
34 |
}
|
jtulach@154
|
35 |
public final void write(char cbuf[], int off, int len)
|
jtulach@154
|
36 |
throws IOException {
|
jtulach@66
|
37 |
if (impl != null) {
|
jtulach@66
|
38 |
impl.write(cbuf, off, len);
|
jtulach@66
|
39 |
} else {
|
jtulach@66
|
40 |
seq.write(new CharSeq(cbuf, off, len));
|
jtulach@66
|
41 |
}
|
jtulach@65
|
42 |
}
|
jtulach@65
|
43 |
public final void write(String str) throws IOException {
|
jtulach@66
|
44 |
if (impl != null) {
|
jtulach@66
|
45 |
impl.write(str, 0, str.length());
|
jtulach@66
|
46 |
} else {
|
jtulach@66
|
47 |
seq.write(str);
|
jtulach@66
|
48 |
}
|
jtulach@65
|
49 |
}
|
jtulach@154
|
50 |
public final void write(String str, int off, int len)
|
jtulach@154
|
51 |
throws IOException {
|
jtulach@66
|
52 |
if (impl != null) {
|
jtulach@66
|
53 |
impl.write(str, off, len);
|
jtulach@66
|
54 |
} else {
|
jtulach@66
|
55 |
seq.write(str.subSequence(off, off + len));
|
jtulach@66
|
56 |
}
|
jtulach@65
|
57 |
}
|
jtulach@65
|
58 |
|
jtulach@66
|
59 |
public final void flush() throws IOException {
|
jtulach@66
|
60 |
if (impl != null) {
|
jtulach@66
|
61 |
impl.flush();
|
jtulach@66
|
62 |
} else {
|
jtulach@66
|
63 |
seq.flush();
|
jtulach@66
|
64 |
}
|
jtulach@66
|
65 |
}
|
jtulach@66
|
66 |
public final void close() throws IOException {
|
jtulach@66
|
67 |
if (impl != null) {
|
jtulach@66
|
68 |
impl.close();
|
jtulach@66
|
69 |
} else {
|
jtulach@66
|
70 |
seq.flush();
|
jtulach@66
|
71 |
}
|
jtulach@66
|
72 |
}
|
jtulach@66
|
73 |
|
jtulach@67
|
74 |
|
jtulach@67
|
75 |
|
jtulach@67
|
76 |
public final Writer append(CharSequence csq) throws IOException {
|
jtulach@67
|
77 |
if (impl != null) {
|
jtulach@67
|
78 |
String s = csq == null ? "null" : csq.toString();
|
jtulach@67
|
79 |
impl.write(s, 0, s.length());
|
jtulach@67
|
80 |
} else {
|
jtulach@67
|
81 |
seq.write(csq);
|
jtulach@67
|
82 |
}
|
jtulach@67
|
83 |
return this;
|
jtulach@67
|
84 |
}
|
jtulach@67
|
85 |
|
jtulach@154
|
86 |
public final Writer append(CharSequence csq, int start, int end)
|
jtulach@154
|
87 |
throws IOException {
|
jtulach@67
|
88 |
return append(csq.subSequence(start, end));
|
jtulach@67
|
89 |
}
|
jtulach@67
|
90 |
|
jtulach@67
|
91 |
public final Writer append(char c) throws IOException {
|
jtulach@67
|
92 |
write(c);
|
jtulach@67
|
93 |
return this;
|
jtulach@67
|
94 |
}
|
jtulach@67
|
95 |
|
jtulach@66
|
96 |
public static Writer create(Impl impl) {
|
jtulach@66
|
97 |
return new Writer(impl, null);
|
jtulach@66
|
98 |
}
|
jtulach@66
|
99 |
|
jtulach@210
|
100 |
public static Writer create(ImplSeq seq) {
|
jtulach@210
|
101 |
return new Writer(null, seq);
|
jtulach@66
|
102 |
}
|
jtulach@66
|
103 |
|
jtulach@66
|
104 |
public static Writer create(final java.io.Writer w) {
|
jtulach@67
|
105 |
return new Writer(null, new ImplSeq() {
|
jtulach@67
|
106 |
public void write(CharSequence seq) throws IOException {
|
jtulach@67
|
107 |
w.append(seq);
|
jtulach@66
|
108 |
}
|
jtulach@66
|
109 |
|
jtulach@66
|
110 |
public void close() throws IOException {
|
jtulach@66
|
111 |
w.close();
|
jtulach@66
|
112 |
}
|
jtulach@66
|
113 |
|
jtulach@66
|
114 |
public void flush() throws IOException {
|
jtulach@66
|
115 |
w.flush();
|
jtulach@66
|
116 |
}
|
jtulach@67
|
117 |
});
|
jtulach@66
|
118 |
}
|
jtulach@66
|
119 |
|
jtulach@66
|
120 |
public static Writer createBuffered(final Writer out) {
|
jtulach@67
|
121 |
return create(new SimpleBuffer(out));
|
jtulach@66
|
122 |
}
|
jtulach@65
|
123 |
|
jtulach@65
|
124 |
|
jtulach@65
|
125 |
public static interface Impl {
|
jtulach@66
|
126 |
public void close() throws IOException;
|
jtulach@66
|
127 |
public void flush() throws IOException;
|
jtulach@66
|
128 |
public void write(String str, int off, int len) throws IOException;
|
jtulach@66
|
129 |
public void write(char[] arr, int off, int len) throws IOException;
|
jtulach@66
|
130 |
}
|
jtulach@66
|
131 |
public static interface ImplSeq {
|
jtulach@66
|
132 |
public void close() throws IOException;
|
jtulach@66
|
133 |
public void flush() throws IOException;
|
jtulach@65
|
134 |
public void write(CharSequence seq) throws IOException;
|
jtulach@65
|
135 |
}
|
jtulach@65
|
136 |
}
|
jtulach@68
|
137 |
// END: writer.final.2.0 |