Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a class Sample which has two constructor. One takes an object of File whereas another takes an 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);
        }
    }

And I am using LinkedBlockingQueue that consumes an object of Sample and has depth of 10000;

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

Let's assume two cases here:

Case A: I initialize so many instances of Sample class by passing InputStream as an arguments and I pushed all these objects into the LinkedBlockingQueue.

Case B: I initialize so many instances of Sample class by passing File object as an arguments and I pushed all these objects into the LinkedBlockingQueue.

In which case my program will take more memory? If I keep an objects of InputStream into the memory then does it mean I am storing the whole file into the memory? What if I have so many large files?

Updated:

Please Note: I am creating the InputStream by this way:

InputStream is = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))));
like image 502
Ashish Pancholi Avatar asked Oct 19 '22 12:10

Ashish Pancholi


1 Answers

It depends.

An InputStream can be buffered or non-buffered, it could store the entire file internally or nothing, it's free to do all of that. There may even be native resources associated with them.

There is a more fundamental problem with this pattern though: InputStreams only really make sense as part of a process that reads them. Storing them en masse is a bad idea because:

  1. They aren't thread-safe. (Multiple threads reading from an InputStream will almost always end in tears.)
  2. If we're talking about FileInputStreams, they will keep the file open and you may run out of file descriptors.
like image 57
biziclop Avatar answered Nov 01 '22 18:11

biziclop