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
     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 // BEGIN: writer.final.1.0
    11 public final class Writer {
    12     private final Impl impl;
    13     
    14     private Writer(Impl impl) {
    15         this.impl = impl;
    16     }
    17     public final void write(int c) throws IOException {
    18         char[] arr = { (char)c };
    19         impl.write(arr, 0, 1);
    20     }
    21     
    22     public final void write(char cbuf[]) throws IOException {
    23 	impl.write(cbuf, 0, cbuf.length);
    24     }
    25     public final void write(char cbuf[], int off, int len) 
    26     throws IOException {
    27         impl.write(cbuf, off, len);
    28     }
    29     public final void write(String str) throws IOException {
    30 	impl.write(str, 0, str.length());
    31     }
    32     public final void write(String str, int off, int len) 
    33     throws IOException {
    34         impl.write(str, off, len);
    35     }
    36     public final void flush() throws IOException {
    37         impl.flush();
    38     }
    39     public final void close() throws IOException {
    40         impl.close();
    41     }
    42 
    43     public static Writer create(Impl impl) {
    44         return new Writer(impl);
    45     }
    46     
    47     public static Writer create(final java.io.Writer w) {
    48         return new Writer(new Impl() {
    49             public void write(String str, int off, int len) 
    50             throws IOException {
    51                 w.write(str, off, len);
    52             }
    53 
    54             public void write(char[] arr, int off, int len) 
    55             throws IOException {
    56                 w.write(arr, off, len);
    57             }
    58 
    59             public void close() throws IOException {
    60                 w.close();
    61             }
    62 
    63             public void flush() throws IOException {
    64                 w.flush();
    65             }
    66         });
    67     }
    68     
    69     public static Writer createBuffered(final Writer out) {
    70         return create(new SimpleBuffer(out));
    71     }
    72 
    73     
    74     public static interface Impl {
    75         public void close() throws IOException;
    76         public void flush() throws IOException;
    77         public void write(String s, int off, int len) throws IOException;
    78         public void write(char[] a, int off, int len) throws IOException;
    79     }
    80 }
    81 // END: writer.final.1.0