Class FileChannelHelper

java.lang.Object
com.helger.io.file.FileChannelHelper

@Immutable public final class FileChannelHelper extends Object
Miscellaneous FileChannel utility methods.
Author:
Philip Helger
  • Method Details

    • getFileSize

      @CheckForSigned public static long getFileSize(@Nullable FileChannel aChannel)
      Get the size of the file represented by the passed channel.
      Parameters:
      aChannel - The file channel to get the size of. May be null.
      Returns:
      The file size in bytes, or -1 if the channel is null or an I/O error occurred.
    • getInputStream

      public static @Nullable InputStream getInputStream(@NonNull File aFile)
      Get an input stream for the passed file. If the file is larger than 1 MB, a memory-mapped input stream is used; otherwise a regular FileInputStream is returned.
      Parameters:
      aFile - The file to read. May not be null.
      Returns:
      null if the file does not exist or cannot be opened.
    • getFileReadChannel

      public static @Nullable FileChannel getFileReadChannel(@NonNull String sFilename)
      Get a read-only FileChannel for the passed filename.
      Parameters:
      sFilename - The name of the file to open. May not be null.
      Returns:
      null if the file could not be opened.
    • getFileReadChannel

      public static @Nullable FileChannel getFileReadChannel(@NonNull File aFile)
      Get a read-only FileChannel for the passed file.
      Parameters:
      aFile - The file to open. May not be null.
      Returns:
      null if the file could not be opened.
    • getMappedInputStream

      public static @Nullable InputStream getMappedInputStream(@NonNull File aFile)
      Get an input stream to the specified file, using memory mapping. If memory mapping fails, a regular FileInputStream is returned.
      Parameters:
      aFile - The file to use. May not be null.
      Returns:
      The Input stream to use.
    • getFileWriteChannel

      public static @Nullable FileChannel getFileWriteChannel(@NonNull String sFilename)
      Get a writable FileChannel for the passed filename using the default append mode.
      Parameters:
      sFilename - The name of the file to open. May not be null.
      Returns:
      null if the file could not be opened.
    • getFileWriteChannel

      public static @Nullable FileChannel getFileWriteChannel(@NonNull String sFilename, @NonNull com.helger.base.io.EAppend eAppend)
      Get a writable FileChannel for the passed filename.
      Parameters:
      sFilename - The name of the file to open. May not be null.
      eAppend - Appending mode. May not be null.
      Returns:
      null if the file could not be opened.
    • getFileWriteChannel

      public static @Nullable FileChannel getFileWriteChannel(@NonNull File aFile)
      Get a writable FileChannel for the passed file using the default append mode.
      Parameters:
      aFile - The file to open. May not be null.
      Returns:
      null if the file could not be opened.
    • getFileWriteChannel

      public static @Nullable FileChannel getFileWriteChannel(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend)
      Get a writable FileChannel for the passed file.
      Parameters:
      aFile - The file to open. May not be null.
      eAppend - Appending mode. May not be null.
      Returns:
      null if the file could not be opened.
    • getMappedOutputStream

      public static @Nullable OutputStream getMappedOutputStream(@NonNull String sFilename)
      Get a memory-mapped output stream for the passed filename using the default append mode.
      Parameters:
      sFilename - The name of the file to open. May not be null.
      Returns:
      null if the file could not be opened or mapped.
    • getMappedOutputStream

      public static @Nullable OutputStream getMappedOutputStream(@NonNull String sFilename, @NonNull com.helger.base.io.EAppend eAppend)
      Get a memory-mapped output stream for the passed filename.
      Parameters:
      sFilename - The name of the file to open. May not be null.
      eAppend - Appending mode. May not be null.
      Returns:
      null if the file could not be opened or mapped.
    • getMappedOutputStream

      public static @Nullable OutputStream getMappedOutputStream(@NonNull File aFile)
      Get a memory-mapped output stream for the passed file using the default append mode.
      Parameters:
      aFile - The file to open. May not be null.
      Returns:
      null if the file could not be opened or mapped.
    • getMappedOutputStream

      public static @Nullable OutputStream getMappedOutputStream(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend)
      Get a memory-mapped output stream for the passed file. If memory mapping fails, a regular FileOutputStream is returned as a fallback.
      Parameters:
      aFile - The file to open. May not be null.
      eAppend - Appending mode. May not be null.
      Returns:
      null if the file could not be opened.