01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
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:
19: /**
20: * This class attempts to erase characters echoed to the console.
21: * @since 1.4
22: */
23: class MaskingThread extends Thread {
24: private volatile boolean stop;
25:
26: //private char echochar = '*';
27:
28: /**
29: * @param prompt The prompt displayed to the user
30: */
31: public MaskingThread(String prompt) {
32: System.out.print(prompt);
33: }
34:
35: /**
36: * Begin masking until asked to stop.
37: */
38: public void run() {
39: int priority = Thread.currentThread().getPriority();
40: Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
41:
42: try {
43: stop = true;
44: while (stop) {
45: System.out.print("\010 " /*+ echochar*/);
46: try {
47: // attempt masking at this rate
48: Thread.currentThread().sleep(1);
49: } catch (InterruptedException iex) {
50: Thread.currentThread().interrupt();
51: return;
52: }
53: }
54: } finally { // restore the original priority
55: Thread.currentThread().setPriority(priority);
56: }
57: }
58:
59: /**
60: * Instruct the thread to stop masking.
61: */
62: public void stopMasking() {
63: this .stop = false;
64: }
65: }
|