package com.filemanager.occupancy;

import android.os.StatFs;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by wuhao on 2015/6/15.
 */
public class FileTreeNode<T> implements Iterable<T> {

    //data use for file
    public File data;
    //the file size
    public long size = 0;

    FileTreeNode<T> parent = null;
    ArrayList<FileTreeNode<T>> children;

    public FileTreeNode(File data) {
        this.data = data;
        this.children = new ArrayList<FileTreeNode<T>>();
        initSize();
    }

    private void initSize() {
        if (data.isFile()) {
            size = data.length();
        } else {
            StatFs fs = new StatFs(data.getPath());
            size = fs.getBlockSize();
        }
    }

    public FileTreeNode<T> addChild(File child) {
        FileTreeNode<T> childNode = new FileTreeNode<T>(child);
        childNode.parent = this;
        size = size + childNode.size;
        this.children.add(childNode);
        return childNode;
    }

    @Override
    public Iterator<T> iterator() {
        //add something
        return null;
    }

    public final void refresh() {
        initSize();
        Iterator iterator = children.iterator();
        while (iterator.hasNext()) {
            FileTreeNode<T> tmp = (FileTreeNode<T>) iterator.next();
            size += tmp.size;
        }
    }
}


@@ -33,34 +33,8 @@
 y;%0A%0A
-import android.os.StatFs;%0A
 %0Aimp
@@ -204,16 +204,77 @@
 e%3CT%3E %7B%0A%0A
+    public static final int DEFAULT_FILE_BLOCK_SIZE = 4096;%0A%0A
     //da
@@ -609,24 +609,42 @@
 nitSize() %7B%0A
+        try %7B%0A    
         if (
@@ -656,20 +656,42 @@
 isFile()
-) %7B%0A
+ && data.canRead()) %7B%0A    
         
@@ -728,14 +728,42 @@
     
+    
 %7D else
+ if (data.isDirectory())
  %7B%0A 
@@ -777,47 +777,89 @@
     
+ 
-StatFs fs = new StatFs(data.getPath());
+   size = DEFAULT_FILE_BLOCK_SIZE;%0A            %7D%0A        %7D catch (Exception e) %7B
 %0A   
@@ -878,25 +878,9 @@
 e = 
-fs.getBlockSize()
+0
 ;%0A  

修复获得文件系统分块大小 bug