I created a class that extends InputStream so that I can keep count of the number of bytes being read and throw an exception if it exceeds a max limit that I define.
Her
The try-with-resources only closes the declared resource. So will only close metadataStream.
metadataStream
You should implement the close method in LimitedSizeInputStream to close the original stream.
close
LimitedSizeInputStream
@Override public void close() throws IOException { original.close(); }