Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Different results in Eclipse console and normal WinXP console
Different results in Eclipse console and normal WinXP console [message #215566] Sat, 24 September 2005 07:34 Go to next message
Eclipse UserFriend
Originally posted by: guozheng.ge.gmail.com

Hi, there

I am trying to add echo, e.g. "*", for users to securely enter password
on the command line. I managed to find a good tutorial at SDN
( http://java.sun.com/developer/technicalArticles/Security/pwo rdmask/).
The basic idea is to have a separate thread to wipe off the input from
InputStream.

I tried the sample code in Eclipse, however, it keeps popping out
\010(backspace) and * in Eclipse console. I tried the same thing in
WinXP console, it seems to be all right.

My guess is that might be related to thread handling in Eclipse console,
anyone knows the details for the problem? Below are the three classes
used in the sample.

Thanks,

guozheng

------------------------------------------------------------ -------

//MaskingThread.java

import java.io.*;

/**
* This class attempts to erase characters echoed to the console.
*/

class MaskingThread extends Thread {
private volatile boolean stop;
private char echochar = '*';

/**
*@param prompt The prompt displayed to the user
*/
public MaskingThread(String prompt) {
System.out.print(prompt);
}

/**
* Begin masking until asked to stop.
*/
public void run() {

int priority = Thread.currentThread().getPriority();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

try {
stop = true;
while(stop) {
System.out.print("\010" + echochar);
try {
// attempt masking at this rate
Thread.currentThread().sleep(1);
}catch (InterruptedException iex) {
Thread.currentThread().interrupt();
return;
}
}
} finally { // restore the original priority
Thread.currentThread().setPriority(priority);
}
}

/**
* Instruct the thread to stop masking.
*/
public void stopMasking() {
this.stop = false;
}
}

------------------------------------------------------------ ------------
//PasswordField.java

import java.io.*;
import java.util.*;

/**
* This class prompts the user for a password and attempts to mask
input with "*"
*/

public class PasswordField {

/**
*@param input stream to be used (e.g. System.in)
*@param prompt The prompt to display to the user.
*@return The password as entered by the user.
*/

public static final char[] getPassword(InputStream in, String
prompt) throws IOException {
MaskingThread maskingthread = new MaskingThread(prompt);
Thread thread = new Thread(maskingthread);
thread.start();

char[] lineBuffer;
char[] buf;
int i;

buf = lineBuffer = new char[128];

int room = buf.length;
int offset = 0;
int c;

loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;

case '\r':
int c2 = in.read();
if ((c2 != '\n') && (c2 != -1)) {
if (!(in instanceof PushbackInputStream)) {
in = new PushbackInputStream(in);
}
((PushbackInputStream)in).unread(c2);
} else {
break loop;
}

default:
if (--room < 0) {
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
Arrays.fill(lineBuffer, ' ');
lineBuffer = buf;
}
buf[offset++] = (char) c;
break;
}
}
maskingthread.stopMasking();
if (offset == 0) {
return null;
}
char[] ret = new char[offset];
System.arraycopy(buf, 0, ret, 0, offset);
Arrays.fill(buf, ' ');
return ret;
}
}

------------------------------------------------------------ ------
//PasswordApp.java

import java.io.*;

