jtulach@63: jtulach@63: package org.apidesign.delegatingwriter; jtulach@63: jtulach@63: import java.io.IOException; jtulach@63: import java.io.Writer; jtulach@63: jtulach@63: /** Writer alters each char from 'A' to 'Z' range with next one just like jtulach@63: * old Romans did. jtulach@63: * jtulach@63: * @author Jaroslav Tulach jtulach@63: */ jtulach@63: public class CryptoWriter extends AltBufferedWriter { jtulach@63: public CryptoWriter(Writer out) { jtulach@63: super(out); jtulach@63: } jtulach@154: public CryptoWriter( jtulach@154: Writer out, AltBufferedWriter.Behaviour behaviour jtulach@154: ) { jtulach@63: super(out, behaviour); jtulach@63: } jtulach@63: /* The above code is here to let us simulate different behaviours of the append jtulach@63: * method. In reality, the class would just subclass BufferedWriter, as shown bellow: jtulach@63: BEGIN: writer.CryptoWriter jtulach@63: public class CryptoWriter extends BufferedWriter { jtulach@63: public CryptoWriter(Writer out) { jtulach@63: super(out); jtulach@63: } jtulach@63: jtulach@154: /* We need to override all known methods of BufferedWriter jtulach@154: * and do conversion of the argument char, string or char array. jtulach@63: */ jtulach@63: jtulach@63: @Override jtulach@154: public void write(char[] buf, int off, int len) throws IOException { jtulach@63: char[] arr = new char[len]; jtulach@63: for (int i = 0; i < len; i++) { jtulach@210: arr[i] = encryptChar(buf[off + i]); jtulach@63: } jtulach@63: super.write(arr, 0, len); jtulach@63: } jtulach@63: jtulach@63: @Override jtulach@63: public void write(int c) throws IOException { jtulach@210: super.write(encryptChar(c)); jtulach@63: } jtulach@63: jtulach@63: @Override jtulach@63: public void write(String str, int off, int len) throws IOException { jtulach@63: StringBuffer sb = new StringBuffer(); jtulach@63: for (int i = 0; i < len; i++) { jtulach@210: sb.append(encryptChar(str.charAt(off + i))); jtulach@63: } jtulach@63: super.write(sb.toString(), 0, len); jtulach@63: } jtulach@63: jtulach@210: private char encryptChar(int c) { jtulach@63: if (c == 'Z') { jtulach@63: return 'A'; jtulach@63: } jtulach@63: if (c == 'z') { jtulach@63: return 'a'; jtulach@63: } jtulach@63: return (char)(c + 1); jtulach@63: } jtulach@181: // FINISH: writer.CryptoWriter jtulach@63: jtulach@63: /* delegates to write(cbuf, 0, cbuf.length) jtulach@63: public void write(char[] cbuf) throws IOException { jtulach@63: } jtulach@63: */ jtulach@63: jtulach@63: /* delegates to write(str, 0, str.length()) jtulach@63: public void write(String str) throws IOException { jtulach@63: } jtulach@63: */ jtulach@63: jtulach@63: jtulach@63: /* As this class was written against the version provided by JDK 1.4, we jtulach@63: * could not override the append methods, as they did not exist at that time. jtulach@63: jtulach@63: @Override jtulach@63: public Writer append(CharSequence csq) throws IOException { jtulach@63: } jtulach@63: jtulach@63: @Override jtulach@63: public Writer append(CharSequence csq, int start, int end) throws IOException { jtulach@63: } jtulach@63: jtulach@63: @Override jtulach@63: public Writer append(char c) throws IOException { jtulach@63: } jtulach@63: */ jtulach@63: jtulach@63: }