Class IOUtilities

java.lang.Object
net.neoforged.neoforge.common.IOUtilities

public final class IOUtilities extends Object
Contains helpers for performing file I/O in a resilient manner.
  • Field Details

    • TEMP_FILE_SUFFIX

      private static final String TEMP_FILE_SUFFIX
      See Also:
    • OPEN_OPTIONS

      private static final OpenOption[] OPEN_OPTIONS
    • LOGGER

      private static final org.slf4j.Logger LOGGER
  • Constructor Details

    • IOUtilities

      private IOUtilities()
  • Method Details

    • tryCleanupTempFiles

      public static void tryCleanupTempFiles(Path targetPath, @Nullable @Nullable String prefix)
      Tries to clean up any temporary files that may have been left over from interrupted calls to atomicWrite(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

      private static List<Path> tryListTempFiles(Path targetPath, @Nullable @Nullable String prefix)
    • 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 as NbtIo.writeCompressed(CompoundTag, Path), but uses atomicWrite(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

      public static void writeNbt(net.minecraft.nbt.CompoundTag tag, Path path) throws IOException
      Behaves much the same as NbtIo.write(CompoundTag, Path), but uses atomicWrite(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.