001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.util.io;
016:
017: import java.io.*;
018: import java.util.*;
019: import java.util.logging.*;
020:
021: /**
022: * This class prompts the user for a password and attempts to mask
023: * input.
024: * @since 1.4
025: */
026: public class PasswordField {
027: private static Logger logger = Logger.getLogger(PasswordField.class
028: .getName());
029:
030: /**
031: * @param prompt The prompt to display to the user.
032: * @return The password as entered by the user.
033: */
034: public static final char[] getPassword(String prompt)
035: throws IOException {
036: return getPassword(System.in, prompt);
037: }
038:
039: /**
040: * @param in input stream to be used (e.g. System.in)
041: * @param prompt The prompt to display to the user.
042: * @return The password as entered by the user.
043: */
044: public static final char[] getPassword(InputStream in, String prompt)
045: throws IOException {
046: MaskingThread maskingthread = new MaskingThread(prompt);
047: Thread thread = new Thread(maskingthread);
048: thread.start();
049:
050: char[] lineBuffer;
051: char[] buf;
052: int i;
053:
054: buf = lineBuffer = new char[128];
055:
056: int room = buf.length;
057: int offset = 0;
058: int c;
059:
060: loop: while (true) {
061: c = in.read();
062: switch (c) {
063: case -1:
064: case '\n':
065: break loop;
066:
067: case '\r':
068: int c2 = in.read();
069: if ((c2 != '\n') && (c2 != -1)) {
070: if (!(in instanceof PushbackInputStream)) {
071: in = new PushbackInputStream(in);
072: }
073: ((PushbackInputStream) in).unread(c2);
074: } else {
075: break loop;
076: }
077: default:
078: if (--room < 0) {
079: buf = new char[offset + 128];
080: room = buf.length - offset - 1;
081: System.arraycopy(lineBuffer, 0, buf, 0, offset);
082: Arrays.fill(lineBuffer, ' ');
083: lineBuffer = buf;
084: }
085: buf[offset++] = (char) c;
086: break;
087: }
088: }
089: maskingthread.stopMasking();
090: System.out.print("\010");
091: //Code to clear doskey on win nt/2000 - Alt+F7
092: String os = System.getProperty("os.name");
093: if (os != null && os.toLowerCase().startsWith("windows")) {
094: try {
095: java.awt.Robot robot = new java.awt.Robot();
096: robot.keyPress(java.awt.event.KeyEvent.VK_ALT);
097: robot.keyPress(java.awt.event.KeyEvent.VK_F7);
098: robot.keyRelease(java.awt.event.KeyEvent.VK_F7);
099: robot.keyRelease(java.awt.event.KeyEvent.VK_ALT);
100: } catch (Exception ignore) {
101: logger.warning("Could not clears command history: "
102: + ignore);
103: }
104: }
105:
106: if (offset == 0) {
107: return null;
108: }
109: char[] ret = new char[offset];
110: System.arraycopy(buf, 0, ret, 0, offset);
111: Arrays.fill(buf, ' ');
112: return ret;
113: }
114: }
|