jtulach@65
|
1 |
package api;
|
jtulach@65
|
2 |
|
jtulach@66
|
3 |
import java.io.BufferedWriter;
|
jtulach@65
|
4 |
import java.io.IOException;
|
jtulach@65
|
5 |
|
jtulach@65
|
6 |
/** Fixing the problem caused by mixing subclassing and delegation in
|
jtulach@65
|
7 |
* the <code>java.io.BufferedWriter</code>
|
jtulach@65
|
8 |
*
|
jtulach@65
|
9 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@65
|
10 |
*/
|
jtulach@65
|
11 |
public final class Writer {
|
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@65
|
35 |
public final void write(char cbuf[], int off, int len) throws IOException {
|
jtulach@66
|
36 |
if (impl != null) {
|
jtulach@66
|
37 |
impl.write(cbuf, off, len);
|
jtulach@66
|
38 |
} else {
|
jtulach@66
|
39 |
seq.write(new CharSeq(cbuf, off, len));
|
jtulach@66
|
40 |
}
|
jtulach@65
|
41 |
}
|
jtulach@65
|
42 |
public final void write(String str) throws IOException {
|
jtulach@66
|
43 |
if (impl != null) {
|
jtulach@66
|
44 |
impl.write(str, 0, str.length());
|
jtulach@66
|
45 |
} else {
|
jtulach@66
|
46 |
seq.write(str);
|
jtulach@66
|
47 |
}
|
jtulach@65
|
48 |
}
|
jtulach@65
|
49 |
public final void write(String str, int off, int len) throws IOException {
|
jtulach@66
|
50 |
if (impl != null) {
|
jtulach@66
|
51 |
impl.write(str, off, len);
|
jtulach@66
|
52 |
} else {
|
jtulach@66
|
53 |
seq.write(str.subSequence(off, off + len));
|
jtulach@66
|
54 |
}
|
jtulach@65
|
55 |
}
|
jtulach@65
|
56 |
|
jtulach@66
|
57 |
public final void flush() throws IOException {
|
jtulach@66
|
58 |
if (impl != null) {
|
jtulach@66
|
59 |
impl.flush();
|
jtulach@66
|
60 |
} else {
|
jtulach@66
|
61 |
seq.flush();
|
jtulach@66
|
62 |
}
|
jtulach@66
|
63 |
}
|
jtulach@66
|
64 |
public final void close() throws IOException {
|
jtulach@66
|
65 |
if (impl != null) {
|
jtulach@66
|
66 |
impl.close();
|
jtulach@66
|
67 |
} else {
|
jtulach@66
|
68 |
seq.flush();
|
jtulach@66
|
69 |
}
|
jtulach@66
|
70 |
}
|
jtulach@66
|
71 |
|
jtulach@66
|
72 |
public static Writer create(Impl impl) {
|
jtulach@66
|
73 |
return new Writer(impl, null);
|
jtulach@66
|
74 |
}
|
jtulach@66
|
75 |
|
jtulach@66
|
76 |
public static Writer create(ImplSeq impl) {
|
jtulach@66
|
77 |
return new Writer(null, impl);
|
jtulach@66
|
78 |
}
|
jtulach@66
|
79 |
|
jtulach@66
|
80 |
public static Writer create(final java.io.Writer w) {
|
jtulach@66
|
81 |
return new Writer(new Impl() {
|
jtulach@66
|
82 |
public void write(String str, int off, int len) throws IOException {
|
jtulach@66
|
83 |
w.write(str, off, len);
|
jtulach@66
|
84 |
}
|
jtulach@66
|
85 |
|
jtulach@66
|
86 |
public void write(char[] arr, int off, int len) throws IOException {
|
jtulach@66
|
87 |
w.write(arr, off, len);
|
jtulach@66
|
88 |
}
|
jtulach@66
|
89 |
|
jtulach@66
|
90 |
public void close() throws IOException {
|
jtulach@66
|
91 |
w.close();
|
jtulach@66
|
92 |
}
|
jtulach@66
|
93 |
|
jtulach@66
|
94 |
public void flush() throws IOException {
|
jtulach@66
|
95 |
w.flush();
|
jtulach@66
|
96 |
}
|
jtulach@66
|
97 |
}, null);
|
jtulach@66
|
98 |
}
|
jtulach@66
|
99 |
|
jtulach@66
|
100 |
public static Writer createBuffered(final Writer out) {
|
jtulach@66
|
101 |
class Delegate extends java.io.Writer {
|
jtulach@66
|
102 |
@Override
|
jtulach@66
|
103 |
public void write(char[] cbuf, int off, int len) throws IOException {
|
jtulach@66
|
104 |
out.write(cbuf, off, len);
|
jtulach@66
|
105 |
}
|
jtulach@66
|
106 |
|
jtulach@66
|
107 |
@Override
|
jtulach@66
|
108 |
public void flush() throws IOException {
|
jtulach@66
|
109 |
out.flush();
|
jtulach@66
|
110 |
}
|
jtulach@66
|
111 |
|
jtulach@66
|
112 |
@Override
|
jtulach@66
|
113 |
public void close() throws IOException {
|
jtulach@66
|
114 |
out.close();
|
jtulach@66
|
115 |
}
|
jtulach@66
|
116 |
|
jtulach@66
|
117 |
}
|
jtulach@66
|
118 |
return create(new BufferedWriter(new Delegate()));
|
jtulach@66
|
119 |
}
|
jtulach@65
|
120 |
|
jtulach@65
|
121 |
|
jtulach@65
|
122 |
public static interface Impl {
|
jtulach@66
|
123 |
public void close() throws IOException;
|
jtulach@66
|
124 |
public void flush() throws IOException;
|
jtulach@66
|
125 |
public void write(String str, int off, int len) throws IOException;
|
jtulach@66
|
126 |
public void write(char[] arr, int off, int len) throws IOException;
|
jtulach@66
|
127 |
}
|
jtulach@66
|
128 |
public static interface ImplSeq {
|
jtulach@66
|
129 |
public void close() throws IOException;
|
jtulach@66
|
130 |
public void flush() throws IOException;
|
jtulach@65
|
131 |
public void write(CharSequence seq) throws IOException;
|
jtulach@65
|
132 |
}
|
jtulach@65
|
133 |
|
jtulach@65
|
134 |
|
jtulach@65
|
135 |
|
jtulach@65
|
136 |
}
|