• Post Reply Bookmark Topic Watch Topic
  • New Topic

Password Masking

 
denis sorn
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am reading this article and wondering how the synchronization between EraserThread thread and PasswordField thread is happening? You'll find code under 'Simple Solution' for 'Command-Line Input Masking'.

This is relevant part of PasswordField.java:

and this of EraserThread.java:


I would expect that EraserThread runs for a while and prints '*' chars. But somehow it waits until you hit a key...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints out "\010*". The \010 part is ascii for backspace. So what it does is replace the last character on the command line with an asterix.

Here is how I guess it works:
1) Before you type anything in, the *s are added and removed faster than they get displayed. Net result: You see nothing.
2) You type in a character, which naturally becomes the last character on the line. The EraserThread uses a backspace to get rid of that character, and adds an *. Net result: one extra * is added to the output at the location of the character you typed
3) Before you type in the next character, the previous * is removed then re-added faster than they get displayed. Net result: You see the * replacing the last character (and all previous characters).
 
denis sorn
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve! I asked my self what character \010 is, but then I tried without it and it seemed to me that it works only with '*'. I actually compiled wrong file . Without \010 it prints a bunch of ****s.
Regarding 'You see nothing' there is an asterisk at the beginning of the line before you type anything.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
denis sorn wrote:Thanks Steve! I asked my self what character \010 is, but then I tried without it and it seemed to me that it works only with '*'. I actually compiled wrong file . Without \010 it prints a bunch of ****s.
Regarding 'You see nothing' there is an asterisk at the beginning of the line before you type anything.


Did you actually get this working? I'm trying to do this, java 1.5, using eclipse, and no matter what i do, i just get squares and stars running forever on the line.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!