private static String encodeAuthPlain(String username, char[] password) {
        // PLAIN mechanism encoding - NUL + username + NUL + password
        int size = username.length() + password.length + 2;
        ByteBuffer buffer = ByteBuffer.allocate(size);
        buffer.put((byte)0);
        buffer.put(username.getBytes());
        buffer.put((byte)0);
        buffer.put(new String(password).getBytes());
        
        buffer.flip();
        
        // Nulling out the password for security. 
        Arrays.fill(password, '\0');

        int len = buffer.remaining();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < len; i++)
        {
            builder.append((char)(buffer.get() & 0xff)); 

        }
        String s = builder.toString();
        return s;

    }
private static String encodeAuthPlain ( String username , char [ ] password ) { int size = username . length ( ) + password . length + 2 ; ByteBuffer buffer = ByteBuffer . allocate ( size ) ; buffer . put ( ( byte ) 0 ) ; buffer . put ( username . getBytes ( ) ) ; buffer . put ( ( byte ) 0 ) ; buffer . put ( new String ( password ) . getBytes ( ) ) ; buffer . flip ( ) ; Arrays . fill ( password , '\0' ) ; int len = buffer . remaining ( ) ; StringBuilder builder = new StringBuilder ( ) ; for ( int i = 0 ; i < len ; i ++ ) { builder . append ( ( char ) ( buffer . get ( ) & 0xff ) ) ; } String s = builder . toString ( ) ; return s ; }
-------------------- Private Encoding Methods -------------------------