JAR资源输入流未读取请求的大小

时间:2018-06-21 20:09:20

标签: java

我运行以下代码:

public static void main(String[] args) {
    InputStream stream = ClassLoader.getSystemResourceAsStream("resources/fonts/calibri.texture");
    try {
        int n = 458343;
        byte[] a = new byte[n];
        int i;
        do {
            i = stream.read(a);
            System.out.println("request: " + n + ", read: " + i);
        } while(i != -1);
    } catch (IOException e) {}
}

在Netbeans中,输出为:

request: 458343, read: 458343
request: 458343, read: -1

使用命令java -jar my_jar.jar是:

request: 458343, read: 50563
request: 458343, read: 47392
request: 458343, read: 52704
request: 458343, read: 48790
request: 458343, read: 48714
request: 458343, read: 48903
request: 458343, read: 63921
request: 458343, read: 47532
request: 458343, read: 49824
request: 458343, read: -1

两个大小的总和为458343,但是为什么JAR内部资源的InputStream会有这种行为?

1 个答案:

答案 0 :(得分:2)

需要解压缩jar中的资源以使用它,这可能是为什么资源比更有效的直接文件系统调用更小的块返回的原因。块大小在这里也可能是一个因素。