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
     1 package api;
     2 
     3 import java.io.IOException;
     4 
     5 /** Fixing the problem caused by mixing subclassing and delegation in 
     6  * the <code>java.io.BufferedWriter</code>
     7  *
     8  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     9  */
    10 public final class Writer {
    11     private final Impl impl;
    12     
    13     private Writer(Impl impl) {
    14         this.impl = impl;
    15     }
    16     public final void write(int c) throws IOException {
    17         char[] arr = { (char)c };
    18         impl.write(arr, 0, 1);
    19     }
    20     
    21     public final void write(char cbuf[]) throws IOException {
    22 	impl.write(cbuf, 0, cbuf.length);
    23     }
    24     public final void write(char cbuf[], int off, int len) throws IOException {
    25         impl.write(cbuf, off, len);
    26     }
    27     public final void write(String str) throws IOException {
    28 	impl.write(str, 0, str.length());
    29     }
    30     public final void write(String str, int off, int len) throws IOException {
    31         impl.write(str, off, len);
    32     }
    33     public final void flush() throws IOException {
    34         impl.flush();
    35     }
    36     public final void close() throws IOException {
    37         impl.close();
    38     }
    39 
    40     public static Writer create(Impl impl) {
    41         return new Writer(impl);
    42     }
    43     
    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);
    48             }
    49 
    50             public void write(char[] arr, int off, int len) throws IOException {
    51                 w.write(arr, off, len);
    52             }
    53 
    54             public void close() throws IOException {
    55                 w.close();
    56             }
    57 
    58             public void flush() throws IOException {
    59                 w.flush();
    60             }
    61         });
    62     }
    63     
    64     public static Writer createBuffered(final Writer out) {
    65         return create(new SimpleBuffer(out));
    66     }
    67 
    68     
    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;
    74     }
    75     
    76     
    77     
    78 }