samples/delegatingwriter/test/org/apidesign/delegatingwriter/CryptoWriter.java
changeset 63 cbba5b31d11c
child 133 50bf1b976c0d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/delegatingwriter/test/org/apidesign/delegatingwriter/CryptoWriter.java	Sat Jun 14 09:53:04 2008 +0200
     1.3 @@ -0,0 +1,92 @@
     1.4 +
     1.5 +package org.apidesign.delegatingwriter;
     1.6 +
     1.7 +import java.io.IOException;
     1.8 +import java.io.Writer;
     1.9 +
    1.10 +/** Writer alters each char from 'A' to 'Z' range with next one just like
    1.11 + * old Romans did.
    1.12 + *
    1.13 + * @author Jaroslav Tulach
    1.14 + */
    1.15 +public class CryptoWriter extends AltBufferedWriter {
    1.16 +    public CryptoWriter(Writer out) {
    1.17 +        super(out);
    1.18 +    }
    1.19 +    public CryptoWriter(Writer out, AltBufferedWriter.Behaviour behaviour) {
    1.20 +        super(out, behaviour);
    1.21 +    }
    1.22 +/* The above code is here to let us simulate different behaviours of the append
    1.23 + * method. In reality, the class would just subclass BufferedWriter, as shown bellow:
    1.24 +BEGIN: writer.CryptoWriter
    1.25 +public class CryptoWriter extends BufferedWriter {
    1.26 +    public CryptoWriter(Writer out) {
    1.27 +        super(out);
    1.28 +    }
    1.29 +    
    1.30 +    /* We need to override all known methods of BufferedWriter and do conversion
    1.31 +    * of the argument char, string or char array.
    1.32 +    */
    1.33 +    
    1.34 +    @Override
    1.35 +    public void write(char[] cbuf, int off, int len) throws IOException {
    1.36 +        char[] arr = new char[len];
    1.37 +        for (int i = 0; i < len; i++) {
    1.38 +            arr[i] = convertChar(cbuf[off + i]);
    1.39 +        }
    1.40 +        super.write(arr, 0, len);
    1.41 +    }
    1.42 +
    1.43 +    @Override
    1.44 +    public void write(int c) throws IOException {
    1.45 +        super.write(convertChar(c));
    1.46 +    }
    1.47 +
    1.48 +    @Override
    1.49 +    public void write(String str, int off, int len) throws IOException {
    1.50 +        StringBuffer sb = new StringBuffer();
    1.51 +        for (int i = 0; i < len; i++) {
    1.52 +            sb.append(convertChar(str.charAt(off + i)));
    1.53 +        }
    1.54 +        super.write(sb.toString(), 0, len);
    1.55 +    }
    1.56 +
    1.57 +    private char convertChar(int c) {
    1.58 +        if (c == 'Z') {
    1.59 +            return 'A';
    1.60 +        }
    1.61 +        if (c == 'z') {
    1.62 +            return 'a';
    1.63 +        }
    1.64 +        return (char)(c + 1);
    1.65 +    }
    1.66 +// END: writer.CryptoWriter
    1.67 +
    1.68 +    /* delegates to write(cbuf, 0, cbuf.length)
    1.69 +    public void write(char[] cbuf) throws IOException {
    1.70 +    }
    1.71 +    */
    1.72 +
    1.73 +    /* delegates to write(str, 0, str.length())
    1.74 +    public void write(String str) throws IOException {
    1.75 +    }
    1.76 +    */
    1.77 +
    1.78 +    
    1.79 +/* As this class was written against the version provided by JDK 1.4, we
    1.80 + * could not override the append methods, as they did not exist at that time.
    1.81 +     
    1.82 +    @Override
    1.83 +    public Writer append(CharSequence csq) throws IOException {
    1.84 +    }
    1.85 +
    1.86 +    @Override
    1.87 +    public Writer append(CharSequence csq, int start, int end) throws IOException {
    1.88 +    }
    1.89 +
    1.90 +    @Override
    1.91 +    public Writer append(char c) throws IOException {
    1.92 +    }
    1.93 +*/
    1.94 +    
    1.95 +}