The method getResourceAsStream
in Class
and ClassLoader
return an InputStream
. Does this InputStream
actually load everything upon the call to getResourceAsStream
or does it simply point to some system that loads the resource when parts of it are requested?

- 337
- 4
- 12
-
It's a stream of data. It will be loaded as needed, though it will very likely be loaded in blocks. That's called buffering. The entire resource is not pre-loaded *(unless smaller than buffer size)*. – Andreas Jul 30 '17 at 01:22
2 Answers
When does
getResourceAsStream
actually load the resource?
The method just opens a stream for reading the resource. It doesn't "load" it".
The actual behavior of the stream will be implementation (e.g. classloader) specific, but there is nothing in the generic ClassLoader
API documentation to suggest1 that a classloader should preread the resource into memory.
If you want a classloader to do that, you can write a custom classloader.
If you want to be sure that a specific classloader does not do that ... read the source code. Then if necessary use a different classloader that doesn't behave that way, or implement your own2.
1 - Not even the method or class name, IMO. A "classloader" loads classes, and I read the word "load" as having a specific technical meaning that is not applicable to other things.
2 - There might be obscure cases where that is impossible. For example, a classloader that reads from a device that requires the data to be read in a single I/O operation.

- 698,415
- 94
- 811
- 1,216