0

I have a try-with-resources block:

try (InputStream stream = file.getValueAs(InputStream.class); 
    InputStream newstream = 
        new LimitedSizeInputStream(stream, MAX_FILE_SIZE_IN_BYTES))
{
   // some logic... btw I defined LimitedSizeInputStream class with a close method
}

and I'm just wondering if I can change it to

try (InputStream newstream = 
        new LimitedSizeInputStream(file.getValueAs(InputStream.class), MAX_FILE_SIZE_IN_BYTES))
{
   // some logic... btw I defined LimitedSizeInputStream class with a close method
}

Will it behave the same way and close both input streams? Or will there be a difference?

ronapple14
  • 93
  • 2
  • 10
  • 1
    In the second example, the underlying InputStream would be closed iff LimitedSizeInputStream implements AutoCloseable, and then in its close method, called close on the underlying InputStream. – nickb May 12 '20 at 02:39
  • Or if it extends `FilterInputStream` correctly. – user207421 May 12 '20 at 05:22

0 Answers0