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