Class PasswordReader


  • @ThreadSafety(level=NOT_THREADSAFE)
    public final class PasswordReader
    extends java.lang.Object
    This class provides a mechanism for reading a password from the command line in a way that attempts to prevent it from being displayed. If it is available (i.e., Java SE 6 or later), the java.io.Console.readPassword method will be used to accomplish this. For Java SE 5 clients, a more primitive approach must be taken, which requires flooding standard output with backspace characters using a high-priority thread. This has only a limited effectiveness, but it is the best option available for older Java versions.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static byte[] readPassword()
      Reads a password from the console as a byte array.
      static char[] readPasswordChars()
      Reads a password from the console as a character array.
      void run()
      Deprecated.
      This method is no longer used.
      static void setTestReader​(java.io.BufferedReader reader)
      Specifies the input stream from which to read the password.
      static void setTestReaderLines​(java.lang.String... lines)
      Specifies the lines that should be used as input when reading the password.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • readPasswordChars

        @NotNull
        public static char[] readPasswordChars()
                                        throws LDAPException
        Reads a password from the console as a character array.
        Returns:
        The characters that comprise the password that was read.
        Throws:
        LDAPException - If a problem is encountered while trying to read the password.
      • readPassword

        @NotNull
        public static byte[] readPassword()
                                   throws LDAPException
        Reads a password from the console as a byte array.
        Returns:
        The characters that comprise the password that was read.
        Throws:
        LDAPException - If a problem is encountered while trying to read the password.
      • run

        @Deprecated
        public void run()
        Deprecated.
        This method is no longer used.
        This is a legacy method that now does nothing. It was required by a former version of this class when older versions of Java were still supported, and is retained only for the purpose of API backward compatibility.
      • setTestReaderLines

        @InternalUseOnly
        public static void setTestReaderLines​(@NotNull
                                              java.lang.String... lines)
        Specifies the lines that should be used as input when reading the password. This should only be set when running unit tests, and the setTestReader(BufferedReader) method should be called with a value of null before the end of the test to ensure that the password reader is reverted back to its normal behavior.
        Parameters:
        lines - The lines of input that should be provided to the password reader instead of actually obtaining them interactively. It must not be null but may be empty.
      • setTestReader

        @InternalUseOnly
        public static void setTestReader​(@Nullable
                                         java.io.BufferedReader reader)
        Specifies the input stream from which to read the password. This should only be set when running unit tests, and this method should be called again with a value of null before the end of the test to ensure that the password reader is reverted back to its normal behavior.
        Parameters:
        reader - The input stream from which to read the password. It may be null to obtain the password from the normal means.