samples/delegatingwriterfinal/src-api1.0/api/Writer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:11 +0200
changeset 154 0fd5e9c500b9
parent 153 b5cbb797ec0a
permissions -rw-r--r--
Merge: Geertjan's changs up to 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@154
    25
    public final void write(char cbuf[], int off, int len) 
jtulach@154
    26
    throws IOException {
jtulach@65
    27
        impl.write(cbuf, off, len);
jtulach@65
    28
    }
jtulach@65
    29
    public final void write(String str) throws IOException {
jtulach@65
    30
	impl.write(str, 0, str.length());
jtulach@65
    31
    }
jtulach@154
    32
    public final void write(String str, int off, int len) 
jtulach@154
    33
    throws IOException {
jtulach@65
    34
        impl.write(str, off, len);
jtulach@65
    35
    }
jtulach@65
    36
    public final void flush() throws IOException {
jtulach@65
    37
        impl.flush();
jtulach@65
    38
    }
jtulach@65
    39
    public final void close() throws IOException {
jtulach@65
    40
        impl.close();
jtulach@65
    41
    }
jtulach@65
    42
jtulach@65
    43
    public static Writer create(Impl impl) {
jtulach@65
    44
        return new Writer(impl);
jtulach@65
    45
    }
jtulach@65
    46
    
jtulach@65
    47
    public static Writer create(final java.io.Writer w) {
jtulach@65
    48
        return new Writer(new Impl() {
jtulach@154
    49
            public void write(String str, int off, int len) 
jtulach@154
    50
            throws IOException {
jtulach@65
    51
                w.write(str, off, len);
jtulach@65
    52
            }
jtulach@65
    53
jtulach@154
    54
            public void write(char[] arr, int off, int len) 
jtulach@154
    55
            throws IOException {
jtulach@65
    56
                w.write(arr, off, len);
jtulach@65
    57
            }
jtulach@65
    58
jtulach@65
    59
            public void close() throws IOException {
jtulach@65
    60
                w.close();
jtulach@65
    61
            }
jtulach@65
    62
jtulach@65
    63
            public void flush() throws IOException {
jtulach@65
    64
                w.flush();
jtulach@65
    65
            }
jtulach@65
    66
        });
jtulach@65
    67
    }
jtulach@65
    68
    
jtulach@65
    69
    public static Writer createBuffered(final Writer out) {
jtulach@67
    70
        return create(new SimpleBuffer(out));
jtulach@65
    71
    }
jtulach@65
    72
jtulach@65
    73
    
jtulach@65
    74
    public static interface Impl {
jtulach@65
    75
        public void close() throws IOException;
jtulach@65
    76
        public void flush() throws IOException;
jtulach@154
    77
        public void write(String s, int off, int len) throws IOException;
jtulach@154
    78
        public void write(char[] a, int off, int len) throws IOException;
jtulach@65
    79
    }
jtulach@65
    80
}
jtulach@68
    81
// END: writer.final.1.0