Use try-with-resources to avoid accidentaly leaking unclosed objects.

Try-with-resources guarantees that objects will be closed even if an
exception happens after their creation. It also handles nulls and avoids
throwing NPE.

Test: built and launched Cuttlefish.
Change-Id: I80e767d1fbbca25a724bfe94e1752f88891a6bb0
1 file changed