fastJSON源码分析_11_IOUtils工具类浅析(1)

本文分析了fastJSON中的IOUtils类,详细解释了其如何通过预设标识符提升变量名解析效率,并介绍了属性文件加载流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2021SC@SDUSC

 文章简介

本篇文章我们分析有关fastJSON的输出工具类IOUtils.该类帮助fastJSON更加快速地进行输入输出.

源码分析

我们先看该类的静态代码块,这里出现了两个循环

    static {
        for (char c = 0; c < firstIdentifierFlags.length; ++c) {
            if (c >= 'A' && c <= 'Z') {
                firstIdentifierFlags[c] = true;
            } else if (c >= 'a' && c <= 'z') {
                firstIdentifierFlags[c] = true;
            } else if (c == '_' || c == '$') {
                firstIdentifierFlags[c] = true;
            }
        }

        for (char c = 0; c < identifierFlags.length; ++c) {
            if (c >= 'A' && c <= 'Z') {
                identifierFlags[c] = true;
            } else if (c >= 'a' && c <= 'z') {
                identifierFlags[c] = true;
            } else if (c == '_') {
                identifierFlags[c] = true;
            } else if (c >= '0' && c <= '9') {
                identifierFlags[c] = true;
            }
        }

        try {
            loadPropertiesFromFile();
        } catch (Throwable e) {
            //skip
        }
    }

第一个循环是在初始化firstIdentifierflag这个bool数组的值,firstIdentifierflag是一个用来表示Ascll码的识别bool数组一共256个字符,同时表示一个变量的第一个字符的限制,而第二个循环则是对变量名称的其他位的识别,满足java的变量命名规则,然后执行loadPropertiesFromFile方法.该方法应该表示的是加载属性文件。

    public static void loadPropertiesFromFile(){
        InputStream imputStream = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
            public InputStream run() {
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                if (cl != null) {
                    return cl.getResourceAsStream(FASTJSON_PROPERTIES);
                } else {
                    return ClassLoader.getSystemResourceAsStream(FASTJSON_PROPERTIES);
                }
            }
        });
        
        if (null != imputStream) {
            try {
                DEFAULT_PROPERTIES.load(imputStream);
                imputStream.close();
            } catch (java.io.IOException e) {
                // skip
            }
        }
    }

大致 过程就是读取属性文件,然后将文件中的内容加载到属性类当中。

本篇总结

ioUtils使用了空间换时间的方式,进一步提高了fastJSON的速度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值