samples/delegatingwriterfinal/src-api1.0/api/Writer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:08 +0200
changeset 153 b5cbb797ec0a
parent 133 50bf1b976c0d
child 154 0fd5e9c500b9
permissions -rw-r--r--
up to line 2000
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.1.0
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@153
    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@153
    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@153
    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@153
    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@67
    66
        return create(new SimpleBuffer(out));
jtulach@65
    67
    }
jtulach@65
    68
jtulach@65
    69
    
jtulach@65
    70
    public static interface Impl {
jtulach@65
    71
        public void close() throws IOException;
jtulach@65
    72
        public void flush() throws IOException;
jtulach@153
    73
        public void write(String str, int off, int len) throws IOException;
jtulach@153
    74
        public void write(char[] arr, int off, int len) throws IOException;
jtulach@65
    75
    }
jtulach@65
    76
}
jtulach@68
    77
// END: writer.final.1.0