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