Use try-with-resources where appropriate

This is more succinct and idiomatic Java style.
Removed without deprecation since this is a @Beta API.