/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

import org.springframework.util.Assert;

/**
 * @author 田平 create 2019年6月14日下午3:12:11
 */
public class Base64Util {
	
	private static final String DEFAULT_CHARSET_NAME = "UTF-8";// 默认编码

	public static void main(String[] args) {
		System.out.println(encodeMime("中国"));
		System.out.println(decodeMime("5Lit5Zu9"));
	}

	public static String decodeMime(String content) {
		Assert.hasText(content, "content not allow null empty blank!");
		String result = "";
		try {
			result = new String(Base64.getMimeDecoder().decode(content), DEFAULT_CHARSET_NAME);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String encodeMime(String content) {
		Assert.hasText(content, "content not allow null empty blank!");
		String result = "";
		try {
			result = new String(Base64.getMimeEncoder().encode(content.getBytes(DEFAULT_CHARSET_NAME)));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String decode(String content) {
		Assert.hasText(content, "content not allow null empty blank!");
		String result = "";
		try {
			result = new String(Base64.getDecoder().decode(content), DEFAULT_CHARSET_NAME);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String encode(String content) {
		Assert.hasText(content, "content not allow null empty blank!");
		String result = "";
		try {
			result = new String(Base64.getEncoder().encode(content.getBytes(DEFAULT_CHARSET_NAME)));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String decodeUrl(String content) {
		Assert.hasText(content, "content not allow null empty blank!");
		String result = "";
		try {
			result = new String(Base64.getUrlDecoder().decode(content), DEFAULT_CHARSET_NAME);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String encodeUrl(String content) {
		Assert.hasText(content, "content not allow null empty blank!");
		String result = "";
		try {
			result = new String(Base64.getUrlEncoder().encode(content.getBytes(DEFAULT_CHARSET_NAME)));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

}
