KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > io > PasswordField


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util.io;
16
17 import java.io.*;
18 import java.util.*;
19 import java.util.logging.*;
20
21 /**
22  * This class prompts the user for a password and attempts to mask
23  * input.
24  * @since 1.4
25  */

26 public class PasswordField {
27     private static Logger logger = Logger.getLogger(PasswordField.class.getName());
28
29     /**
30      * @param prompt The prompt to display to the user.
31      * @return The password as entered by the user.
32      */

33     public static final char[] getPassword(String JavaDoc prompt)
34             throws IOException {
35        return getPassword(System.in, prompt);
36     }
37
38     /**
39      * @param in input stream to be used (e.g. System.in)
40      * @param prompt The prompt to display to the user.
41      * @return The password as entered by the user.
42      */

43     public static final char[] getPassword(InputStream in, String JavaDoc prompt)
44             throws IOException {
45         MaskingThread maskingthread = new MaskingThread(prompt);
46         Thread JavaDoc thread = new Thread JavaDoc(maskingthread);
47         thread.start();
48
49         char[] lineBuffer;
50         char[] buf;
51         int i;
52
53         buf = lineBuffer = new char[128];
54
55         int room = buf.length;
56         int offset = 0;
57         int c;
58
59         loop: while(true) {
60             c = in.read();
61             switch(c) {
62                 case -1:
63                 case '\n':
64                    break loop;
65
66                 case '\r':
67                    int c2 = in.read();
68                    if((c2 != '\n') && (c2 != -1)) {
69                       if(!(in instanceof PushbackInputStream)) {
70                          in = new PushbackInputStream(in);
71                       }
72                       ((PushbackInputStream)in).unread(c2);
73                     } else {
74                       break loop;
75                     }
76                 default:
77                    if (--room < 0) {
78                       buf = new char[offset + 128];
79                       room = buf.length - offset - 1;
80                       System.arraycopy(lineBuffer, 0, buf, 0, offset);
81                       Arrays.fill(lineBuffer, ' ');
82                       lineBuffer = buf;
83                    }
84                    buf[offset++] = (char) c;
85                    break;
86             }
87         }
88         maskingthread.stopMasking();
89         System.out.print("\010");
90         //Code to clear doskey on win nt/2000 - Alt+F7
91
String JavaDoc os = System.getProperty("os.name");
92         if(os!=null && os.toLowerCase().startsWith("windows")) {
93             try {
94                 java.awt.Robot JavaDoc robot = new java.awt.Robot JavaDoc();
95                 robot.keyPress(java.awt.event.KeyEvent.VK_ALT);
96                 robot.keyPress(java.awt.event.KeyEvent.VK_F7);
97                 robot.keyRelease(java.awt.event.KeyEvent.VK_F7);
98                 robot.keyRelease(java.awt.event.KeyEvent.VK_ALT);
99             } catch(Exception JavaDoc ignore) {
100                 logger.warning("Could not clears command history: "+ignore);
101             }
102         }
103
104         if(offset == 0) {
105             return null;
106         }
107         char[] ret = new char[offset];
108         System.arraycopy(buf, 0, ret, 0, offset);
109         Arrays.fill(buf, ' ');
110         return ret;
111     }
112 }
113
Popular Tags