View Javadoc

1   /***
2    * 
3    */
4   package de.cohesion.bssh.impl.io;
5   
6   /***
7    * @author schulzs
8    */
9   class MaskingThread extends Thread {
10  
11  	private volatile boolean stop;
12  
13  	private char echochar = ' ';
14  
15  	/***
16  	 * @param prompt
17  	 *            The prompt displayed to the user
18  	 */
19  	public MaskingThread(String prompt) {
20  		System.out.print(prompt);
21  	}
22  
23  	/***
24  	 * Begin masking until asked to stop.
25  	 */
26  	public void run() {
27  
28  		int priority = Thread.currentThread().getPriority();
29  		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
30  
31  		try {
32  			stop = true;
33  			while (stop) {
34  				System.out.print("\010" + echochar);
35  				try {
36  					Thread.sleep(1);
37  				} catch (InterruptedException iex) {
38  					Thread.currentThread().interrupt();
39  					return;
40  				}
41  			}
42  		} finally { // restore the original priority
43  			Thread.currentThread().setPriority(priority);
44  		}
45  	}
46  
47  	/***
48  	 * Instruct the thread to stop masking.
49  	 */
50  	public void stopMasking() {
51  		this.stop = false;
52  	}
53  }