2 package org.apidesign.delegatingwriter;
4 import java.io.IOException;
7 /** Writer alters each char from 'A' to 'Z' range with next one just like
10 * @author Jaroslav Tulach
12 public class CryptoWriter extends AltBufferedWriter {
13 public CryptoWriter(Writer out) {
16 public CryptoWriter(Writer out, AltBufferedWriter.Behaviour behaviour) {
17 super(out, behaviour);
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) {
27 /* We need to override all known methods of BufferedWriter and do conversion
28 * of the argument char, string or char array.
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]);
37 super.write(arr, 0, len);
41 public void write(int c) throws IOException {
42 super.write(convertChar(c));
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)));
51 super.write(sb.toString(), 0, len);
54 private char convertChar(int c) {
63 // END: writer.CryptoWriter
65 /* delegates to write(cbuf, 0, cbuf.length)
66 public void write(char[] cbuf) throws IOException {
70 /* delegates to write(str, 0, str.length())
71 public void write(String str) throws IOException {
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.
80 public Writer append(CharSequence csq) throws IOException {
84 public Writer append(CharSequence csq, int start, int end) throws IOException {
88 public Writer append(char c) throws IOException {