Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.net.URLEncoder;

public class Main {
    /**
     * Encode user and password for URL.<br>
     * <br>
     * If user is empty you get an empty string.<br>
     * If password is empty you get {@code user@}.
     * 
     * @param user
     *            user
     * @param password
     *            password
     * 
     * @return URL encoded string with format {@code user:password@}
     */
    public static String encodeAuth(String user, String password) {
        StringBuilder auth = new StringBuilder();

        if (user != null && !user.trim().equals("")) {
            auth.append(URLEncoder.encode(user));

            if (password != null && !password.trim().equals("")) {
                auth.append(":");
                auth.append(URLEncoder.encode(password));
            }

            auth.append("@");
        }

        return auth.toString();
    }
}