aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common/util
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2021-10-03 17:55:35 +0100
committerLuck <git@lucko.me>2021-10-03 17:55:35 +0100
commitdfd397d90a98c9edb110dcfdf4098a4350fa15ac (patch)
tree5c34ad1cf855681746082bf319e7c910db4c5bdc /spark-common/src/main/java/me/lucko/spark/common/util
parent89bd36f6cd9859ee8d7f6d9d51620326d678b457 (diff)
downloadspark-dfd397d90a98c9edb110dcfdf4098a4350fa15ac.tar.gz
spark-dfd397d90a98c9edb110dcfdf4098a4350fa15ac.tar.bz2
spark-dfd397d90a98c9edb110dcfdf4098a4350fa15ac.zip
Some tidying up
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/util')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/util/TemporaryFiles.java63
1 files changed, 63 insertions, 0 deletions
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) <luck@lucko.me>
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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<Path> 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<Path> iterator = DELETE_SET.iterator(); iterator.hasNext(); ) {
+ Path path = iterator.next();
+ try {
+ Files.deleteIfExists(path);
+ } catch (IOException e) {
+ // ignore
+ }
+ iterator.remove();
+ }
+ }
+ }
+
+}