Saturday, September 12, 2015

Learning JDK IO Code



http://www.bysocket.com/?p=420
Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);
path.list()为什么会返回String[] filenams的数组呢?怎么不是List呢?
自问自答:这时候,我们应该去看看ArrayList的实现,ArrayList其实是动态的数组实现。动态,动态的弊端就是效率低。此时,返回一个固定的数组,而不是一个灵活的类容器,因为其目录元素是固定的
1、static private FileSystem fs
     FileSystem : 对本地文件系统的抽象
2、String path 文件路径名
3、内联枚举类
     PathStatus 地址是否合法 ENUM类 private static enum PathStatus { INVALID, CHECKED };
4、prefixLength 前缀长度
其实操作的是 FileSystem : 对本地文件系统的抽象,真正操作的是 FileSytem派生类。通过源码Ctrl+T发现如下:Win下操作的是 Win32FileSystem 和WinNTFileSystem类。看来真正通过jvm,native调用系统的File是他们。
File.java

private static final FileSystem fs = DefaultFileSystem.getFileSystem();
class DefaultFileSystem {
    public static FileSystem getFileSystem() {
        return new UnixFileSystem();
    }

}
图解 Java IO : 二、FilenameFilter源码
File.java
    public String[] list(FilenameFilter filter) {
        String names[] = list();
        if ((names == null) || (filter == null)) {
            return names;
        }
        List<String> v = new ArrayList<>();
        for (int i = 0 ; i < names.length ; i++) {
            if (filter.accept(this, names[i])) {
                v.add(names[i]);
            }
        }
        return v.toArray(new String[v.size()]);

    }
public interface FilenameFilter {
    boolean accept(File dir, String name);
}
这是一个策略模式的例子,因为list()实现了基本功能,而按着形式提供了这个策略,完善list()提供服务所需的算法。
FilenameFiler Interface 就是这里的抽象策略角色。其实也可以用抽象类实现。
深度分析:接口要有真正的实现才能算行为模式中真正实现。所以这里使用的是策略模式,涉及到三个角色:
环境(Context)角色
抽象策略(Strategy)角色
具体策略(Context Strategy)角色
但是,装饰器模式也有缺点:在编写程序的时候,它提供了相当多的灵活性(容易混合和匹配属性),同时也增加了代码的复杂性
String[] fs = f.list()
File[] fs = f.listFiles()
String []fs = f.list(FilenameFilter filter);;
File[]fs = f.listFiles(FilenameFilter filter);

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts