Package org.apache.hadoop.io.nativeio
Class NativeIO
java.lang.Object
org.apache.hadoop.io.nativeio.NativeIO
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.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopyFileUnbuffered(File src, File dst) 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.static FileOutputStreamgetCreateForWriteFileOutputStream(File f, int permissions) static Stringstatic FileDescriptorgetShareDeleteFileDescriptor(File f, long seekOffset) 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.static booleanstatic voidDeprecated.static voidA version of renameTo that throws a descriptive exception when it fails.
-
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
- Throws:
IOException
-
getCreateForWriteFileOutputStream
public static FileOutputStream getCreateForWriteFileOutputStream(File f, int permissions) throws IOException - Parameters:
f- the file that we want to createpermissions- 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 existsIOException- if any other error occurred
-
renameTo
A version of renameTo that throws a descriptive exception when it fails.- Parameters:
src- The source pathdst- The destination path- Throws:
NativeIOException- On failure.IOException
-
link
Deprecated.Creates a hardlink "dst" that points to "src". This is deprecated since JDK7 NIO can create hardlinks via theFilesAPI.- Parameters:
src- source filedst- hardlink location- Throws:
IOException- raised on errors performing I/O.
-
copyFileUnbuffered
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 pathdst- The destination path- Throws:
IOException- raised on errors performing I/O.
-