From 49ff45b7612f3a0d89c931ee218c80400eedbec2 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Thu, 3 Nov 2016 16:21:13 +0100 Subject: Properly close open files(persistence) Change-Id: I0b6a01c7360e056059b0c8ed1821a0fdddc59308 Signed-off-by: Maros Marsalek --- .../java/io/fd/honeycomb/data/impl/PersistingDataTreeAdapter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'infra/data-impl') diff --git a/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/PersistingDataTreeAdapter.java b/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/PersistingDataTreeAdapter.java index 356ca51c1..3c356c256 100644 --- a/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/PersistingDataTreeAdapter.java +++ b/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/PersistingDataTreeAdapter.java @@ -24,6 +24,7 @@ import com.google.common.base.MoreObjects; import com.google.common.base.Optional; import io.fd.honeycomb.translate.util.JsonUtils; import java.io.IOException; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; @@ -109,11 +110,10 @@ public class PersistingDataTreeAdapter implements DataTree { void persistCurrentData(final Optional> currentRoot) { if (currentRoot.isPresent()) { - try { + try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING)) { LOG.trace("Persisting current data: {} into: {}", currentRoot.get(), path); - JsonUtils.writeJsonRoot(currentRoot.get(), schemaServiceDependency.getGlobalContext(), - Files.newOutputStream(path, StandardOpenOption.CREATE, - StandardOpenOption.TRUNCATE_EXISTING)); + JsonUtils.writeJsonRoot(currentRoot.get(), schemaServiceDependency.getGlobalContext(), outputStream); LOG.trace("Data persisted successfully in {}", path); } catch (IOException e) { throw new IllegalStateException("Unable to persist current data", e); -- cgit 1.2.3-korg