Package org.agrona

Class IoUtil


  • public final class IoUtil
    extends java.lang.Object
    Collection of IO utilities for dealing with files, especially mapping and un-mapping.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private IoUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkFileExists​(java.io.File file, java.lang.String name)
      Check that a file exists and throw an exception if not.
      static java.nio.channels.FileChannel createEmptyFile​(java.io.File file, long length)
      Create an empty file, fill with 0s, and return the FileChannel.
      static java.nio.channels.FileChannel createEmptyFile​(java.io.File file, long length, boolean fillWithZeros)
      Create an empty file, and optionally fill with 0s, and return the FileChannel.
      static void delete​(java.io.File file, boolean ignoreFailures)
      Recursively delete a file or directory tree.
      static void delete​(java.io.File file, ErrorHandler errorHandler)
      Recursively delete a file or directory tree.
      static void deleteIfExists​(java.io.File file)
      Delete file only if it already exists.
      static void deleteIfExists​(java.io.File file, ErrorHandler errorHandler)
      Delete file only if it already exists.
      static void ensureDirectoryExists​(java.io.File directory, java.lang.String descriptionLabel)
      Create a directory if it doesn't already exist.
      static void ensureDirectoryIsRecreated​(java.io.File directory, java.lang.String descriptionLabel, java.util.function.BiConsumer<java.lang.String,​java.lang.String> callback)
      Create a directory, removing previous directory if it already exists.
      static void fill​(java.nio.channels.FileChannel fileChannel, long position, long length, byte value)
      Fill region of a file with a given byte value.
      private static java.lang.String getFileMode​(java.nio.channels.FileChannel.MapMode mode)  
      static java.nio.MappedByteBuffer mapExistingFile​(java.io.File location, java.lang.String descriptionLabel)
      Check that file exists, open file, and return MappedByteBuffer for entire file as FileChannel.MapMode.READ_WRITE.
      static java.nio.MappedByteBuffer mapExistingFile​(java.io.File location, java.lang.String descriptionLabel, long offset, long length)
      Check that file exists, open file, and return MappedByteBuffer for only region specified as FileChannel.MapMode.READ_WRITE.
      static java.nio.MappedByteBuffer mapExistingFile​(java.io.File location, java.nio.channels.FileChannel.MapMode mapMode, java.lang.String descriptionLabel)
      Check that file exists, open file, and return MappedByteBuffer for entire file for a given FileChannel.MapMode.
      static java.nio.MappedByteBuffer mapExistingFile​(java.io.File location, java.nio.channels.FileChannel.MapMode mapMode, java.lang.String descriptionLabel, long offset, long length)
      Check that file exists, open file, and return MappedByteBuffer for only region specified for a given FileChannel.MapMode.
      static java.nio.MappedByteBuffer mapNewFile​(java.io.File location, long length)
      Create a new file, fill with 0s, and return a MappedByteBuffer for the file.
      static java.nio.MappedByteBuffer mapNewFile​(java.io.File location, long length, boolean fillWithZeros)
      Create a new file, and optionally fill with 0s, and return a MappedByteBuffer for the file.
      static void removeTrailingSlashes​(java.lang.StringBuilder builder)
      Remove trailing slash characters from a builder leaving the remaining characters.
      static java.lang.String tmpDirName()
      Return the system property for java.io.tmpdir ensuring a File.separator is at the end.
      static void unmap​(java.nio.ByteBuffer buffer)
      Unmap a ByteBuffer without waiting for the next GC cycle if its memory mapped.
      static void unmap​(java.nio.MappedByteBuffer buffer)
      Unmap a MappedByteBuffer without waiting for the next GC cycle.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IoUtil

        private IoUtil()
    • Method Detail

      • fill

        public static void fill​(java.nio.channels.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​(java.io.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​(java.io.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​(java.io.File directory,
                                                 java.lang.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​(java.io.File directory,
                                                      java.lang.String descriptionLabel,
                                                      java.util.function.BiConsumer<java.lang.String,​java.lang.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​(java.io.File file)
        Delete file only if it already exists.
        Parameters:
        file - to delete.
      • deleteIfExists

        public static void deleteIfExists​(java.io.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 java.nio.channels.FileChannel createEmptyFile​(java.io.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 java.nio.channels.FileChannel createEmptyFile​(java.io.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 java.nio.MappedByteBuffer mapExistingFile​(java.io.File location,
                                                                java.lang.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 java.nio.MappedByteBuffer mapExistingFile​(java.io.File location,
                                                                java.lang.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 java.nio.MappedByteBuffer mapExistingFile​(java.io.File location,
                                                                java.nio.channels.FileChannel.MapMode mapMode,
                                                                java.lang.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 java.nio.MappedByteBuffer mapExistingFile​(java.io.File location,
                                                                java.nio.channels.FileChannel.MapMode mapMode,
                                                                java.lang.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 java.nio.MappedByteBuffer mapNewFile​(java.io.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 java.nio.MappedByteBuffer mapNewFile​(java.io.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​(java.io.File file,
                                           java.lang.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​(java.nio.MappedByteBuffer buffer)
        Unmap a MappedByteBuffer without waiting for the next GC cycle.
        Parameters:
        buffer - to be unmapped.
        See Also:
        BufferUtil.free(ByteBuffer)
      • unmap

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

        public static java.lang.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​(java.lang.StringBuilder builder)
        Remove trailing slash characters from a builder leaving the remaining characters.
        Parameters:
        builder - to remove trailing slash characters from.
      • getFileMode

        private static java.lang.String getFileMode​(java.nio.channels.FileChannel.MapMode mode)