-2

This question specifically refers to Java version of Input Streams but the question could probably apply to other languages.

Does an Input Stream act as a pointer to a file until a read call is made? In a toy program I am working with I am passing a Input Stream through a few different functions and would like to understand if I am passing the entire file as bytes loaded into RAM, a specific amount of bytes that are loaded into a buffer which is in RAM, or just a pointer to the file that will only load bytes into RAM upon a read call.

Does the type of Input Stream I use have different implementations (i.e. ByteArrayInputStream vs BufferedInputStream)?

Sam
  • 93
  • 10
  • 1
    What does the javadoc state? – Sotirios Delimanolis Jul 15 '15 at 19:24
  • An `InputStream` is just something from which you can read bytes (one at a time, several at a time, etc); it may, or may not, be tied to a filesystem resource. You said it yourself: there is `ByteArrayInputStream`; this is not tied to a filesystem resource, is it? The only thing that really matters is that you open, use, and close the resource properly. – fge Jul 15 '15 at 19:30
  • Java has no pointers, only references. Therefore: no, an unread `InputStream` is not a pointer to a file. – Turing85 Jul 15 '15 at 19:35

1 Answers1

0

java.io.File is probably closer to the idea of a pointer to a File. java.io.FileInputStream and other InputStreams are more like transformers/interleaves to a serialized representation of whatever is being read.

In terms of are you passing the whole file around, the answer is no. In Java, all objects are passed by pointer... Just 4 bytes. So, even if you did load the whole file into a byte[], you can pass that byte[] to other methods and it will have virtually no impact on memory... and no more impact than passing a small byte[].

BufferedInputStreams and BufferedReaders are basically only ueful for doing inefficient reading. If you're trying to read a whole file or a whole URL's content before processing, buffering won't matter. If, on the other hand, you are reading a file line by line, that's when the Buffer is useful.

ControlAltDel
  • 28,815
  • 6
  • 42
  • 68
  • Java is neither pass-by-refernce nor pass-by-pointer. [It is pass-by-value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Turing85 Jul 15 '15 at 19:40
  • @Turing85 yes, I am aware. But when passing objects around, the value is the pointer to the object, not the entire object like you can do in C/C++ – ControlAltDel Jul 15 '15 at 19:43