package com.cm.retrofit2.converter.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import okhttp3.ResponseBody;

/**
 * Created by Cmad on 2016/5/3.
 */
public class FileUtils {

    /**
     * 将文件写入本地
     * @param body http响应体
     * @param path 保存路径
     * @return 保存file
     */
    public static File writeResponseBodyToDisk(ResponseBody body, String path) {

        File futureStudioIconFile = null;
        try {

            futureStudioIconFile = new File(path);

            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {
                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                }

                outputStream.flush();

                return futureStudioIconFile;
            } catch (IOException e) {
                return futureStudioIconFile;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return futureStudioIconFile;
        }
    }
}

@@ -470,32 +470,20 @@
    File 
-futureStudioIcon
+save
 File = n
@@ -514,32 +514,20 @@
         
-futureStudioIcon
+save
 File = n
@@ -538,24 +538,59 @@
 ile(path);%0A%0A
+            createDirs(saveFile);%0A%0A
             
@@ -840,32 +840,20 @@
 tStream(
-futureStudioIcon
+save
 File);%0A%0A
@@ -1180,32 +1180,20 @@
  return 
-futureStudioIcon
+save
 File;%0A  
@@ -1251,32 +1251,20 @@
  return 
-futureStudioIcon
+save
 File;%0A  
@@ -1563,28 +1563,339 @@
 urn 
-futureStudioIconFile
+saveFile;%0A        %7D%0A    %7D%0A%0A    public static void createDirs(File file)%7B%0A        if(file != null)%7B%0A            String dir = file.getParent();%0A            createDirs(dir);%0A        %7D%0A    %7D%0A%0A    public static void createDirs(String dirPath)%7B%0A        File file = new File(dirPath);%0A        if(!file.exists())%7B%0A            file.mkdirs()
 ;%0A  

create save dir