public class ReliableFileDescriptor extends Object
FileDescriptor that can be accessed from multiple threads.
This class provides methods for creating a reliable data pipe
and obtaining the wrapped FileDescriptor or native file descriptor.
| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
ReliableFileDescriptor.AutoCloseFileInputStream |
Represents a file input stream whose file descriptor is automatically closed when this stream is closed.
|
static class |
ReliableFileDescriptor.AutoCloseFileOutputStream |
Represents a file output stream whose file descriptor is automatically closed when this stream is closed.
|
| 构造器 | 说明 |
|---|---|
ReliableFileDescriptor(FileDescriptor fd) |
A constructor used to create a
ReliableFileDescriptor instance
with the specified FileDescriptor. |
ReliableFileDescriptor(FileDescriptor fd,
FileDescriptor commonFd) |
A constructor used to create a
ReliableFileDescriptor instance with the specified FileDescriptor
and the file descriptor used to check file status. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
close() |
Closes this
ReliableFileDescriptor object. |
static ReliableFileDescriptor[] |
createPipe2() |
Creates an array of two
ReliableFileDescriptor objects. |
static ReliableFileDescriptor[] |
createReliablePipe() |
Creates an array of two
ReliableFileDescriptor objects, with each containing a socket file descriptor. |
static ReliableFileDescriptor |
dupFromSocket(Socket socket) |
Creates a
ReliableFileDescriptor by duplicating the file descriptor of a specified socket. |
FileDescriptor |
getFileDescriptor() |
Obtains the
FileDescriptor wrapped in this ReliableFileDescriptor. |
long |
getFileTotalSize() |
Obtains the size of the file matching this file descriptor.
|
int |
getNativeFd() |
Obtains the native file descriptor wrapped in this
ReliableFileDescriptor. |
static ReliableFileDescriptor |
takeChargeOfFileDescriptor(int fd) |
Creates a
ReliableFileDescriptor by duplicating an existing file descriptor. |
public ReliableFileDescriptor(FileDescriptor fd)
ReliableFileDescriptor instance
with the specified FileDescriptor.fd - Indicates the FileDescriptor to wrap in the ReliableFileDescriptor instance.public ReliableFileDescriptor(FileDescriptor fd, FileDescriptor commonFd)
ReliableFileDescriptor instance with the specified FileDescriptor
and the file descriptor used to check file status.fd - Indicates the FileDescriptor to wrap in the ReliableFileDescriptor instance.commonFd - Indicates the file descriptor used to check whether the file is closed.public FileDescriptor getFileDescriptor()
FileDescriptor wrapped in this ReliableFileDescriptor.FileDescriptor wrapped in this ReliableFileDescriptor.public int getNativeFd()
ReliableFileDescriptor.
This ReliableFileDescriptor still owns the file descriptor,
which must be closed through this ReliableFileDescriptor.
ReliableFileDescriptor.
Do not use the return value to close the file descriptor or
pass the return value to any method that assumes ownership of the file descriptor.
Otherwise, the file descriptor will be closed repeatedly.public static ReliableFileDescriptor[] createPipe2() throws OsHelperErrnoException
ReliableFileDescriptor objects.
The array functions as a data pipe. One of the two objects is used to read data, and the other is used to write data.
OsHelperErrnoException - Throws this exception if the array fails to be created.public static ReliableFileDescriptor[] createReliablePipe() throws IOException, OsHelperErrnoException
ReliableFileDescriptor objects, with each containing a socket file descriptor.
The array functions as a data pipe. One of the two objects is used to read data, and the other is used to write data.
OsHelperErrnoException - Throws this exception if the array fails to be created.IOException - Throws this exception if the socket pair used to operate the data pipe fails to be created.public static ReliableFileDescriptor takeChargeOfFileDescriptor(int fd) throws IOException, OsHelperErrnoException
ReliableFileDescriptor by duplicating an existing file descriptor.
After the ReliableFileDescriptor is created, you must close it and the existing file descriptor.
fd - Indicates the existing file descriptor.ReliableFileDescriptor.OsHelperErrnoException - Throws this exception if operating the file descriptor fails.IOException - Throws this exception if the ReliableFileDescriptor fails to be created.public static ReliableFileDescriptor dupFromSocket(Socket socket) throws IOException, OsHelperErrnoException
ReliableFileDescriptor by duplicating the file descriptor of a specified socket.
After the ReliableFileDescriptor is created, you must close it and the socket.
socket - Indicates the specified socket.ReliableFileDescriptor.OsHelperErrnoException - Throws this exception if operating the file descriptor fails.IOException - Throws this exception if the ReliableFileDescriptor fails to be created.public long getFileTotalSize()
-1 if this file descriptor does not match a file, for example,
the file descriptor is a ReliableFileDescriptor obtained via Socket or Pipe.public void close()
ReliableFileDescriptor object.