jtulach@65
|
1 |
package api;
|
jtulach@65
|
2 |
|
jtulach@65
|
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@65
|
13 |
|
jtulach@65
|
14 |
private Writer(Impl impl) {
|
jtulach@65
|
15 |
this.impl = impl;
|
jtulach@65
|
16 |
}
|
jtulach@65
|
17 |
public final void write(int c) throws IOException {
|
jtulach@65
|
18 |
char[] arr = { (char)c };
|
jtulach@65
|
19 |
impl.write(arr, 0, 1);
|
jtulach@65
|
20 |
}
|
jtulach@65
|
21 |
|
jtulach@65
|
22 |
public final void write(char cbuf[]) throws IOException {
|
jtulach@65
|
23 |
impl.write(cbuf, 0, cbuf.length);
|
jtulach@65
|
24 |
}
|
jtulach@65
|
25 |
public final void write(char cbuf[], int off, int len) throws IOException {
|
jtulach@65
|
26 |
impl.write(cbuf, off, len);
|
jtulach@65
|
27 |
}
|
jtulach@65
|
28 |
public final void write(String str) throws IOException {
|
jtulach@65
|
29 |
impl.write(str, 0, str.length());
|
jtulach@65
|
30 |
}
|
jtulach@65
|
31 |
public final void write(String str, int off, int len) throws IOException {
|
jtulach@65
|
32 |
impl.write(str, off, len);
|
jtulach@65
|
33 |
}
|
jtulach@65
|
34 |
public final void flush() throws IOException {
|
jtulach@65
|
35 |
impl.flush();
|
jtulach@65
|
36 |
}
|
jtulach@65
|
37 |
public final void close() throws IOException {
|
jtulach@65
|
38 |
impl.close();
|
jtulach@65
|
39 |
}
|
jtulach@65
|
40 |
|
jtulach@65
|
41 |
public static Writer create(Impl impl) {
|
jtulach@65
|
42 |
return new Writer(impl);
|
jtulach@65
|
43 |
}
|
jtulach@65
|
44 |
|
jtulach@65
|
45 |
public static Writer create(final java.io.Writer w) {
|
jtulach@65
|
46 |
return new Writer(new Impl() {
|
jtulach@65
|
47 |
public void write(String str, int off, int len) throws IOException {
|
jtulach@65
|
48 |
w.write(str, off, len);
|
jtulach@65
|
49 |
}
|
jtulach@65
|
50 |
|
jtulach@65
|
51 |
public void write(char[] arr, int off, int len) throws IOException {
|
jtulach@65
|
52 |
w.write(arr, off, len);
|
jtulach@65
|
53 |
}
|
jtulach@65
|
54 |
|
jtulach@65
|
55 |
public void close() throws IOException {
|
jtulach@65
|
56 |
w.close();
|
jtulach@65
|
57 |
}
|
jtulach@65
|
58 |
|
jtulach@65
|
59 |
public void flush() throws IOException {
|
jtulach@65
|
60 |
w.flush();
|
jtulach@65
|
61 |
}
|
jtulach@65
|
62 |
});
|
jtulach@65
|
63 |
}
|
jtulach@65
|
64 |
|
jtulach@65
|
65 |
public static Writer createBuffered(final Writer out) {
|
jtulach@65
|
66 |
class Delegate extends java.io.Writer {
|
jtulach@65
|
67 |
@Override
|
jtulach@65
|
68 |
public void write(char[] cbuf, int off, int len) throws IOException {
|
jtulach@65
|
69 |
out.write(cbuf, off, len);
|
jtulach@65
|
70 |
}
|
jtulach@65
|
71 |
|
jtulach@65
|
72 |
@Override
|
jtulach@65
|
73 |
public void flush() throws IOException {
|
jtulach@65
|
74 |
out.flush();
|
jtulach@65
|
75 |
}
|
jtulach@65
|
76 |
|
jtulach@65
|
77 |
@Override
|
jtulach@65
|
78 |
public void close() throws IOException {
|
jtulach@65
|
79 |
out.close();
|
jtulach@65
|
80 |
}
|
jtulach@65
|
81 |
|
jtulach@65
|
82 |
}
|
jtulach@65
|
83 |
return create(new BufferedWriter(new Delegate()));
|
jtulach@65
|
84 |
}
|
jtulach@65
|
85 |
|
jtulach@65
|
86 |
|
jtulach@65
|
87 |
public static interface Impl {
|
jtulach@65
|
88 |
public void close() throws IOException;
|
jtulach@65
|
89 |
public void flush() throws IOException;
|
jtulach@65
|
90 |
public void write(String str, int off, int len) throws IOException;
|
jtulach@65
|
91 |
public void write(char[] arr, int off, int len) throws IOException;
|
jtulach@65
|
92 |
}
|
jtulach@65
|
93 |
|
jtulach@65
|
94 |
|
jtulach@65
|
95 |
|
jtulach@65
|
96 |
}
|