package com.qiniu.api.net;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import com.qiniu.api.config.Config;

/**
 * URLEncoding is the alternate base64 encoding defined in RFC 4648. It is
 * typically used in URLs and file names.
 * 
 */
public class EncodeUtils {

	public static byte[] urlsafeEncodeBytes(byte[] src) {
		if (src.length % 3 == 0) {
			return encodeBase64Ex(src);
		}

		byte[] b = encodeBase64Ex(src);
		if (b.length % 4 == 0) {
			return b;
		}

		int pad = 4 - b.length % 4;
		byte[] b2 = new byte[b.length + pad];
		System.arraycopy(b, 0, b2, 0, b.length);
		b2[b.length] = '=';
		if (pad > 1) {
			b2[b.length + 1] = '=';
		}
		return b2;
	}
	
	public static byte[] urlsafeBase64Decode(String encoded){
		byte[] rawbs = toByte(encoded);
		for(int i=0;i<rawbs.length;i++){
			if(rawbs[i] == '_'){
				rawbs[i] = '/';
			}else if(rawbs[i] == '-'){
				rawbs[i] = '+';
			}
		}
		return Base64.decodeBase64(rawbs);
	}
	
	public static String urlsafeEncodeString(byte[] src) {
		return toString(urlsafeEncodeBytes(src));
	}

	public static String urlsafeEncode(String text) {
		return toString(urlsafeEncodeBytes(toByte(text)));
	}

	// replace '/' with '_', '+" with '-'
	private static byte[] encodeBase64Ex(byte[] src) {
		// urlsafe version is not supported in version 1.4 or lower.
		byte[] b64 = Base64.encodeBase64(src);

		for (int i = 0; i < b64.length; i++) {
			if (b64[i] == '/') {
				b64[i] = '_';
			} else if (b64[i] == '+') {
				b64[i] = '-';
			}
		}
		return b64;
	}

	public static String encodeParams(Object params) {
		if (params instanceof String) {
			return (String) params;
		}
		if (params instanceof HashMap<?, ?>) {
			@SuppressWarnings("unchecked")
			Map<String, String> map = (HashMap<String, String>) params;
			ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
			for (Entry<String, String> entry : map.entrySet()) {
				list.add(new BasicNameValuePair(entry.getKey(), entry
						.getValue()));
			}
			return URLEncodedUtils.format(list, Config.CHARSET);
		}
		return null;
	}
	
	public static byte[] toByte(String s){
		try {
			return s.getBytes(Config.CHARSET);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String toString(byte[] bs){
		try {
			return new String(bs, Config.CHARSET);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}
}

@@ -521,17 +521,16 @@
 ames.%0A *
- 
 %0A */%0Apub
@@ -613,38 +613,8 @@
 ) %7B%0A
-%09%09if (src.length %25 3 == 0) %7B%0A%09
 %09%09re
@@ -643,293 +643,12 @@
 c);%0A
-%09
 %09%7D%0A%0A
-%09%09byte%5B%5D b = encodeBase64Ex(src);%0A%09%09if (b.length %25 4 == 0) %7B%0A%09%09%09return b;%0A%09%09%7D%0A%0A%09%09int pad = 4 - b.length %25 4;%0A%09%09byte%5B%5D b2 = new byte%5Bb.length + pad%5D;%0A%09%09System.arraycopy(b, 0, b2, 0, b.length);%0A%09%09b2%5Bb.length%5D = '=';%0A%09%09if (pad %3E 1) %7B%0A%09%09%09b2%5Bb.length + 1%5D = '=';%0A%09%09%7D%0A%09%09return b2;%0A%09%7D%0A%09%0A
 %09pub
@@ -910,25 +910,24 @@
 (rawbs);%0A%09%7D%0A
-%09
 %0A%09public sta
@@ -2018,25 +2018,24 @@
 rn null;%0A%09%7D%0A
-%09
 %0A%09public sta
@@ -2195,17 +2195,16 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%09public

remove useless base64 code