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