KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cvsgrab > util > PasswordField


1 /*
2  * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification,are permitted provided that the following conditions are met:
6  * - Redistribution of source code must retain the above copyright notice, this list
7  * of conditions and the following disclaimer.
8  * - Redistribution in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  *
12  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
13  * be used to endorse or promote products derived from this software without
14  * specific prior written permission.
15  * This software is provided "AS IS," without a warranty of any kind. ALL
16  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
17  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
18  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND
19  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS
20  * A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
21  * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
22  * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
23  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
24  * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
25  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
26  *
27  * You acknowledge that this software is not designed, licensed or intended for
28  * use in the design, construction, operation or maintenance of any nuclear facility.
29  */

30
31 package net.sourceforge.cvsgrab.util;
32
33 import java.io.*;
34
35 /**
36  * This class prompts the user for a password and attempts to mask input with ""
37  *
38  * @author <a HREF=mailto:"qmahmoud@javacourses.com">Qusay H. Mahmoud</a>
39  */

40
41 public class PasswordField {
42
43     /**
44      * Gets the passowrd while masking the user input.
45      *
46      * @param prompt The prompt to display to the user.
47      * @return The password as entered by the user.
48      */

49     public String JavaDoc getPassword(String JavaDoc prompt) throws IOException {
50         // password holder
51
String JavaDoc password = "";
52         MaskingThread maskingthread = new MaskingThread(prompt);
53         Thread JavaDoc thread = new Thread JavaDoc(maskingthread);
54         thread.start();
55         // block until enter is pressed
56
while (true) {
57             char c = (char) System.in.read();
58             // assume enter pressed, stop masking
59
maskingthread.stopMasking();
60
61             if (c == '\r') {
62                 c = (char) System.in.read();
63                 if (c == '\n') {
64                     break;
65                 } else {
66                     continue;
67                 }
68             } else if (c == '\n') {
69                 break;
70             } else {
71                 // store the password
72
password += c;
73             }
74         }
75         return password;
76     }
77 }
Popular Tags