Merge: Scanner does not really need to override any visitXYZ methods that do default traversal
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) {
17 Writer out, AltBufferedWriter.Behaviour behaviour
19 super(out, behaviour);
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) {
29 /* We need to override all known methods of BufferedWriter
30 * and do conversion of the argument char, string or char array.
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]);
39 super.write(arr, 0, len);
43 public void write(int c) throws IOException {
44 super.write(encryptChar(c));
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)));
53 super.write(sb.toString(), 0, len);
56 private char encryptChar(int c) {
65 // FINISH: writer.CryptoWriter
67 /* delegates to write(cbuf, 0, cbuf.length)
68 public void write(char[] cbuf) throws IOException {
72 /* delegates to write(str, 0, str.length())
73 public void write(String str) throws IOException {
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.
82 public Writer append(CharSequence csq) throws IOException {
86 public Writer append(CharSequence csq, int start, int end) throws IOException {
90 public Writer append(char c) throws IOException {