1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/delegatingwriterfinal/src-test/api/usage/CryptoWriter.java Sat Jun 14 09:53:06 2008 +0200
1.3 @@ -0,0 +1,60 @@
1.4 +
1.5 +package api.usage;
1.6 +
1.7 +import api.Writer;
1.8 +import java.io.IOException;
1.9 +
1.10 +
1.11 +/** Writer alters each char from 'A' to 'Z' range with next one just like
1.12 + * old Romans did.
1.13 + *
1.14 + * @author Jaroslav Tulach
1.15 + */
1.16 +public class CryptoWriter implements Writer.Impl {
1.17 + private Writer out;
1.18 +
1.19 + private CryptoWriter(Writer out) {
1.20 + this.out = out;
1.21 + }
1.22 +
1.23 +
1.24 + public static Writer create(Writer out) {
1.25 + return Writer.create(new CryptoWriter(out));
1.26 + }
1.27 +
1.28 + @Override
1.29 + public void write(char[] cbuf, int off, int len) throws IOException {
1.30 + char[] arr = new char[len];
1.31 + for (int i = 0; i < len; i++) {
1.32 + arr[i] = convertChar(cbuf[off + i]);
1.33 + }
1.34 + out.write(arr, 0, len);
1.35 + }
1.36 +
1.37 + @Override
1.38 + public void write(String str, int off, int len) throws IOException {
1.39 + StringBuffer sb = new StringBuffer();
1.40 + for (int i = 0; i < len; i++) {
1.41 + sb.append(convertChar(str.charAt(off + i)));
1.42 + }
1.43 + out.write(sb.toString(), 0, len);
1.44 + }
1.45 +
1.46 + private char convertChar(int c) {
1.47 + if (c == 'Z') {
1.48 + return 'A';
1.49 + }
1.50 + if (c == 'z') {
1.51 + return 'a';
1.52 + }
1.53 + return (char)(c + 1);
1.54 + }
1.55 +
1.56 + public void close() throws IOException {
1.57 + out.close();
1.58 + }
1.59 +
1.60 + public void flush() throws IOException {
1.61 + out.flush();
1.62 + }
1.63 +}