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 |
Eclipse User |
|
|
|
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 |
Kevin Barnes 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 |
Eclipse User |
|
|
|
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 #215751 is a reply to message #215704] |
Tue, 27 September 2005 14:42 |
Kevin Barnes 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));
>>> }
>>> }
>>> }
|
|
|
Goto Forum:
Current Time: Fri Apr 26 23:38:24 GMT 2024
Powered by FUDForum. Page generated in 0.03450 seconds
|