Top Description Fields Constructors Methods
java.io

public final Class FileDescriptor

extends Object
Class Inheritance
Imports
java.util.ArrayList, .List, .Objects, jdk.internal.access.JavaIOFileDescriptorAccess, .SharedSecrets, jdk.internal.misc.Blocker, jdk.internal.ref.PhantomCleanable

Instances of the file descriptor class serve as an opaque handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes. The main practical use for a file descriptor is to create a FileInputStream or FileOutputStream to contain it.

Applications should not create their own file descriptors.

Author
Pavani Diwanji
Since
1.0

Field Summary

Modifier and TypeField and Description
private boolean
append

true, if file is opened for appending.

private PhantomCleanable<FileDescriptor>
cleanup

Cleanup in case FileDescriptor is not explicitly closed.

private boolean
public static final FileDescriptor
err

A handle to the standard error stream.

private int
private long
public static final FileDescriptor
in

A handle to the standard input stream.

private List<Closeable>
public static final FileDescriptor
out

A handle to the standard output stream.

private Closeable

Constructor Summary

AccessConstructor and Description
public
FileDescriptor()

Constructs an (invalid) FileDescriptor object.

private
FileDescriptor(int
the raw fd number (0, 1, 2)
fd
)

Used for standard input, output, and error only.

Method Summary

Modifier and TypeMethod and Description
pack-priv synchronized void
attach(Closeable c)

Attach a Closeable to this FD for tracking.

pack-priv synchronized void
close()

Close the raw file descriptor or handle, if it has not already been closed.

private native void
pack-priv synchronized void
closeAll(Closeable releaser)

Cycle through all Closeables sharing this FD and call close() on each one.

private static native boolean
getAppend(int fd)

Returns true, if the file was opened for appending.

private static native long
getHandle(int d)

private static native void
pack-priv synchronized void
registerCleanup(PhantomCleanable<FileDescriptor>
a PhantomCleanable to register
cleanable
)

Register a cleanup for the current handle.

pack-priv synchronized void
set(int
the raw fd or -1 to indicate closed
fd
)

Set the fd.

pack-priv void
setHandle(long
the handle or -1 to indicate closed
handle
)

Set the handle.

public void
sync()

Force all system buffers to synchronize with the underlying device.

private native void
pack-priv synchronized void
unregisterCleanup()

Unregister a cleanup for the current raw fd or handle.

public boolean

Returns:

true if the file descriptor object represents a valid, open file, socket, or other active I/O connection; false otherwise.
valid
()

Tests if this file descriptor object is valid.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

appendback to summary
private boolean append

true, if file is opened for appending.

cleanupback to summary
private PhantomCleanable<FileDescriptor> cleanup

Cleanup in case FileDescriptor is not explicitly closed.

closedback to summary
private boolean closed
errback to summary
public static final FileDescriptor err

A handle to the standard error stream. Usually, this file descriptor is not used directly, but rather via the output stream known as System.err.

See Also
java.lang.System#err
fdback to summary
private int fd
handleback to summary
private long handle
inback to summary
public static final FileDescriptor in

A handle to the standard input stream. Usually, this file descriptor is not used directly, but rather via the input stream known as System.in.

See Also
java.lang.System#in
otherParentsback to summary
private List<Closeable> otherParents
outback to summary
public static final FileDescriptor out

A handle to the standard output stream. Usually, this file descriptor is not used directly, but rather via the output stream known as System.out.

See Also
java.lang.System#out
parentback to summary
private Closeable parent

Constructor Detail

FileDescriptorback to summary
public FileDescriptor()

Constructs an (invalid) FileDescriptor object. The fd or handle is set later.

FileDescriptorback to summary
private FileDescriptor(int fd)

Used for standard input, output, and error only. For Windows the corresponding handle is initialized. For Unix the append mode is cached.

Parameters
fd:int

the raw fd number (0, 1, 2)

Method Detail

attachback to summary
pack-priv synchronized void attach(Closeable c)

Attach a Closeable to this FD for tracking. parent reference is added to otherParents when needed to make closeAll simpler.

closeback to summary
pack-priv synchronized void close() throws IOException

Close the raw file descriptor or handle, if it has not already been closed. The native code sets the fd and handle to -1. Clear the cleaner so the close does not happen twice. Package private to allow it to be used in java.io.

Exceptions
IOException:
if close fails
close0back to summary
private native void close0() throws IOException
closeAllback to summary
pack-priv synchronized void closeAll(Closeable releaser) throws IOException

Cycle through all Closeables sharing this FD and call close() on each one. The caller closeable gets to call close0().

Annotations
@SuppressWarnings:try
getAppendback to summary
private static native boolean getAppend(int fd)

Returns true, if the file was opened for appending.

getHandleback to summary
private static native long getHandle(int d)
initIDsback to summary
private static native void initIDs()
registerCleanupback to summary
pack-priv synchronized void registerCleanup(PhantomCleanable<FileDescriptor> cleanable)

Register a cleanup for the current handle. Used directly in java.io and indirectly via fdAccess. The cleanup should be registered after the handle is set in the FileDescriptor.

Parameters
cleanable:PhantomCleanable<FileDescriptor>

a PhantomCleanable to register

setback to summary
pack-priv synchronized void set(int fd)

Set the fd. Used on Unix and for sockets on Windows and Unix. If setting to -1, clear the cleaner. The registerCleanup method should be called for new fds.

Parameters
fd:int

the raw fd or -1 to indicate closed

setHandleback to summary
pack-priv void setHandle(long handle)

Set the handle. Used on Windows for regular files. If setting to -1, clear the cleaner. The registerCleanup method should be called for new handles.

Parameters
handle:long

the handle or -1 to indicate closed

syncback to summary
public void sync() throws SyncFailedException

Force all system buffers to synchronize with the underlying device. This method returns after all modified data and attributes of this FileDescriptor have been written to the relevant device(s). In particular, if this FileDescriptor refers to a physical storage medium, such as a file in a file system, sync will not return until all in-memory modified copies of buffers associated with this FileDescriptor have been written to the physical medium. sync is meant to be used by code that requires physical storage (such as a file) to be in a known state For example, a class that provided a simple transaction facility might use sync to ensure that all changes to a file caused by a given transaction were recorded on a storage medium. sync only affects buffers downstream of this FileDescriptor. If any in-memory buffering is being done by the application (for example, by a BufferedOutputStream object), those buffers must be flushed into the FileDescriptor (for example, by invoking OutputStream.flush) before that data will be affected by sync.

Exceptions
SyncFailedException:
Thrown when the buffers cannot be flushed, or because the system cannot guarantee that all the buffers have been synchronized with physical media.
Since
1.1
sync0back to summary
private native void sync0() throws SyncFailedException
unregisterCleanupback to summary
pack-priv synchronized void unregisterCleanup()

Unregister a cleanup for the current raw fd or handle. Used directly in java.io and indirectly via fdAccess. Normally close() should be used except in cases where it is certain the caller will close the raw fd and the cleanup must not close the raw fd. unregisterCleanup() must be called before the raw fd is closed to prevent a race that makes it possible for the fd to be reallocated to another use and later the cleanup might be invoked.

validback to summary
public boolean valid()

Tests if this file descriptor object is valid.

Returns:boolean

true if the file descriptor object represents a valid, open file, socket, or other active I/O connection; false otherwise.