Package net.neoforged.neoforge.common
Class IOUtilities
java.lang.Object
net.neoforged.neoforge.common.IOUtilities
Contains helpers for performing file I/O in a resilient manner.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Declares an interface which is functionally equivalent toConsumer
, except supports the ability to throw IOExceptions that may occur. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.slf4j.Logger
private static final OpenOption[]
private static CompletableFuture
<Void> private static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
atomicWrite
(Path targetPath, IOUtilities.WriteCallback writeCallback) Writes data to the given path "atomically", such that a crash will not leave the file containing corrupted or otherwise half-written data.static void
cleanupTempFiles
(Path targetPath, @Nullable String prefix) Deprecated, for removal: This API element is subject to removal in a future version.This method has been removed in 1.21.5private static BiPredicate
<Path, BasicFileAttributes> createPredicate
(@Nullable String prefix) static void
tryCleanupTempFiles
(Path targetPath, @Nullable String prefix) Tries to clean up any temporary files that may have been left over from interrupted calls toatomicWrite(Path, WriteCallback)
.tryListTempFiles
(Path targetPath, @Nullable String prefix) static void
static void
withIOWorker
(Runnable task) static void
writeNbt
(CompoundTag tag, Path path) Behaves much the same asNbtIo.write(CompoundTag, Path)
, but usesatomicWrite(Path, WriteCallback)
behind the scenes to ensure the data is stored resiliently.static void
writeNbtCompressed
(CompoundTag tag, Path path) Behaves much the same asNbtIo.writeCompressed(CompoundTag, Path)
, but usesatomicWrite(Path, WriteCallback)
behind the scenes to ensure the data is stored resiliently.
-
Field Details
-
TEMP_FILE_SUFFIX
- See Also:
-
OPEN_OPTIONS
-
LOGGER
private static final org.slf4j.Logger LOGGER -
saveDataTasks
-
-
Constructor Details
-
IOUtilities
private IOUtilities()
-
-
Method Details
-
tryCleanupTempFiles
Tries to clean up any temporary files that may have been left over from interrupted calls toatomicWrite(Path, WriteCallback)
.Failures to find or remove the temporary files are logged instead of thrown.
- Parameters:
targetPath
- The target path to clean up temporary files in.prefix
- The prefix of temporary files to clean up, or null if all temporary files should be removed.
-
tryListTempFiles
-
cleanupTempFiles
@Deprecated(forRemoval=true) public static void cleanupTempFiles(Path targetPath, @Nullable @Nullable String prefix) throws IOException Deprecated, for removal: This API element is subject to removal in a future version.This method has been removed in 1.21.5Cleans up any temporary files that may have been left over from interrupted calls toatomicWrite(Path, WriteCallback)
.- Parameters:
targetPath
- The target path to clean up temporary files in.prefix
- The prefix of temporary files to clean up, or null if all temporary files should be removed.- Throws:
IOException
- if an I/O error occurs during deletion.
-
createPredicate
private static BiPredicate<Path,BasicFileAttributes> createPredicate(@Nullable @Nullable String prefix) -
writeNbtCompressed
Behaves much the same asNbtIo.writeCompressed(CompoundTag, Path)
, but usesatomicWrite(Path, WriteCallback)
behind the scenes to ensure the data is stored resiliently.- Parameters:
tag
- The tag to write.path
- The path to write the NBT to.- Throws:
IOException
- if an I/O error occurs during writing.
-
writeNbt
Behaves much the same asNbtIo.write(CompoundTag, Path)
, but usesatomicWrite(Path, WriteCallback)
behind the scenes to ensure the data is stored resiliently.- Parameters:
tag
- The tag to write.path
- The path to write the NBT to.- Throws:
IOException
- if an I/O error occurs during writing.
-
atomicWrite
public static void atomicWrite(Path targetPath, IOUtilities.WriteCallback writeCallback) throws IOException Writes data to the given path "atomically", such that a crash will not leave the file containing corrupted or otherwise half-written data.This method operates by creating a temporary file, writing to that file, and then moving the temporary file to the correct location after flushing. If a crash occurs during this process, the temporary file will be abandoned.
Furthermore, the stream passed to
writeCallback
is not buffered, and it is the handler's responsibility to implement any buffering on top of this method.- Parameters:
targetPath
- The desired path to write to.writeCallback
- A callback which receives the opened stream to write data to.- Throws:
IOException
- if an I/O error occurs during writing.
-
withIOWorker
-
waitUntilIOWorkerComplete
public static void waitUntilIOWorkerComplete()
-