如果我将 InputStream 的对象保存到内存中,是否意味着我将整个文件存储到内存中?

If I keep an objects of InputStream into the memory then does it mean I am storing the whole file into the memory?

我有一个 class Sample 有两个构造函数。一个接受 File 的对象,而另一个接受 InputStream 的对象。

package org.xyz.core;

    import java.io.File;
    import java.io.InputStream;

    /**
     * Created by Ashish Pancholi on 26-03-2016.
     */
    public class Sample {

        File file;

        public Sample(File file){
            this.file=file;
        }

        public Sample(InputStream inputStream){
            this.file = createFileFromInputStream(inputStream);
        }
    }

我正在使用 LinkedBlockingQueue,它使用 Sample 对象并且深度为 10000;

BlockingQueue<String> queue   = new LinkedBlockingQueue<Sample>(10000);

这里假设两种情况:

案例 A: 我通过传递 InputStream 作为参数来初始化 Sample class 的许多实例,然后我推送了所有这些对象进入LinkedBlockingQueue

案例 B: 我通过传递 File 对象作为参数来初始化 Sample class 的许多实例,然后我推送了所有这些对象进入LinkedBlockingQueue

在什么情况下我的程序会占用更多内存?如果我将 InputStream 的对象保存到内存中,是否意味着我将整个文件存储到内存中?如果我有这么多大文件怎么办?

更新:

请注意:我正在通过这种方式创建 InputStream

InputStream is = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))));

视情况而定。

一个InputStream可以是缓冲的或非缓冲的,它可以在内部存储整个文件或什么都不存储,它可以自由地做所有这些。甚至可能有与之关联的原生资源。

虽然这种模式有一个更根本的问题:InputStreams 只有作为读取它们的过程的一部分才真正有意义。大量存储它们不是一个好主意,因为:

  1. 它们不是线程安全的。 (多线程阅读 InputStream 几乎总是以泪流满面。)
  2. 如果我们谈论 FileInputStreams,他们将保持文件打开,您可能 运行 没有文件描述符。