Package org.agrona

Class IoUtil

java.lang.Object
org.agrona.IoUtil

public final class IoUtil extends Object
Collection of IO utilities for dealing with files, especially mapping and un-mapping.
  • Field Details

  • Constructor Details

    • IoUtil

      private IoUtil()
  • Method Details

    • fill

      public static void fill(FileChannel fileChannel, long position, long length, byte value)
      Fill region of a file with a given byte value.
      Parameters:
      fileChannel - to fill.
      position - at which to start writing.
      length - of the region to write.
      value - to fill the region with.
    • delete

      public static void delete(File file, boolean ignoreFailures)
      Recursively delete a file or directory tree.
      Parameters:
      file - to be deleted.
      ignoreFailures - don't throw an exception if delete fails.
    • delete

      public static void delete(File file, ErrorHandler errorHandler)
      Recursively delete a file or directory tree.
      Parameters:
      file - to be deleted.
      errorHandler - to delegate errors to on exception.
    • ensureDirectoryExists

      public static void ensureDirectoryExists(File directory, String descriptionLabel)
      Create a directory if it doesn't already exist.
      Parameters:
      directory - the directory which definitely exists after this method call.
      descriptionLabel - to associate with the directory for any exceptions.
    • ensureDirectoryIsRecreated

      public static void ensureDirectoryIsRecreated(File directory, String descriptionLabel, BiConsumer<String,String> callback)
      Create a directory, removing previous directory if it already exists.

      Call callback if it does exist.

      Parameters:
      directory - the directory which definitely exists after this method call.
      descriptionLabel - to associate with the directory for any exceptions and callback.
      callback - to call if directory exists passing back absolute path and descriptionLabel.
    • deleteIfExists

      public static void deleteIfExists(File file)
      Delete file only if it already exists.
      Parameters:
      file - to delete.
    • deleteIfExists

      public static void deleteIfExists(File file, ErrorHandler errorHandler)
      Delete file only if it already exists.
      Parameters:
      file - to delete.
      errorHandler - to delegate error to on exception.
    • createEmptyFile

      public static FileChannel createEmptyFile(File file, long length)
      Create an empty file, fill with 0s, and return the FileChannel.
      Parameters:
      file - to create.
      length - of the file to create.
      Returns:
      FileChannel for the file.
    • createEmptyFile

      public static FileChannel createEmptyFile(File file, long length, boolean fillWithZeros)
      Create an empty file, and optionally fill with 0s, and return the FileChannel.
      Parameters:
      file - to create.
      length - of the file to create.
      fillWithZeros - to the length of the file to force allocation.
      Returns:
      FileChannel for the file.
    • mapExistingFile

      public static MappedByteBuffer mapExistingFile(File location, String descriptionLabel)
      Check that file exists, open file, and return MappedByteBuffer for entire file as FileChannel.MapMode.READ_WRITE.

      The file itself will be closed, but the mapping will persist.

      Parameters:
      location - of the file to map.
      descriptionLabel - to be associated for any exceptions.
      Returns:
      MappedByteBuffer for the file.
    • mapExistingFile

      public static MappedByteBuffer mapExistingFile(File location, String descriptionLabel, long offset, long length)
      Check that file exists, open file, and return MappedByteBuffer for only region specified as FileChannel.MapMode.READ_WRITE.

      The file itself will be closed, but the mapping will persist.

      Parameters:
      location - of the file to map.
      descriptionLabel - to be associated for an exceptions.
      offset - offset to start mapping at.
      length - length to map region.
      Returns:
      MappedByteBuffer for the file.
    • mapExistingFile

      public static MappedByteBuffer mapExistingFile(File location, FileChannel.MapMode mapMode, String descriptionLabel)
      Check that file exists, open file, and return MappedByteBuffer for entire file for a given FileChannel.MapMode.

      The file itself will be closed, but the mapping will persist.

      Parameters:
      location - of the file to map.
      mapMode - for the mapping.
      descriptionLabel - to be associated for any exceptions.
      Returns:
      MappedByteBuffer for the file.
    • mapExistingFile

      public static MappedByteBuffer mapExistingFile(File location, FileChannel.MapMode mapMode, String descriptionLabel, long offset, long length)
      Check that file exists, open file, and return MappedByteBuffer for only region specified for a given FileChannel.MapMode.

      The file itself will be closed, but the mapping will persist.

      Parameters:
      location - of the file to map.
      mapMode - for the mapping.
      descriptionLabel - to be associated for an exceptions.
      offset - offset to start mapping at.
      length - length to map region.
      Returns:
      MappedByteBuffer for the file.
    • mapNewFile

      public static MappedByteBuffer mapNewFile(File location, long length)
      Create a new file, fill with 0s, and return a MappedByteBuffer for the file.

      The file itself will be closed, but the mapping will persist.

      Parameters:
      location - of the file to create and map.
      length - of the file to create and map.
      Returns:
      MappedByteBuffer for the file.
    • mapNewFile

      public static MappedByteBuffer mapNewFile(File location, long length, boolean fillWithZeros)
      Create a new file, and optionally fill with 0s, and return a MappedByteBuffer for the file.

      The file itself will be closed, but the mapping will persist.

      Parameters:
      location - of the file to create and map.
      length - of the file to create and map.
      fillWithZeros - to force allocation.
      Returns:
      MappedByteBuffer for the file.
    • checkFileExists

      public static void checkFileExists(File file, String name)
      Check that a file exists and throw an exception if not.
      Parameters:
      file - to check existence of.
      name - to associate for the exception.
    • unmap

      public static void unmap(MappedByteBuffer buffer)
      Unmap a MappedByteBuffer without waiting for the next GC cycle.
      Parameters:
      buffer - to be unmapped.
      See Also:
    • unmap

      public static void unmap(ByteBuffer buffer)
      Unmap a ByteBuffer without waiting for the next GC cycle if its memory mapped.
      Parameters:
      buffer - to be unmapped.
    • tmpDirName

      public static String tmpDirName()
      Return the system property for java.io.tmpdir ensuring a File.separator is at the end.
      Returns:
      tmp directory for the runtime.
    • removeTrailingSlashes

      public static void removeTrailingSlashes(StringBuilder builder)
      Remove trailing slash characters from a builder leaving the remaining characters.
      Parameters:
      builder - to remove trailing slash characters from.
    • getFileMode

      private static String getFileMode(FileChannel.MapMode mode)