package com.qiniu.android.storage.persistent;

import com.qiniu.android.http.custom.DnsCacheInfo;
import com.qiniu.android.storage.Recorder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by jemy on 2019/9/17.
 */

public class DnsCacheFile implements Recorder {

    public String directory;
    public File f;

    public DnsCacheFile(String directory) throws IOException {
        this.directory = directory;
        f = new File(directory);
        if (!f.isDirectory() || !f.exists()) {
            boolean r = f.mkdirs();
            if (!r) {
                throw new IOException("mkdir failed");
            }
            return;
        }
        if (!f.isDirectory()) {
            throw new IOException("does not mkdir");
        }
    }

    /**
     * 设置DNS缓存
     *
     * @param key  缓存文件明
     * @param data 缓存数据
     */
    @Override
    public void set(String key, byte[] data) {
        File[] fs = f.listFiles();
        if (fs == null) return;
        if (fs.length > 0) {
            for (int i = 0; i < fs.length; i++) {
                del(fs[i].getName());
            }
        }

        File f = new File(directory, key);
        FileOutputStream fo = null;
        try {
            fo = new FileOutputStream(f);
            fo.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (fo != null) {
            try {
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取缓存
     *
     * @param key 缓存文件名
     */
    @Override
    public byte[] get(String key) {
        File f = new File(directory, key);
        FileInputStream fi = null;
        byte[] data = null;
        int read = 0;
        try {
            data = new byte[(int) f.length()];
            fi = new FileInputStream(f);
            read = fi.read(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (fi != null) {
            try {
                fi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (read == 0) {
            return null;
        }
        return data;
    }

    //f.delete()=false时才会有fs.length>1的情况
    public String getFileName() {
        return "dnsCache";
    }

    @Override
    public void del(String key) {
        if (key != null) {
            File f = new File(directory, key);
            f.delete();
        }
    }
}

@@ -518,24 +518,25 @@
 directory);%0A
+%0A
         if (
@@ -555,89 +555,12 @@
 ry()
- %7C%7C !f.exists()
 ) %7B%0A
-            boolean r = f.mkdirs();%0A            if (!r) %7B%0A    
     
@@ -594,20 +594,22 @@
 on(%22
-mkdir failed
+does not mkdir
 %22);%0A
@@ -620,76 +620,100 @@
     
+%7D%0A%0A
     
-%7D%0A            return;%0A   
+    if (!f.exists()) %7B%0A            boolean r = f.mkdirs();%0A
      
-%7D%0A
        
- 
 if (!
-f.isDirectory()) %7B%0A
+r) %7B%0A    
     
@@ -743,33 +743,45 @@
 eption(%22
-does not mkdir%22);
+mkdir failed%22);%0A            %7D
 %0A       

change dns cache file create logic