samples/delegatingwriter/test/org/apidesign/delegatingwriter/CryptoWriter.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:53 +0200
changeset 210 acf2c31e22d4
parent 209 1c999569643b
child 232 e8d817c1bdc4
permissions -rw-r--r--
Merge: Geertjan's changes to the end of the chapter
     1 
     2 package org.apidesign.delegatingwriter;
     3 
     4 import java.io.IOException;
     5 import java.io.Writer;
     6 
     7 /** Writer alters each char from 'A' to 'Z' range with next one just like
     8  * old Romans did.
     9  *
    10  * @author Jaroslav Tulach
    11  */
    12 public class CryptoWriter extends AltBufferedWriter {
    13     public CryptoWriter(Writer out) {
    14         super(out);
    15     }
    16     public CryptoWriter(
    17         Writer out, AltBufferedWriter.Behaviour behaviour
    18     ) {
    19         super(out, behaviour);
    20     }
    21 /* The above code is here to let us simulate different behaviours of the append
    22  * method. In reality, the class would just subclass BufferedWriter, as shown bellow:
    23 BEGIN: writer.CryptoWriter
    24 public class CryptoWriter extends BufferedWriter {
    25     public CryptoWriter(Writer out) {
    26         super(out);
    27     }
    28     
    29     /* We need to override all known methods of BufferedWriter 
    30     * and do conversion of the argument char, string or char array.
    31     */
    32     
    33     @Override
    34     public void write(char[] buf, int off, int len) throws IOException {
    35         char[] arr = new char[len];
    36         for (int i = 0; i < len; i++) {
    37             arr[i] = encryptChar(buf[off + i]);
    38         }
    39         super.write(arr, 0, len);
    40     }
    41 
    42     @Override
    43     public void write(int c) throws IOException {
    44         super.write(encryptChar(c));
    45     }
    46 
    47     @Override
    48     public void write(String str, int off, int len) throws IOException {
    49         StringBuffer sb = new StringBuffer();
    50         for (int i = 0; i < len; i++) {
    51             sb.append(encryptChar(str.charAt(off + i)));
    52         }
    53         super.write(sb.toString(), 0, len);
    54     }
    55 
    56     private char encryptChar(int c) {
    57         if (c == 'Z') {
    58             return 'A';
    59         }
    60         if (c == 'z') {
    61             return 'a';
    62         }
    63         return (char)(c + 1);
    64     }
    65 // FINISH: writer.CryptoWriter
    66 
    67     /* delegates to write(cbuf, 0, cbuf.length)
    68     public void write(char[] cbuf) throws IOException {
    69     }
    70     */
    71 
    72     /* delegates to write(str, 0, str.length())
    73     public void write(String str) throws IOException {
    74     }
    75     */
    76 
    77     
    78 /* As this class was written against the version provided by JDK 1.4, we
    79  * could not override the append methods, as they did not exist at that time.
    80      
    81     @Override
    82     public Writer append(CharSequence csq) throws IOException {
    83     }
    84 
    85     @Override
    86     public Writer append(CharSequence csq, int start, int end) throws IOException {
    87     }
    88 
    89     @Override
    90     public Writer append(char c) throws IOException {
    91     }
    92 */
    93     
    94 }