samples/delegatingwriterfinal/src-api1.0/api/Writer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:07 +0200
changeset 67 b029a28df444
parent 65 4db7ceebd2b3
child 68 a2de5429d581
permissions -rw-r--r--
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
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@65
    10
public final class Writer {
jtulach@65
    11
    private final Impl impl;
jtulach@65
    12
    
jtulach@65
    13
    private Writer(Impl impl) {
jtulach@65
    14
        this.impl = impl;
jtulach@65
    15
    }
jtulach@65
    16
    public final void write(int c) throws IOException {
jtulach@65
    17
        char[] arr = { (char)c };
jtulach@65
    18
        impl.write(arr, 0, 1);
jtulach@65
    19
    }
jtulach@65
    20
    
jtulach@65
    21
    public final void write(char cbuf[]) throws IOException {
jtulach@65
    22
	impl.write(cbuf, 0, cbuf.length);
jtulach@65
    23
    }
jtulach@65
    24
    public final void write(char cbuf[], int off, int len) throws IOException {
jtulach@65
    25
        impl.write(cbuf, off, len);
jtulach@65
    26
    }
jtulach@65
    27
    public final void write(String str) throws IOException {
jtulach@65
    28
	impl.write(str, 0, str.length());
jtulach@65
    29
    }
jtulach@65
    30
    public final void write(String str, int off, int len) throws IOException {
jtulach@65
    31
        impl.write(str, off, len);
jtulach@65
    32
    }
jtulach@65
    33
    public final void flush() throws IOException {
jtulach@65
    34
        impl.flush();
jtulach@65
    35
    }
jtulach@65
    36
    public final void close() throws IOException {
jtulach@65
    37
        impl.close();
jtulach@65
    38
    }
jtulach@65
    39
jtulach@65
    40
    public static Writer create(Impl impl) {
jtulach@65
    41
        return new Writer(impl);
jtulach@65
    42
    }
jtulach@65
    43
    
jtulach@65
    44
    public static Writer create(final java.io.Writer w) {
jtulach@65
    45
        return new Writer(new Impl() {
jtulach@65
    46
            public void write(String str, int off, int len) throws IOException {
jtulach@65
    47
                w.write(str, off, len);
jtulach@65
    48
            }
jtulach@65
    49
jtulach@65
    50
            public void write(char[] arr, int off, int len) throws IOException {
jtulach@65
    51
                w.write(arr, off, len);
jtulach@65
    52
            }
jtulach@65
    53
jtulach@65
    54
            public void close() throws IOException {
jtulach@65
    55
                w.close();
jtulach@65
    56
            }
jtulach@65
    57
jtulach@65
    58
            public void flush() throws IOException {
jtulach@65
    59
                w.flush();
jtulach@65
    60
            }
jtulach@65
    61
        });
jtulach@65
    62
    }
jtulach@65
    63
    
jtulach@65
    64
    public static Writer createBuffered(final Writer out) {
jtulach@67
    65
        return create(new SimpleBuffer(out));
jtulach@65
    66
    }
jtulach@65
    67
jtulach@65
    68
    
jtulach@65
    69
    public static interface Impl {
jtulach@65
    70
        public void close() throws IOException;
jtulach@65
    71
        public void flush() throws IOException;
jtulach@65
    72
        public void write(String str, int off, int len) throws IOException;
jtulach@65
    73
        public void write(char[] arr, int off, int len) throws IOException;
jtulach@65
    74
    }
jtulach@65
    75
    
jtulach@65
    76
    
jtulach@65
    77
    
jtulach@65
    78
}