title: Java Try-with-resource storing input stream in Map
body: <p>In my API (Spring boot) I have an endpoint where users can upload multiple file at once. The endpoint takes as input a list of <code>MultipartFile</code>.</p>
  
  <p>I wish not to directly pass this <code>MultipartFile</code> object to the service directly so I loop through each <code>MultipartFile</code> and create a simple map that stored the filename and its <code>InputStream</code>.</p>
  
  <p>Like this:</p>
  
  <pre><code>for (MultipartFile file : files) {       
        try (InputStream is = multipartFile.getInputStream()) {
          filesMap.put(file.getOriginalFilename(), is);
        }
      }
  service.uploadFiles(filesMap)
  </code></pre>
  
  <p>My understanding for Java streams and streams closing is quite limited. 
  I thought that <code>try-with-resources</code> automatically closes the <code>InputStream</code> once the code reached the end of the try block.</p>
  
  <p>In the above code when does exactly the the <code>multipartFile.getInputStream()</code> gets closed? </p>
  
  <p>The fact that I'm storing the stream in a map will that cause a memory leak?</p>