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))));
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: InputStream
s only really make sense as part of a process that reads them. Storing them en masse is a bad idea because:
InputStream
will almost always end in tears.)FileInputStream
s, they will keep the file open and you may run out of file descriptors.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With