/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.net;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

public class NetUtil {
    static public String urlEncodeUTF8(String s) {
        try {
            return URLEncoder.encode(s, "UTF-8");
        } 
        catch (UnsupportedEncodingException e) {
            // NA
        }
        
        return null;
    }
    
    static public String urlEncodeUTF8(Map<String,String> map) {
        StringBuilder sb = new StringBuilder();
        
        for (Map.Entry<String,String> entry : map.entrySet()) {
            if (sb.length() > 0) 
                sb.append("&");
            
            sb.append(urlEncodeUTF8(entry.getKey()) + "=" + urlEncodeUTF8(entry.getValue()));
        }
        
        return sb.toString();       
    }
}
