package org.fouram.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Base64;

/**
 * Base64 工具类
 * @author Liup
 */
public class Base64Util {
	
	public static String encodeFile(String path) throws Exception {
		File file = new File(path);
		FileInputStream inputFile = new FileInputStream(file);
		byte[] buffer = new byte[(int) file.length()];
		inputFile.read(buffer);
		inputFile.close();
		return Base64.getEncoder().encodeToString(buffer);
	}
	
	public static String encodeString(String str) {
		return Base64.getEncoder().encodeToString(str.getBytes());
	}

	public static void decodeFile(String base64Code, String targetPath) throws Exception {
		File file = new File(targetPath);
		file.getParentFile().mkdirs();
		file.createNewFile();
		byte[] buffer = Base64.getDecoder().decode(base64Code);
		FileOutputStream out = new FileOutputStream(targetPath);
		out.write(buffer);
		out.close();
	}
	
	public static String decodeString(String base64Code) {
		return new String(Base64.getDecoder().decode(base64Code));
	}
	
	public static void main(String[] args) {
		System.out.println(encodeString("APIADMIN@cmccoltdD:12345"));
		System.out.println(encodeString("APIADMIN@cmccoltd:12345"));
	}
}