samples/delegatingwriter/test/org/apidesign/delegatingwriter/CryptoWriter.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:08 +0200
changeset 153 b5cbb797ec0a
parent 152 eb6f29070331
child 154 0fd5e9c500b9
permissions -rw-r--r--
up to line 2000
     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(Writer out, AltBufferedWriter.Behaviour behaviour) {
    17         super(out, behaviour);
    18     }
    19 /* The above code is here to let us simulate different behaviours of the append
    20  * method. In reality, the class would just subclass BufferedWriter, as shown bellow:
    21 BEGIN: writer.CryptoWriter
    22 public class CryptoWriter extends BufferedWriter {
    23     public CryptoWriter(Writer out) {
    24         super(out);
    25     }
    26     
    27     /* We need to override all known methods of BufferedWriter and do conversion
    28     * of the argument char, string or char array.
    29     */
    30     
    31     @Override
    32     public void write(char[] cbuf, int off, int len) throws IOException {
    33         char[] arr = new char[len];
    34         for (int i = 0; i < len; i++) {
    35             arr[i] = convertChar(cbuf[off + i]);
    36         }
    37         super.write(arr, 0, len);
    38     }
    39 
    40     @Override
    41     public void write(int c) throws IOException {
    42         super.write(convertChar(c));
    43     }
    44 
    45     @Override
    46     public void write(String str, int off, int len) throws IOException {
    47         StringBuffer sb = new StringBuffer();
    48         for (int i = 0; i < len; i++) {
    49             sb.append(convertChar(str.charAt(off + i)));
    50         }
    51         super.write(sb.toString(), 0, len);
    52     }
    53 
    54     private char convertChar(int c) {
    55         if (c == 'Z') {
    56             return 'A';
    57         }
    58         if (c == 'z') {
    59             return 'a';
    60         }
    61         return (char)(c + 1);
    62     }
    63 // END: writer.CryptoWriter
    64 
    65     /* delegates to write(cbuf, 0, cbuf.length)
    66     public void write(char[] cbuf) throws IOException {
    67     }
    68     */
    69 
    70     /* delegates to write(str, 0, str.length())
    71     public void write(String str) throws IOException {
    72     }
    73     */
    74 
    75     
    76 /* As this class was written against the version provided by JDK 1.4, we
    77  * could not override the append methods, as they did not exist at that time.
    78      
    79     @Override
    80     public Writer append(CharSequence csq) throws IOException {
    81     }
    82 
    83     @Override
    84     public Writer append(CharSequence csq, int start, int end) throws IOException {
    85     }
    86 
    87     @Override
    88     public Writer append(char c) throws IOException {
    89     }
    90 */
    91     
    92 }