1 | /*
|
---|
2 | Copyright (c) Dale Anson, 2004
|
---|
3 | Redistribution and use in source and binary forms, with or without
|
---|
4 | modification, are permitted provided that the following conditions are met:
|
---|
5 | 1. Redistributions of source code must retain the above copyright notice,
|
---|
6 | this list of conditions and the following disclaimer.
|
---|
7 | 2. Redistributions in binary form must reproduce the above copyright
|
---|
8 | notice, this list of conditions and the following disclaimer in the
|
---|
9 | documentation and/or other materials provided with the distribution.
|
---|
10 | 3. The name of the author may not be used to endorse or promote products
|
---|
11 | derived from this software without specific prior written permission.
|
---|
12 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
---|
13 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
---|
14 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
---|
15 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
16 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
17 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
---|
18 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
---|
19 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
---|
20 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
---|
21 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
22 | */
|
---|
23 | package ise.antelope.tasks.password;
|
---|
24 |
|
---|
25 | import java.io.*;
|
---|
26 | import java.nio.*;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * An input stream for getting a password from the command line, provides for
|
---|
30 | * masking of user input. Reads and masks input from the command line, then
|
---|
31 | * stops getting input from the command line on the first line separator.
|
---|
32 | *
|
---|
33 | * @version $Revision: 1.1 $
|
---|
34 | */
|
---|
35 | public class PasswordInputStream extends InputStream {
|
---|
36 |
|
---|
37 | private StringBuffer buffer = new StringBuffer();
|
---|
38 | private String LS = System.getProperty("line.separator");
|
---|
39 | private InputStream _system_in = System.in;
|
---|
40 |
|
---|
41 | private Thread reader = null;
|
---|
42 |
|
---|
43 | private int ptr = 0;
|
---|
44 |
|
---|
45 | /** Constructor for PasswordInputStream */
|
---|
46 | public PasswordInputStream() {
|
---|
47 | _system_in = System.in;
|
---|
48 | _system_in.mark(256);
|
---|
49 | System.setIn(this);
|
---|
50 |
|
---|
51 | reader =
|
---|
52 | new Thread() {
|
---|
53 | public void run() {
|
---|
54 | try {
|
---|
55 | _system_in.reset();
|
---|
56 | }
|
---|
57 | catch (Exception ignored) {
|
---|
58 | }
|
---|
59 | do {
|
---|
60 | try {
|
---|
61 | int c = _system_in.read();
|
---|
62 | buffer.append((char) c);
|
---|
63 | if (buffer.indexOf(LS) != -1) {
|
---|
64 | System.setIn(_system_in);
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | sleep(1);
|
---|
68 | }
|
---|
69 | catch (Exception e) {
|
---|
70 | e.printStackTrace();
|
---|
71 | }
|
---|
72 | } while (true);
|
---|
73 | }
|
---|
74 | };
|
---|
75 | reader.start();
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Reads the next byte of data from the input stream. The value byte is
|
---|
81 | * returned as an int in the range 0 to 255. If no byte is available because
|
---|
82 | * the end of the stream has been reached, we're in trouble because we're
|
---|
83 | * reading from System.in. This method blocks until input data is available,
|
---|
84 | * the end of the stream is detected, or an exception is thrown.
|
---|
85 | *
|
---|
86 | * @return the next byte of data
|
---|
87 | * @exception IOException if an I/O error occurs.
|
---|
88 | */
|
---|
89 | public int read() throws IOException {
|
---|
90 | if (buffer.indexOf(LS) != -1) {
|
---|
91 | for (int i = 0; i < LS.length(); i++)
|
---|
92 | System.out.print("\b");
|
---|
93 | return -1;
|
---|
94 | }
|
---|
95 | while (true) {
|
---|
96 | if (buffer.length() > 0 && ptr < buffer.length()) {
|
---|
97 | int i = (int) buffer.charAt(ptr);
|
---|
98 | ++ptr;
|
---|
99 | return i;
|
---|
100 | }
|
---|
101 | System.out.print("\b ");
|
---|
102 | try {
|
---|
103 | Thread.currentThread().sleep(1);
|
---|
104 | }
|
---|
105 | catch (Exception e) {
|
---|
106 | throw new IOException();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|