As the purpose of buffer is to "buffer", let's modify our example to delegate to appendable methods directly only if the appendable is too big
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 public final class Writer {
11 private final Impl impl;
13 private Writer(Impl impl) {
16 public final void write(int c) throws IOException {
17 char[] arr = { (char)c };
18 impl.write(arr, 0, 1);
21 public final void write(char cbuf[]) throws IOException {
22 impl.write(cbuf, 0, cbuf.length);
24 public final void write(char cbuf[], int off, int len) throws IOException {
25 impl.write(cbuf, off, len);
27 public final void write(String str) throws IOException {
28 impl.write(str, 0, str.length());
30 public final void write(String str, int off, int len) throws IOException {
31 impl.write(str, off, len);
33 public final void flush() throws IOException {
36 public final void close() throws IOException {
40 public static Writer create(Impl impl) {
41 return new Writer(impl);
44 public static Writer create(final java.io.Writer w) {
45 return new Writer(new Impl() {
46 public void write(String str, int off, int len) throws IOException {
47 w.write(str, off, len);
50 public void write(char[] arr, int off, int len) throws IOException {
51 w.write(arr, off, len);
54 public void close() throws IOException {
58 public void flush() throws IOException {
64 public static Writer createBuffered(final Writer out) {
65 return create(new SimpleBuffer(out));
69 public static interface Impl {
70 public void close() throws IOException;
71 public void flush() throws IOException;
72 public void write(String str, int off, int len) throws IOException;
73 public void write(char[] arr, int off, int len) throws IOException;