01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.util.transformers;
11:
12: import java.io.Reader;
13: import java.io.Writer;
14:
15: import org.mmbase.util.logging.*;
16:
17: /**
18: * Rot5 implementation. Digits 0-4 are shifted 5 positions forward, digits 5-9 are shifted 5
19: * backwards and other characters are untouched, which results in scrambled - but easily decoded -
20: * strings. You would want this to combine with {@link Rot13} for the letters.
21: *
22: * @author Michiel Meeuwissen
23: * @since MMBase-1.8
24: */
25:
26: public class Rot5 extends ReaderTransformer implements CharTransformer {
27:
28: private static final Logger log = Logging
29: .getLoggerInstance(Rot5.class);
30:
31: protected Writer rot5(Reader r, Writer w) {
32: try {
33: int c = r.read();
34: while (c != -1) {
35: if (c >= '0' && c <= '4') {
36: c += 5;
37: } else if (c >= '5' && c <= '9') {
38: c -= 5;
39: }
40: w.write(c);
41: c = r.read();
42: }
43: } catch (java.io.IOException ioe) {
44: log.error(ioe);
45: }
46: return w;
47: }
48:
49: public Writer transform(Reader r, Writer w) {
50: return rot5(r, w);
51: }
52:
53: /**
54: * For Rot13, transformBack does the same as {@link #transform}
55: **/
56: public Writer transformBack(Reader r, Writer w) {
57: return rot5(r, w);
58: }
59:
60: public String toString() {
61: return "ROT-5";
62: }
63: }
|