Class NativeIO

java.lang.Object
org.apache.hadoop.io.nativeio.NativeIO

@Private @Unstable public class NativeIO extends Object
JNI wrappers for various native IO-related calls not available in Java. These functions should generally be used alongside a fallback to another more portable mechanism.
  • Constructor Details

    • NativeIO

      public NativeIO()
  • Method Details

    • isAvailable

      public static boolean isAvailable()
      Returns:
      Return true if the JNI-based native IO extensions are available.
    • getOwner

      public static String getOwner(FileDescriptor fd) throws IOException
      Throws:
      IOException
    • getShareDeleteFileDescriptor

      public static FileDescriptor getShareDeleteFileDescriptor(File f, long seekOffset) throws IOException
      Create a FileDescriptor that shares delete permission on the file opened at a given offset, i.e. other process can delete the file the FileDescriptor is reading. Only Windows implementation uses the native interface.
      Parameters:
      f - input f.
      seekOffset - input seekOffset.
      Returns:
      FileDescriptor.
      Throws:
      IOException - raised on errors performing I/O.
    • getCreateForWriteFileOutputStream

      public static FileOutputStream getCreateForWriteFileOutputStream(File f, int permissions) throws IOException
      Parameters:
      f - the file that we want to create
      permissions - we want to have on the file (if security is enabled)
      Returns:
      Create the specified File for write access, ensuring that it does not exist.
      Throws:
      SecureIOUtils.AlreadyExistsException - if the file already exists
      IOException - if any other error occurred
    • renameTo

      public static void renameTo(File src, File dst) throws IOException
      A version of renameTo that throws a descriptive exception when it fails.
      Parameters:
      src - The source path
      dst - The destination path
      Throws:
      NativeIOException - On failure.
      IOException
    • link

      @Deprecated public static void link(File src, File dst) throws IOException
      Deprecated.
      Creates a hardlink "dst" that points to "src". This is deprecated since JDK7 NIO can create hardlinks via the Files API.
      Parameters:
      src - source file
      dst - hardlink location
      Throws:
      IOException - raised on errors performing I/O.
    • copyFileUnbuffered

      public static void copyFileUnbuffered(File src, File dst) throws IOException
      Unbuffered file copy from src to dst without tainting OS buffer cache In POSIX platform: It uses FileChannel#transferTo() which internally attempts unbuffered IO on OS with native sendfile64() support and falls back to buffered IO otherwise. It minimizes the number of FileChannel#transferTo call by passing the the src file size directly instead of a smaller size as the 3rd parameter. This saves the number of sendfile64() system call when native sendfile64() is supported. In the two fall back cases where sendfile is not supported, FileChannle#transferTo already has its own batching of size 8 MB and 8 KB, respectively. In Windows Platform: It uses its own native wrapper of CopyFileEx with COPY_FILE_NO_BUFFERING flag, which is supported on Windows Server 2008 and above. Ideally, we should use FileChannel#transferTo() across both POSIX and Windows platform. Unfortunately, the wrapper(Java_sun_nio_ch_FileChannelImpl_transferTo0) used by FileChannel#transferTo for unbuffered IO is not implemented on Windows. Based on OpenJDK 6/7/8 source code, Java_sun_nio_ch_FileChannelImpl_transferTo0 on Windows simply returns IOS_UNSUPPORTED. Note: This simple native wrapper does minimal parameter checking before copy and consistency check (e.g., size) after copy. It is recommended to use wrapper function like the Storage#nativeCopyFileUnbuffered() function in hadoop-hdfs with pre/post copy checks.
      Parameters:
      src - The source path
      dst - The destination path
      Throws:
      IOException - raised on errors performing I/O.