Monday, February 13, 2012

Console Class

The Console class is a new feature of Java 6.0. It provides an alternative to the standard streams currently being used.



The Console is commonly used as a support in securing password entries. It contains methods that can suppress the characters being displayed on the user's screen, and remove them from memory when they are no longer needed.
To use the Console, you must retrieve the Console object using the System.console method. If the Console object is available, the Console object is returned. Otherwise, it returns null, and use of the Console is not permitted. This happens when the Console is not supported by the underlying OS or the application is launched in an environment that is not interactive.
Consider this sample code that uses the Console class to verify a password.

The first statement executed within the static main method attempts to retrieve an instance of the Console object using System.console().
import java.util.Arrays ;
import java.io.* ;
public class SampleConsole {
    
    public static void main (String args[]) throws IOException {

        Console c = System.console() ;
        if (c == null) {
            System.err.println("No console is available.") ;
            System.exit(1) ;
        }

        String login = c.readLine("Please enter your login information: ") ;
        char [] oldPassword = c.readPassword("Enter your old password: ") ;
      boolean test ;
        
            do {
                char [] newPassword1 =
                    c.readPassword("Input your new password: ") ;
                char [] newPassword2 =
                    c.readPassword("Input the new password again: ") ;
                test = ! Arrays.equals(newPassword1, newPassword2) ;
                if (test) {
                    c.format("Your passwords do not match. Try again.%n") ;
                } else {
                    c.format("The password for %s changed.%n", login) ;
                }
                
            } while (test) ;        
  }  
}
If the Console object is not available, the application is aborted using the System.exit(1) method.

If a Console object is available, the readLine method is invoked to prompt for and read the user's login name.
The readPassword method is invoked to prompt and read the password. Values typed are not echoed on screen. This provides a secure entry of values being typed for a password.
import java.util.Arrays ;
import java.io.* ;
public class SampleConsole {
    
    public static void main (String args[]) throws IOException {

        Console c = System.console() ;
        if (c == null) {
            System.err.println("No console is available.") ;
            System.exit(1) ;
        }

        String login = c.readLine("Please enter your login information: ") ;
        char [] oldPassword = c.readPassword("Enter your old password: ") ;
      boolean test ;
        
            do {
                char [] newPassword1 =
                    c.readPassword("Input your new password: ") ;
                char [] newPassword2 =
                    c.readPassword("Input the new password again: ") ;
                test = ! Arrays.equals(newPassword1, newPassword2) ;
                if (test) {
                    c.format("Your passwords do not match. Try again.%n") ;
                } else {
                    c.format("The password for %s changed.%n", login) ;
                }
                
            } while (test) ;        
  }  
}
The Arrays.equals method is used to test the two character arrays. If they contain the same values, the Arrays.equals method will return a boolean value.
The format method writes output to the console's outputstream using the specified format. It is similar to a printf method because it enables more control over how the output should display.
import java.util.Arrays ;
import java.io.* ;
public class SampleConsole {
    
    public static void main (String args[]) throws IOException {

        Console c = System.console() ;
        if (c == null) {
            System.err.println("No console is available.") ;
            System.exit(1) ;
        }

        String login = c.readLine("Please enter your login information: ") ;
        char [] oldPassword = c.readPassword("Enter your old password: ") ;
      boolean test ;
        
            do {
                char [] newPassword1 =
                    c.readPassword("Input your new password: ") ;
                char [] newPassword2 =
                    c.readPassword("Input the new password again: ") ;
                test = ! Arrays.equals(newPassword1, newPassword2) ;
                if (test) {
                    c.format("Your passwords do not match. Try again.%n") ;
                } else {
                    c.format("The password for %s changed.%n", login) ;
                }
                
            } while (test) ;        
  }  
}
The %s coversion type format is used to represent a value as a String. The login varaible represents the value that is being formatted by %s and the %n conversion type format is equivalent to a carriage return.

0 comments:

Post a Comment