public class PasswordApp {
public static void main(String argv[]) {
char password[] = null;
try {
password = PasswordField.getPassword(System.in, "Enter your
password: ");
} catch(IOException ioe) {
ioe.printStackTrace();
}
if(password == null ) {
System.out.println("No password entered");
} else {
System.out.println("The password entered is:
"+String.valueOf(password));
}
}
}
Re: Different results in Eclipse console and normal WinXP console [message #215672 is a reply to message #215566] Mon, 26 September 2005 16:19 Go to previous messageGo to next message
Kevin Barnes is currently offline Kevin BarnesFriend
Messages: 174
Registered: July 2009
Senior Member
This is a known bug. The console in Eclipse does not handle \b (or \010)
properly.
see https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936 for the details.
Kevin


Guozheng Ge wrote:
> Hi, there
>
> I am trying to add echo, e.g. "*", for users to securely enter password
> on the command line. I managed to find a good tutorial at SDN
> ( http://java.sun.com/developer/technicalArticles/Security/pwo rdmask/).
> The basic idea is to have a separate thread to wipe off the input from
> InputStream.
>
> I tried the sample code in Eclipse, however, it keeps popping out
> \010(backspace) and * in Eclipse console. I tried the same thing in
> WinXP console, it seems to be all right.
>
> My guess is that might be related to thread handling in Eclipse console,
> anyone knows the details for the problem? Below are the three classes
> used in the sample.
>
> Thanks,
>
> guozheng
>
> ------------------------------------------------------------ -------
>
> //MaskingThread.java
>
> import java.io.*;
>
> /**
> * This class attempts to erase characters echoed to the console.
> */
>
> class MaskingThread extends Thread {
> private volatile boolean stop;
> private char echochar = '*';
>
> /**
> *@param prompt The prompt displayed to the user
> */
> public MaskingThread(String prompt) {
> System.out.print(prompt);
> }
>
> /**
> * Begin masking until asked to stop.
> */
> public void run() {
>
> int priority = Thread.currentThread().getPriority();
> Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
>
> try {
> stop = true;
> while(stop) {
> System.out.print("\010" + echochar);
> try {
> // attempt masking at this rate
> Thread.currentThread().sleep(1);
> }catch (InterruptedException iex) {
> Thread.currentThread().interrupt();
> return;
> }
> }
> } finally { // restore the original priority
> Thread.currentThread().setPriority(priority);
> }
> }
>
> /**
> * Instruct the thread to stop masking.
> */
> public void stopMasking() {
> this.stop = false;
> }
> }
>
> ------------------------------------------------------------ ------------
> //PasswordField.java
>
> import java.io.*;
> import java.util.*;
>
> /**
> * This class prompts the user for a password and attempts to mask input
> with "*"
> */
>
> public class PasswordField {
>
> /**
> *@param input stream to be used (e.g. System.in)
> *@param prompt The prompt to display to the user.
> *@return The password as entered by the user.
> */
>
> public static final char[] getPassword(InputStream in, String prompt)
> throws IOException {
> MaskingThread maskingthread = new MaskingThread(prompt);
> Thread thread = new Thread(maskingthread);
> thread.start();
>
> char[] lineBuffer;
> char[] buf;
> int i;
>
> buf = lineBuffer = new char[128];
>
> int room = buf.length;
> int offset = 0;
> int c;
>
> loop: while (true) {
> switch (c = in.read()) {
> case -1:
> case '\n':
> break loop;
>
> case '\r':
> int c2 = in.read();
> if ((c2 != '\n') && (c2 != -1)) {
> if (!(in instanceof PushbackInputStream)) {
> in = new PushbackInputStream(in);
> }
> ((PushbackInputStream)in).unread(c2);
> } else {
> break loop;
> }
>
> default:
> if (--room < 0) {
> buf = new char[offset + 128];
> room = buf.length - offset - 1;
> System.arraycopy(lineBuffer, 0, buf, 0, offset);
> Arrays.fill(lineBuffer, ' ');
> lineBuffer = buf;
> }
> buf[offset++] = (char) c;
> break;
> }
> }
> maskingthread.stopMasking();
> if (offset == 0) {
> return null;
> }
> char[] ret = new char[offset];
> System.arraycopy(buf, 0, ret, 0, offset);
> Arrays.fill(buf, ' ');
> return ret;
> }
> }
>
> ------------------------------------------------------------ ------
> //PasswordApp.java
>
> import java.io.*;
>
> public class PasswordApp {
> public static void main(String argv[]) {
> char password[] = null;
> try {
> password = PasswordField.getPassword(System.in, "Enter your
> password: ");
> } catch(IOException ioe) {
> ioe.printStackTrace();
> }
> if(password == null ) {
> System.out.println("No password entered");
> } else {
> System.out.println("The password entered is:
> "+String.valueOf(password));
> }
> }
> }
Re: Different results in Eclipse console and normal WinXP console [message #215704 is a reply to message #215672] Mon, 26 September 2005 22:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: guozheng.ge.gmail.com

Thanks a lot, Kevin. It is quite wired that the bug's status is resolved
while it apparently is not: I am using the latest version of Eclipse.
Hope this issue will be addressed soon.

guozheng

Kevin Barnes wrote:
>
> This is a known bug. The console in Eclipse does not handle \b (or \010)
> properly.
> see https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936 for the details.
> Kevin
>
>
> Guozheng Ge wrote:
>
>> Hi, there
>>
>> I am trying to add echo, e.g. "*", for users to securely enter
>> password on the command line. I managed to find a good tutorial at SDN
>> ( http://java.sun.com/developer/technicalArticles/Security/pwo rdmask/).
>> The basic idea is to have a separate thread to wipe off the input from
>> InputStream.
>>
>> I tried the sample code in Eclipse, however, it keeps popping out
>> \010(backspace) and * in Eclipse console. I tried the same thing in
>> WinXP console, it seems to be all right.
>>
>> My guess is that might be related to thread handling in Eclipse
>> console, anyone knows the details for the problem? Below are the
>> three classes used in the sample.
>>
>> Thanks,
>>
>> guozheng
>>
>> ------------------------------------------------------------ -------
>>
>> //MaskingThread.java
>>
>> import java.io.*;
>>
>> /**
>> * This class attempts to erase characters echoed to the console.
>> */
>>
>> class MaskingThread extends Thread {
>> private volatile boolean stop;
>> private char echochar = '*';
>>
>> /**
>> *@param prompt The prompt displayed to the user
>> */
>> public MaskingThread(String prompt) {
>> System.out.print(prompt);
>> }
>>
>> /**
>> * Begin masking until asked to stop.
>> */
>> public void run() {
>>
>> int priority = Thread.currentThread().getPriority();
>> Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
>>
>> try {
>> stop = true;
>> while(stop) {
>> System.out.print("\010" + echochar);
>> try {
>> // attempt masking at this rate
>> Thread.currentThread().sleep(1);
>> }catch (InterruptedException iex) {
>> Thread.currentThread().interrupt();
>> return;
>> }
>> }
>> } finally { // restore the original priority
>> Thread.currentThread().setPriority(priority);
>> }
>> }
>>
>> /**
>> * Instruct the thread to stop masking.
>> */
>> public void stopMasking() {
>> this.stop = false;
>> }
>> }
>>
>> ------------------------------------------------------------ ------------
>> //PasswordField.java
>>
>> import java.io.*;
>> import java.util.*;
>>
>> /**
>> * This class prompts the user for a password and attempts to mask
>> input with "*"
>> */
>>
>> public class PasswordField {
>>
>> /**
>> *@param input stream to be used (e.g. System.in)
>> *@param prompt The prompt to display to the user.
>> *@return The password as entered by the user.
>> */
>>
>> public static final char[] getPassword(InputStream in, String
>> prompt) throws IOException {
>> MaskingThread maskingthread = new MaskingThread(prompt);
>> Thread thread = new Thread(maskingthread);
>> thread.start();
>> char[] lineBuffer;
>> char[] buf;
>> int i;
>>
>> buf = lineBuffer = new char[128];
>>
>> int room = buf.length;
>> int offset = 0;
>> int c;
>>
>> loop: while (true) {
>> switch (c = in.read()) {
>> case -1:
>> case '\n':
>> break loop;
>>
>> case '\r':
>> int c2 = in.read();
>> if ((c2 != '\n') && (c2 != -1)) {
>> if (!(in instanceof PushbackInputStream)) {
>> in = new PushbackInputStream(in);
>> }
>> ((PushbackInputStream)in).unread(c2);
>> } else {
>> break loop;
>> }
>>
>> default:
>> if (--room < 0) {
>> buf = new char[offset + 128];
>> room = buf.length - offset - 1;
>> System.arraycopy(lineBuffer, 0, buf, 0, offset);
>> Arrays.fill(lineBuffer, ' ');
>> lineBuffer = buf;
>> }
>> buf[offset++] = (char) c;
>> break;
>> }
>> }
>> maskingthread.stopMasking();
>> if (offset == 0) {
>> return null;
>> }
>> char[] ret = new char[offset];
>> System.arraycopy(buf, 0, ret, 0, offset);
>> Arrays.fill(buf, ' ');
>> return ret;
>> }
>> }
>>
>> ------------------------------------------------------------ ------
>> //PasswordApp.java
>>
>> import java.io.*;
>>
>> public class PasswordApp {
>> public static void main(String argv[]) {
>> char password[] = null;
>> try {
>> password = PasswordField.getPassword(System.in, "Enter your
>> password: ");
>> } catch(IOException ioe) {
>> ioe.printStackTrace();
>> }
>> if(password == null ) {
>> System.out.println("No password entered");
>> } else {
>> System.out.println("The password entered is:
>> "+String.valueOf(password));
>> }
>> }
>> }
Re: Different results in Eclipse console and normal WinXP console [message #215718 is a reply to message #215704] Tue, 27 September 2005 07:11 Go to previous messageGo to next message
Johnnie Hougaard Nielsen is currently offline Johnnie Hougaard NielsenFriend
Messages: 20
Registered: July 2009
Junior Member
Guozheng Ge wrote:
> It is quite wired that the bug's status is resolved
> while it apparently is not

Well, it is "only" resolved that the issue will be resolved later....
> Resolution: LATER
Re: Different results in Eclipse console and normal WinXP console [message #215751 is a reply to message #215704] Tue, 27 September 2005 14:42 Go to previous message
Kevin Barnes is currently offline Kevin BarnesFriend
Messages: 174
Registered: July 2009
Senior Member
There is not currently anyone working on that bug. The debug team uses
Resolved:Later to mark bugs that are unlikely to get fixed in the
current release cycle.
If this is a blocker for you I'd suggest that you vote for the bug and
add yourself as a CC on it. It would be even better if you could supply
a patch!
Kevin

Guozheng Ge wrote:
> Thanks a lot, Kevin. It is quite wired that the bug's status is resolved
> while it apparently is not: I am using the latest version of Eclipse.
> Hope this issue will be addressed soon.
>
> guozheng
>
> Kevin Barnes wrote:
>
>>
>> This is a known bug. The console in Eclipse does not handle \b (or
>> \010) properly.
>> see https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936 for the details.
>> Kevin
>>
>>
>> Guozheng Ge wrote:
>>
>>> Hi, there
>>>
>>> I am trying to add echo, e.g. "*", for users to securely enter
>>> password on the command line. I managed to find a good tutorial at
>>> SDN
>>> ( http://java.sun.com/developer/technicalArticles/Security/pwo rdmask/).
>>> The basic idea is to have a separate thread to wipe off the input
>>> from InputStream.
>>>
>>> I tried the sample code in Eclipse, however, it keeps popping out
>>> \010(backspace) and * in Eclipse console. I tried the same thing in
>>> WinXP console, it seems to be all right.
>>>
>>> My guess is that might be related to thread handling in Eclipse
>>> console, anyone knows the details for the problem? Below are the
>>> three classes used in the sample.
>>>
>>> Thanks,
>>>
>>> guozheng
>>>
>>> ------------------------------------------------------------ -------
>>>
>>> //MaskingThread.java
>>>
>>> import java.io.*;
>>>
>>> /**
>>> * This class attempts to erase characters echoed to the console.
>>> */
>>>
>>> class MaskingThread extends Thread {
>>> private volatile boolean stop;
>>> private char echochar = '*';
>>>
>>> /**
>>> *@param prompt The prompt displayed to the user
>>> */
>>> public MaskingThread(String prompt) {
>>> System.out.print(prompt);
>>> }
>>>
>>> /**
>>> * Begin masking until asked to stop.
>>> */
>>> public void run() {
>>>
>>> int priority = Thread.currentThread().getPriority();
>>> Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
>>>
>>> try {
>>> stop = true;
>>> while(stop) {
>>> System.out.print("\010" + echochar);
>>> try {
>>> // attempt masking at this rate
>>> Thread.currentThread().sleep(1);
>>> }catch (InterruptedException iex) {
>>> Thread.currentThread().interrupt();
>>> return;
>>> }
>>> }
>>> } finally { // restore the original priority
>>> Thread.currentThread().setPriority(priority);
>>> }
>>> }
>>>
>>> /**
>>> * Instruct the thread to stop masking.
>>> */
>>> public void stopMasking() {
>>> this.stop = false;
>>> }
>>> }
>>>
>>> ------------------------------------------------------------ ------------
>>> //PasswordField.java
>>>
>>> import java.io.*;
>>> import java.util.*;
>>>
>>> /**
>>> * This class prompts the user for a password and attempts to mask
>>> input with "*"
>>> */
>>>
>>> public class PasswordField {
>>>
>>> /**
>>> *@param input stream to be used (e.g. System.in)
>>> *@param prompt The prompt to display to the user.
>>> *@return The password as entered by the user.
>>> */
>>>
>>> public static final char[] getPassword(InputStream in, String
>>> prompt) throws IOException {
>>> MaskingThread maskingthread = new MaskingThread(prompt);
>>> Thread thread = new Thread(maskingthread);
>>> thread.start();
>>> char[] lineBuffer;
>>> char[] buf;
>>> int i;
>>>
>>> buf = lineBuffer = new char[128];
>>>
>>> int room = buf.length;
>>> int offset = 0;
>>> int c;
>>>
>>> loop: while (true) {
>>> switch (c = in.read()) {
>>> case -1:
>>> case '\n':
>>> break loop;
>>>
>>> case '\r':
>>> int c2 = in.read();
>>> if ((c2 != '\n') && (c2 != -1)) {
>>> if (!(in instanceof PushbackInputStream)) {
>>> in = new PushbackInputStream(in);
>>> }
>>> ((PushbackInputStream)in).unread(c2);
>>> } else {
>>> break loop;
>>> }
>>>
>>> default:
>>> if (--room < 0) {
>>> buf = new char[offset + 128];
>>> room = buf.length - offset - 1;
>>> System.arraycopy(lineBuffer, 0, buf, 0, offset);
>>> Arrays.fill(lineBuffer, ' ');
>>> lineBuffer = buf;
>>> }
>>> buf[offset++] = (char) c;
>>> break;
>>> }
>>> }
>>> maskingthread.stopMasking();
>>> if (offset == 0) {
>>> return null;
>>> }
>>> char[] ret = new char[offset];
>>> System.arraycopy(buf, 0, ret, 0, offset);
>>> Arrays.fill(buf, ' ');
>>> return ret;
>>> }
>>> }
>>>
>>> ------------------------------------------------------------ ------
>>> //PasswordApp.java
>>>
>>> import java.io.*;
>>>
>>> public class PasswordApp {
>>> public static void main(String argv[]) {
>>> char password[] = null;
>>> try {
>>> password = PasswordField.getPassword(System.in, "Enter your
>>> password: ");
>>> } catch(IOException ioe) {
>>> ioe.printStackTrace();
>>> }
>>> if(password == null ) {
>>> System.out.println("No password entered");
>>> } else {
>>> System.out.println("The password entered is:
>>> "+String.valueOf(password));
>>> }
>>> }
>>> }
Previous Topic:Include jar in ant export?
Next Topic:PropertyChangeEvent
Goto Forum:
  


Current Time: Fri Apr 26 04:24:09 GMT 2024

Powered by FUDForum. Page generated in 0.03080 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top