author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:53:07 +0200 | |
changeset 67 | b029a28df444 |
permissions | -rw-r--r-- |
jtulach@67 | 1 |
|
jtulach@67 | 2 |
package api.usage; |
jtulach@67 | 3 |
|
jtulach@67 | 4 |
import api.Writer; |
jtulach@67 | 5 |
import java.io.IOException; |
jtulach@67 | 6 |
|
jtulach@67 | 7 |
|
jtulach@67 | 8 |
/** Writer alters each char from 'A' to 'Z' range with next one just like |
jtulach@67 | 9 |
* old Romans did. |
jtulach@67 | 10 |
* |
jtulach@67 | 11 |
* @author Jaroslav Tulach |
jtulach@67 | 12 |
*/ |
jtulach@67 | 13 |
public class CryptoWriter implements Writer.Impl { |
jtulach@67 | 14 |
private Writer out; |
jtulach@67 | 15 |
|
jtulach@67 | 16 |
private CryptoWriter(Writer out) { |
jtulach@67 | 17 |
this.out = out; |
jtulach@67 | 18 |
} |
jtulach@67 | 19 |
|
jtulach@67 | 20 |
|
jtulach@67 | 21 |
public static Writer create(Writer out) { |
jtulach@67 | 22 |
return Writer.create(new CryptoWriter(out)); |
jtulach@67 | 23 |
} |
jtulach@67 | 24 |
|
jtulach@67 | 25 |
@Override |
jtulach@67 | 26 |
public void write(char[] cbuf, int off, int len) throws IOException { |
jtulach@67 | 27 |
char[] arr = new char[len]; |
jtulach@67 | 28 |
for (int i = 0; i < len; i++) { |
jtulach@67 | 29 |
arr[i] = convertChar(cbuf[off + i]); |
jtulach@67 | 30 |
} |
jtulach@67 | 31 |
out.write(arr, 0, len); |
jtulach@67 | 32 |
} |
jtulach@67 | 33 |
|
jtulach@67 | 34 |
@Override |
jtulach@67 | 35 |
public void write(String str, int off, int len) throws IOException { |
jtulach@67 | 36 |
StringBuffer sb = new StringBuffer(); |
jtulach@67 | 37 |
for (int i = 0; i < len; i++) { |
jtulach@67 | 38 |
sb.append(convertChar(str.charAt(off + i))); |
jtulach@67 | 39 |
} |
jtulach@67 | 40 |
out.write(sb.toString(), 0, len); |
jtulach@67 | 41 |
} |
jtulach@67 | 42 |
|
jtulach@67 | 43 |
private char convertChar(int c) { |
jtulach@67 | 44 |
if (c == 'Z') { |
jtulach@67 | 45 |
return 'A'; |
jtulach@67 | 46 |
} |
jtulach@67 | 47 |
if (c == 'z') { |
jtulach@67 | 48 |
return 'a'; |
jtulach@67 | 49 |
} |
jtulach@67 | 50 |
return (char)(c + 1); |
jtulach@67 | 51 |
} |
jtulach@67 | 52 |
|
jtulach@67 | 53 |
public void close() throws IOException { |
jtulach@67 | 54 |
out.close(); |
jtulach@67 | 55 |
} |
jtulach@67 | 56 |
|
jtulach@67 | 57 |
public void flush() throws IOException { |
jtulach@67 | 58 |
out.flush(); |
jtulach@67 | 59 |
} |
jtulach@67 | 60 |
} |