Commit 83850daa authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Make sure that the DirectoryStream gets closed.

As the docs say, "If timely disposal of file system resources is
required, the try-with-resources construct should be used to ensure
that the stream's close method is invoked after the stream operations
are completed."

Turns out that without closing the stream, the JVM runs out of memory
pretty quickly. Doing this is not optional but mandatory.
parent 7439fa93
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.stream.Stream;

public class PersistenceUtils {

@@ -132,10 +133,13 @@ public class PersistenceUtils {
      @Override
      public FileVisitResult postVisitDirectory(Path dir, IOException exc)
          throws IOException {
        if (!pathToClean.equals(dir)
            && !Files.list(dir).findFirst().isPresent()) {
        if (!pathToClean.equals(dir)) {
          try (Stream<Path> files = Files.list(dir)) {
            if (!files.findFirst().isPresent()) {
              Files.delete(dir);
            }
          }
        }
        return FileVisitResult.CONTINUE;
      }
    };