im not sure because im not sure who does the echoing.. DOS, or the java
program..
if dost does the echoing, erm, well.. im more stuck!
It's DOS, not Java doing the echoing. I wrote this little hack once,
verified to work on Win2k and bash shells:
import java.io.*;
public class ConsolePassword {
public static void main(String[] args) throws Exception {
Eraser eraser = new Eraser();
System.out.print("Password? ");
eraser.start();
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
String pass = stdin.readLine();
eraser.halt();
// kill the unwanted *
// comment out the next line to see what I mean
System.out.print("\b");
System.out.println("Password: '" + pass + "'");
}
}
class Eraser extends Thread {
private boolean shouldRun = true;
public void run() {
while (shouldRun) {
System.out.print("\b*");
}
}
public synchronized void halt() {
shouldRun = false;
}
}
It just backspaces out anything you type and replaces it with a *. Use at
your own peril.
Michiel
start a new InputStreamReader
do a read.. it will block for 1 char..
i then immediately after it unblocks, print a carriage return followed by an
int indicating the number of chars read.. which wipes over the echoed
character (and provides the user with an indication of how much they
typed)..
looking at the code there... i see how crafty it is.. when the user types
nothing, the program is constantly typing * [back] * [back] * [back] *
[back] * [back] .... typing a character of your own, advances the cursor
one, and the program sets about repeatedly obliterating what you typed there
instead..
i like it!
"Michiel Konstapel" <a...@me.nl> wrote in message
news:c_xn8.150735$Fw2.4...@nlnews00.chello.com...
Yes, that would work if:
- the console returns the characters on a char-by-char basis, which by
default it doesn't. On Linux, you can do something with stty to set it to
that mode.
- CR just goes back to the start of the line without doing a line feed.
Might work.
> looking at the code there... i see how crafty it is.. when the user types
> nothing, the program is constantly typing * [back] * [back] * [back] *
> [back] * [back] .... typing a character of your own, advances the cursor
> one, and the program sets about repeatedly obliterating what you typed
there
> instead..
>
> i like it!
:)
Heh.. unfortunately, unlike sockets, typing in a character at the DOS
console doesnt transmit it to the underlying application immediately..
I cant even see how it can be accurately done with a socket loopback,
because even still, dos will echo the chars as it transmits them to
System.in... telnet must do something funky with the console in an
os-dependent way...
i think your hack is as close as can reasonably be achieved without
disproportionate effort.. the only thing i have been pondering on is how to
get rid of the first, extraneous *.. type a 3 letter password, and it looks
like you typed 4..
hrmm...
With the right OS you can do this:
public static void echo(boolean on) {
try {
String[] cmd = {
"/bin/sh",
"-c",
"/bin/stty " + (on ? "echo" : "-echo") + " < /dev/tty"
};
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
}
catch (IOException e) { }
catch (InterruptedException e) { }
}
Then use echo(false) to turn off echo, and echo(true) to turn it back
on again:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
echo(false);
System.out.print("enter password: ");
String pass = br.readLine();
System.out.println();
System.out.println("you entered: " + pass);
}
finally {
echo(true);
}
(A similar mechanism can be used to set up the console for character
at a time input.)
/gordon
--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
Yeah, I've been thinking about that but haven't come up with a solution,
either.
Michiel