Why does "loadFont" not close the input stream? Should I close it?

Looking at the Font#loadFont , I came across this remark:

This method does not close the input stream.

Unfortunately, this is not explained or expanded. So my question is:

  • What are the possible reasons the API will not close the input stream? Perhaps you would like to reuse the stream? I mainly use this method as follows:

     Font.loadFont(getClass().getResourceAsStream("path/to/font"), 13.0); 

    to make sure the font is available for my application, so I never reuse the input stream, and I can't think of the reason I would like.

  • Do I have to close the input stream myself? Should I expect any problems if I do not close the input stream? I used to have problems with the font loaded so that some of the labels configured with this font started showing squares, while others (on the same scene!) Worked fine. Could this be due to not closing the input stream?
+5
source share
1 answer

The documentation for each API, including limited or external resources (for example, file descriptors or streams), will make it clear whose responsibility it is to clean up (in this case, close the stream). This is sometimes called "property."

In this case, the documentation states that the loadFont method loadFont not accept ownership of the stream. Therefore, it still belongs to you: your responsibility is to close the flow.

The easiest way to do this is with the try-with-resources statement.

+1
source

All Articles