code: public static final char[] getPassword(InputStream in, String prompt)
              throws IOException {
          if (in == null) {
              throw new InvalidArgument("in is null");
          }
  
          char[] pwd;
          if (in.equals(System.in)) {
              System.out.print(prompt);
  
              Console console = System.console();
  
              if (console == null) {
                  throw new IOException(
                          "Cannot obtain the console with which to read the password from.");
              }
  
              pwd = console.readPassword();
          } else {
              BufferedReader pwdReader =
                      new BufferedReader(new InputStreamReader(in));
              String buffer = pwdReader.readLine();
              if (buffer == null) return null;
              pwd = buffer.toCharArray();
              pwdReader.close();
          }
  
          return pwd;
      }
docs: Retrieve a password in the {@link InputStream} with an optional
  {@code prompt} message if reading from {@link System#in}.
  
  @param in {@link InputStream}, the input stream to retrieve the
  password from, which cannot be null
  @param prompt {@link String}, the message to prompt, which will
  only be used if {@link InputStream} equals {@link System#in}.
  @return {@code char[]}, the password characters
  @throws IOException - Thrown if an io error occurred reading the
  password from the {@link InputStream}.