Rot13Charsetpublic class Rot13Charset extends Charset A Charset implementation which performs Rot13 encoding. Rot-13 encoding
is a simple text obfuscation algorithm which shifts alphabetical characters
by 13 so that 'a' becomes 'n', 'o' becomes 'b', etc. This algorithm
was popularized by the Usenet discussion forums many years ago to mask
naughty words, hide answers to questions, and so on. The Rot13 algorithm
is symmetrical, applying it to text that has been scrambled by Rot13 will
give you the original unscrambled text.
Applying this Charset encoding to an output stream will cause everything
you write to that stream to be Rot13 scrambled as it's written out. And
appying it to an input stream causes data read to be Rot13 descrambled
as it's read. |
Fields Summary |
---|
private static final String | BASE_CHARSET_NAME | Charset | baseCharset |
Constructors Summary |
---|
protected Rot13Charset(String canonical, String[] aliases)Constructor for the Rot13 charset. Call the superclass
constructor to pass along the name(s) we'll be known by.
Then save a reference to the delegate Charset.
super (canonical, aliases);
// Save the base charset we're delegating to.
baseCharset = Charset.forName (BASE_CHARSET_NAME);
|
Methods Summary |
---|
public boolean | contains(java.nio.charset.Charset cs)This method must be implemented by concrete Charsets. We always
say no, which is safe.
return (false);
| public static void | main(java.lang.String[] argv)Unit test for the Rot13 Charset. This main() will open and read
an input file if named on the command line, or stdin if no args
are provided, and write the contents to stdout via the X-ROT13
charset encoding.
The "encryption" implemented by the Rot13 algorithm is symmetrical.
Feeding in a plain-text file, such as Java source code for example,
will output a scrambled version. Feeding the scrambled version
back in will yield the original plain-text document.
BufferedReader in;
if (argv.length > 0) {
// open the named file
in = new BufferedReader (new FileReader (argv [0]));
} else {
// wrap a BufferedReader around stdin
in = new BufferedReader (new InputStreamReader (System.in));
}
// Create a PrintStream which uses the Rot13 encoding
PrintStream out = new PrintStream (System.out, false, "X-ROT13");
String s = null;
// Read all input and write it to the output
// As the data passes through the PrintStream
// it will be Rot13 encoded.
while ((s = in.readLine()) != null) {
out.println (s);
}
out.flush();
| public java.nio.charset.CharsetDecoder | newDecoder()Called by users of this Charset to obtain a decoder.
This implementation instantiates an instance of a private class
(defined below) and passes it a decoder from the base Charset.
return new Rot13Decoder (this, baseCharset.newDecoder());
| public java.nio.charset.CharsetEncoder | newEncoder()Called by users of this Charset to obtain an encoder.
This implementation instantiates an instance of a private class
(defined below) and passes it an encoder from the base Charset.
return new Rot13Encoder (this, baseCharset.newEncoder());
| private void | rot13(java.nio.CharBuffer cb)Common routine to rotate all the ASCII alpha chars in the given
CharBuffer by 13. Note that this code explicitly compares for
upper and lower case ASCII chars rather than using the methods
Character.isLowerCase and Character.isUpperCase. This is because
the rotate-by-13 scheme only works properly for the alphabetic
characters of the ASCII charset and those methods can return
true for non-ASCII Unicode chars.
for (int pos = cb.position(); pos < cb.limit(); pos++) {
char c = cb.get (pos);
char a = '\u0000";
// Is it lower case alpha?
if ((c >= 'a") && (c <= 'z")) {
a = 'a";
}
// Is it upper case alpha?
if ((c >= 'A") && (c <= 'Z")) {
a = 'A";
}
// If either, roll it by 13
if (a != '\u0000") {
c = (char)((((c - a) + 13) % 26) + a);
cb.put (pos, c);
}
}
|
|