From dfd397d90a98c9edb110dcfdf4098a4350fa15ac Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 3 Oct 2021 17:55:35 +0100 Subject: Some tidying up --- .../me/lucko/spark/common/util/TemporaryFiles.java | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/TemporaryFiles.java (limited to 'spark-common/src/main/java/me/lucko/spark/common/util') diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/TemporaryFiles.java b/spark-common/src/main/java/me/lucko/spark/common/util/TemporaryFiles.java new file mode 100644 index 0000000..8a4a621 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/TemporaryFiles.java @@ -0,0 +1,63 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.util; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +/** + * Utility for handling temporary files. + */ +public final class TemporaryFiles { + private TemporaryFiles() {} + + private static final Set DELETE_SET = Collections.synchronizedSet(new HashSet<>()); + + public static Path create(String prefix, String suffix) throws IOException { + return register(Files.createTempFile(prefix, suffix)); + } + + public static Path register(Path path) { + path.toFile().deleteOnExit(); + DELETE_SET.add(path); + return path; + } + + public static void deleteTemporaryFiles() { + synchronized (DELETE_SET) { + for (Iterator iterator = DELETE_SET.iterator(); iterator.hasNext(); ) { + Path path = iterator.next(); + try { + Files.deleteIfExists(path); + } catch (IOException e) { + // ignore + } + iterator.remove(); + } + } + } + +} -- cgit