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 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.private 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
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
(net.minecraft.nbt.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
-
-
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
-
createPredicate
private static BiPredicate<Path,BasicFileAttributes> createPredicate(@Nullable @Nullable String prefix) -
writeNbtCompressed
public static void writeNbtCompressed(net.minecraft.nbt.CompoundTag tag, Path path) throws IOException 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.
-