| 1 |
/* |
| 2 |
* Copyright (c) 2013 Jean Niklas L'orange. All rights reserved. |
| 3 |
* |
| 4 |
* The use and distribution terms for this software are covered by the |
| 5 |
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
| 6 |
* which can be found in the file LICENSE at the root of this distribution. |
| 7 |
* |
| 8 |
* By using this software in any fashion, you are agreeing to be bound by |
| 9 |
* the terms of this license. |
| 10 |
* |
| 11 |
* You must not remove this notice, or any other, from this software. |
| 12 |
*/ |
| 13 |
|
| 14 |
package com.hypirion.io; |
| 15 |
|
| 16 |
import java.io.Console; |
| 17 |
import java.io.IOException; |
| 18 |
import java.io.InputStream; |
| 19 |
import java.io.InputStreamReader; |
| 20 |
import java.io.OutputStream; |
| 21 |
|
| 22 |
import java.lang.reflect.Field; |
| 23 |
import java.lang.reflect.InvocationTargetException; |
| 24 |
import java.lang.reflect.Method; |
| 25 |
|
| 26 |
/** |
| 27 |
* The ConsoleUtils class is a static collection of methods used to modify or |
| 28 |
* use the console in rather unconventional ways. It is mainly a way to set on |
| 29 |
* and off echoing in the console, either manually or through password reading |
| 30 |
* from an arbitrary InputStream. |
| 31 |
* |
| 32 |
* @author Jean Niklas L'orange |
| 33 |
* @see java.io.Console |
| 34 |
* @since <code>com.hypirion.io 0.3.0</code> |
| 35 |
*/ |
| 36 |
|
| 37 |
public final class ConsoleUtils { |
| 38 |
|
| 39 |
/** |
| 40 |
* A static method which peeks into the {@link java.io.Console} class and |
| 41 |
* manually turns on or off echoing in the given console. |
| 42 |
* |
| 43 |
* @param on whether to set echoing on or off. |
| 44 |
* |
| 45 |
* @exception NoSuchMethodException if Java's own console class have no |
| 46 |
* private method named "echo". |
| 47 |
* @exception NoSuchFieldException if Java's own console class have no |
| 48 |
* private field named "echoOff". |
| 49 |
* @exception InvocationTargetException if Java's own console class have a |
| 50 |
* private method named "echo", but that this method tries to invoke that |
| 51 |
* method erroneously. |
| 52 |
* @exception IllegalAccessException if the access restrictions prohibits |
| 53 |
* this method from modifying and using private methods in Java's own |
| 54 |
* console class. |
| 55 |
*/ |
| 56 |
public static synchronized boolean setEcho(boolean on) |
| 57 |
throws NoSuchMethodException, IllegalAccessException, NoSuchFieldException, InvocationTargetException { |
| 58 |
Class params[] = new Class[1]; |
| 59 |
params[0] = Boolean.TYPE; |
| 60 |
Method echo = Console.class.getDeclaredMethod("echo", params); |
| 61 |
echo.setAccessible(true); |
| 62 |
boolean res = (Boolean) echo.invoke(null, on); |
| 63 |
Field echoOff = Console.class.getDeclaredField("echoOff"); |
| 64 |
echoOff.setAccessible(true); |
| 65 |
echoOff.set(null, res); |
| 66 |
return res; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* A static method which will read from a given InputStream while it turns |
| 71 |
* echo off in the JVM console. Works just as {@link |
| 72 |
* java.io.Console#readPassword}. |
| 73 |
* |
| 74 |
* @param is the InputStream to read from. |
| 75 |
* |
| 76 |
* @exception NoSuchMethodException if Java's own console class have no |
| 77 |
* private method named "echo". |
| 78 |
* @exception NoSuchFieldException if Java's own console class have no |
| 79 |
* private field named "echoOff". |
| 80 |
* @exception InvocationTargetException if Java's own console class have a |
| 81 |
* private method named "echo", but that this method tries to invoke that |
| 82 |
* method erroneously. |
| 83 |
* @exception IllegalAccessException if the access restrictions prohibits |
| 84 |
* {@link #setEcho(boolean)} from modifying and using private methods in |
| 85 |
* Java's own console class. |
| 86 |
* @exception IOException if the given <code>InputStream</code> throws an |
| 87 |
* <code>IOException</code>. |
| 88 |
*/ |
| 89 |
public static char[] readPassword(InputStream is) throws |
| 90 |
NoSuchMethodException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, IOException { |
| 91 |
try { |
| 92 |
synchronized(is) { |
| 93 |
setEcho(false); |
| 94 |
InputStreamReader isr = new InputStreamReader(is); |
| 95 |
char[] buf = new char[32]; |
| 96 |
int len = 0; |
| 97 |
loop: |
| 98 |
while (true) { |
| 99 |
int c = isr.read(); |
| 100 |
switch (c) { |
| 101 |
case -1: |
| 102 |
case '\n': |
| 103 |
break loop; |
| 104 |
case '\r': |
| 105 |
continue; |
| 106 |
} |
| 107 |
buf[len] = (char) c; |
| 108 |
len++; |
| 109 |
if (len == buf.length) { |
| 110 |
char[] fresh = new char[2*len]; |
| 111 |
System.arraycopy(buf, 0, fresh, 0, len); |
| 112 |
buf = fresh; |
| 113 |
} |
| 114 |
} |
| 115 |
char[] out = new char[len]; |
| 116 |
System.arraycopy(buf, 0, out, 0, len); |
| 117 |
return out; |
| 118 |
} |
| 119 |
} |
| 120 |
finally { |
| 121 |
setEcho(true); |
| 122 |
} |
| 123 |
} |
| 124 |
} |