A Charset implementation which performs Rot13 encoding. - Java Internationalization

Java examples for Internationalization:Charset

Introduction

Rot-13 encoding is a simple text obfuscation algorithm which shifts alphabetical characters by 13 so that 'a' becomes 'n', 'o' becomes 'b', etc.

Demo Code

 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;

class Rot13Charset extends Charset {
  private static final String BASE_CHARSET_NAME = "UTF-8";
  Charset baseCharset;/*w  ww .  j a v  a 2 s  .  com*/

  protected Rot13Charset(String canonical, String[] aliases) {
    super(canonical, aliases);
    baseCharset = Charset.forName(BASE_CHARSET_NAME);
  }
  public CharsetEncoder newEncoder() {
    return new Rot13Encoder(this, baseCharset.newEncoder());
  }
  public CharsetDecoder newDecoder() {
    return new Rot13Decoder(this, baseCharset.newDecoder());
  }
  public boolean contains(Charset cs) {
    return (false);
  }
  public static void rot13(CharBuffer cb) {
    for (int pos = cb.position(); pos < cb.limit(); pos++) {
      char c = cb.get(pos);
      char a = '\u0000';
      // Is it lowercase alpha?
      if ((c >= 'a') && (c <= 'z')) {
        a = 'a';
      }
      // Is it uppercase 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);
      }
    }
  }  
}

class Rot13Encoder extends CharsetEncoder {
  private CharsetEncoder baseEncoder;
  Rot13Encoder(Charset cs, CharsetEncoder baseEncoder) {
    super(cs, baseEncoder.averageBytesPerChar(), baseEncoder
        .maxBytesPerChar());
    this.baseEncoder = baseEncoder;
  }

  protected CoderResult encodeLoop(CharBuffer cb, ByteBuffer bb) {
    CharBuffer tmpcb = CharBuffer.allocate(cb.remaining());

    while (cb.hasRemaining()) {
      tmpcb.put(cb.get());
    }
    tmpcb.rewind();
    Rot13Charset.rot13(tmpcb);
    baseEncoder.reset();
    CoderResult cr = baseEncoder.encode(tmpcb, bb, true);
    cb.position(cb.position() - tmpcb.remaining());
    return (cr);
  }
}
class Rot13Decoder extends CharsetDecoder {
  private CharsetDecoder baseDecoder;
  Rot13Decoder(Charset cs, CharsetDecoder d) {
    super(cs, d.averageCharsPerByte(), d
        .maxCharsPerByte());
    baseDecoder = d;
  }
  protected CoderResult decodeLoop(ByteBuffer bb, CharBuffer cb) {
    baseDecoder.reset();
    CoderResult result = baseDecoder.decode(bb, cb, true);
    Rot13Charset.rot13(cb);
    return (result);
  }
}
public class Main{
  public static void main(String[] argv) throws Exception {
    BufferedReader in = new BufferedReader(new FileReader("main.java"));
    //in = new BufferedReader(new InputStreamReader(System.in));
    PrintStream out = new PrintStream(System.out, false, "X-ROT13");
    String s = null;
    while ((s = in.readLine()) != null) {
      out.println(s);
    }
    out.flush();
  }
}

Related Tutorials