/*
* Copyright 2011 Jesper Terkelsen.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package dk.deck.remoteconsole.input;
import java.io.PrintStream;
/**
* This class attempts to erase characters echoed to the console.
* @author Jesper Terkelsen
*/
class MaskingThread extends Thread {
private volatile boolean stop;
private char echochar = '*';
private PrintStream out;
/**
* @param prompt The prompt displayed to the user
* @param out The PrintStream to mask on (e.g. System.out)
*/
public MaskingThread(String prompt, PrintStream out) {
this.out = out;
this.out.print(prompt);
}
/**
* Begin masking until asked to stop.
*/
@Override
public void run() {
int priority = Thread.currentThread().getPriority();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
try {
stop = true;
while (stop) {
out.print("\010" + echochar);
try {
// attempt masking at this rate
Thread.currentThread().sleep(1);
} catch (InterruptedException iex) {
Thread.currentThread().interrupt();
}
}
} finally { // restore the original priority
Thread.currentThread().setPriority(priority);
}
}
/**
* Instruct the thread to stop masking.
*/
public void stopMasking() {
this.stop = false;
}
}