Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Echoing of characters at te console; How to turn off?

193 views
Skip to first unread message

Sum Ting Wong

unread,
Mar 24, 2002, 6:44:45 PM3/24/02
to
I'm thinking of creating a PrintStream that drops requests to print data to
the console, and using System.setOut(silentPrintStream) to stop echoing of
characters.. is there a better way? is this a way that will work?

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!


Michiel Konstapel

unread,
Mar 24, 2002, 11:48:08 PM3/24/02
to
"Sum Ting Wong" <cha...@hotmail.com> wrote in message
news:1017013495.84953@dyfi...

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


Sum Ting Wong

unread,
Mar 25, 2002, 5:13:34 PM3/25/02
to
ahh.. i had a similar idea in this:


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...

Michiel Konstapel

unread,
Mar 25, 2002, 5:38:19 PM3/25/02
to
"Sum Ting Wong" <cha...@hotmail.com> wrote in message
news:1017094432.707022@dyfi...

> ahh.. i had a similar idea in this:
>
>
> 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)..

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!

:)


Sum Ting Wong

unread,
Mar 26, 2002, 7:17:24 AM3/26/02
to

"Michiel Konstapel" <a...@me.nl> wrote in message
news:vFNn8.156866$Fw2.4...@nlnews00.chello.com...

> "Sum Ting Wong" <cha...@hotmail.com> wrote in message
> news:1017094432.707022@dyfi...
> > ahh.. i had a similar idea in this:
> >
> >
> > 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)..
>
> 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.

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...


Gordon Beaton

unread,
Mar 26, 2002, 8:29:22 AM3/26/02
to
On Tue, 26 Mar 2002 12:17:24 -0000, Sum Ting Wong wrote:
> i think your hack is as close as can reasonably be achieved without
> disproportionate effort..

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

Michiel Konstapel

unread,
Mar 26, 2002, 8:01:13 PM3/26/02
to
"Sum Ting Wong" <cha...@hotmail.com> wrote in message
news:1017145068.35554@dyfi...
<snip>

> 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...

Yeah, I've been thinking about that but haven't come up with a solution,
either.
Michiel


0 new messages