package com.tohi.common.pwdutil;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.*;
/**
 * 文件工具
 */
public class FileUtils {

    /**
     * 把字节转换成文件
     * @param fileData
     * @param fileName
     * @throws IOException
     */
    private static void byteDataConvertToFile(byte[] fileData, String fileName) throws IOException{
        ByteArrayInputStream is = new ByteArrayInputStream(fileData);
        writeToFile(is,fileName);
    }

    /**
     * 把字节转换成文件
     * @param fileBase64Data
     * @param fileName
     * @throws IOException
     */
    private static void base64DataConvertToFile(String fileBase64Data, String fileName) throws IOException{
        byte[] bytes = Base64.decode(fileBase64Data);
        byteDataConvertToFile(bytes,fileName);
    }

    /**
     * 把二进制流转换成文件
     * @param inStream
     * @param fileName
     * @throws IOException
     */
    private static void writeToFile(InputStream inStream, String fileName) throws IOException{
        FileOutputStream outStream = new FileOutputStream(fileName);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }
        outStream.close();
        inStream.close();
    }

    /**
     * 把文件转换成字节
     * @param file
     * @return
     */
    private static byte[] fileConvertToByteData(File file){
        FileInputStream fileInputStream = null;
        byte[] fileData = null;
        try {
            fileData = new byte[(int) file.length()];
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(fileData);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fileData;
    }

    public String fileConvertToStringData(File file,String endoce){
        String result = null;
        if(endoce==null||"".equals(endoce)){
            endoce = "UTF-8";
        }
        byte[] bytes = fileConvertToByteData(file);
        if(bytes!=null){
            try{
                result = new String(bytes, "UTF-8");
            }catch (Exception e){
                e.printStackTrace();
            }
            return result;
        }
        return result;
    }



    private static String fileConvertToBase64Data(File file) throws IOException{
        InputStream in = new FileInputStream(file);
        // 取得文件大小
        long length = file.length();
        // 根据大小创建字节数组
        byte[] bytes = new byte[(int) length];
        // 读取文件内容到字节数组
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
                && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        // 读取完毕的校验
        if (offset < bytes.length) {
            throw new IOException("不能读取文件："+ file.getName());
        }
        in.close();
        String encodedFileString = Base64.encode(bytes);
        //String encodedFileString = java.util.Base64.getEncoder().encode(bytes).toString();
        return encodedFileString;
    }

    public void addFile(String ownerId,String ownerTable,String fileData){

    }

    public static void main(String[] args) throws IOException{
        File file = new File("E:\\file.rar");

        byte[] bytes =  FileUtils.fileConvertToByteData(file);
        //bytes转成文件
        //FileUtils.byteDataConvertToFile(bytes,"E:\\file2.rar");
        System.out.println("字节流:"+bytes);
        System.out.println("转换字符串:"+new String(bytes,"UTF-8"));
        String[] str ={new String(bytes,"UTF-8")};
        System.out.println("封装String数组"+str);
        String fileData = FileUtils.fileConvertToBase64Data(file);
        System.out.println("Base64加密:"+fileData);


        //  byte[] bytes = Base64.decode(s);
        //byteDataConvertToFile(bytes,"D:\\mmo2.txt");
        base64DataConvertToFile(fileData,"D:\\mmo2.txt");
//        String[] str = {bytes.toString()};
//        System.out.println(bytes+"=="+str[0]);

    }
}