package market.Com.pb.fdfs;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

import sun.misc.BASE64Decoder;

public class PbCode {
	
	private static final String temp_path=PbCode.class.getResource("/").getPath()+"fdfs/fdfs_TempDir.properties";
	public static HashMap<String,String> SvrTempDir(){
		//String strpath="";
		HashMap<String,String> hm=new HashMap<String,String>();
		Properties prop = new Properties();
		try{
			//读取属性文件a.properties
			InputStream in =new FileInputStream(temp_path);
			prop.load(in);     ///加载属性列表
			Iterator<String> it=prop.stringPropertyNames().iterator();
			while(it.hasNext()){
				String key=it.next();
				hm.put(key,prop.getProperty(key));
				//strpath=prop.getProperty(key);
				//System.out.println(key+":"+prop.getProperty(key));
			}
			in.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return hm;
	}
	
	/**
	 * base64加密
	 * @param str
	 * @return
	 */
	public static String EncryptByBase64(String str){
		if (str == null) return null; 
		return (new sun.misc.BASE64Encoder()).encode(str.getBytes());
	}
	
	/**
	 * base64解密
	 * @param str
	 * @return
	 */
	public static String DecryptFromBase64(String str){
		if (str == null) return null; 
		BASE64Decoder decoder = new BASE64Decoder(); 
		try { 
			byte[] b = decoder.decodeBuffer(str); 
			return new String(b); 
		} catch (Exception e) { 
		    return null; 
		} 
	}
	
	public static byte[] decodeBASE64(String str) throws Exception {
		if (str != null) {
			return new sun.misc.BASE64Decoder().decodeBuffer(str);
		}
		return null;
	}
	
	/**
	 * 随机数生成
	 * */
	public static String RandomCreate(int i){
		String str="";
		for(int k=0;k<i;k++){
			str+=(int)(Math.random()*10);//[1-10)随机数 即0-9
		}
		return str;
	}

}
