import cs1705.*; import java.io.*; // ------------------------------------------------------------------------- /** * A Rot-13 decryption class. * This in-class demo is based on the Lab 06 assignment. * * @author Stephen Edwards * @version 2003.10.07 */ public class RotDecryptor { //~ Instance/static variables ............................................. //~ Constructor ........................................................... //~ Methods ............................................................... // ---------------------------------------------------------- /** * Compute the Rot-13 version of the character (or return the character * unchanged if it is not a letter). * * @ return the Rot-13 encoding of the character */ public int rotateCharacter( int chr ) { if ( Character.isLowerCase( (char)chr ) ) { return ( chr - 'a' + 13 ) % 26 + 'a'; } else if ( Character.isUpperCase( (char)chr ) ) { return ( chr - 'A' + 13 ) % 26 + 'A'; } else { return chr; } } // ---------------------------------------------------------- /** * Read all input from the provided input stream, and write the * equivalent Rot-13 translation on the given output stream. * * @param in the input stream to read from * @param out the output stream to write to */ public void decrypt( BufferedReader in, PrintWriter out ) throws Exception { int chr = in.read(); while ( chr != -1 ) { out.write( rotateCharacter( chr ) ); chr = in.read(); } } // ---------------------------------------------------------- /** * Read all input from the provided input file, and write the * equivalent Rot-13 translation on the given output file. * * @param inFile the name of the file to read from * @param outFile the name of the file to write to */ public void decryptFile( String inFile, String outFile ) { try { // First, set up the streams BufferedReader in = IOHelper.createBufferedReader( inFile ); PrintWriter out = IOHelper.createPrintWriter( outFile ); // Now, call the real operation to do the work decrypt( in, out ); // Now clean up in.close(); out.close(); } catch ( Exception e ) { e.printStackTrace(); } } }