上文已经说了FileChannel是一个抽象类,FileChannelImpl是其实现,接下来介绍FileChannelImpl,参考代码来自OpenJDK7
首先
public class FileChannelImpl extends FileChannel
该类的成员有:
// Memory allocation size for mapping buffers
private static final long allocationGranularity;
// Used to make native read and write calls
private final FileDispatcher nd;
// File descriptor
private final FileDescriptor fd;
// File access mode (immutable)
private final boolean writable;
private final boolean readable;
private final boolean append;
// Required to prevent finalization of creating stream (immutable)
private final Object parent;
// Thread-safe set of IDs of native threads, for signalling
private final NativeThreadSet threads = new NativeThreadSet(2);
// Lock for operations involving position and size
private final Object positionLock = new Object();
//发现NIO这些类里面好多直接拿一个Object当做锁用的,没有用ReentrantLock等显式锁,毕竟1.6之后内置锁的效率已经得到了提升,并且更方便
关于FileDescriptor的描述:
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.
接下来介绍私有构造函数
private FileChannelImpl(FileDescriptor fd, boolean readable,
boolean writable, boolean append, Object parent)
{
this.fd = fd;
this.readable = readable;
this.writable = writable;
this.append = append;
this.parent = parent;
this.nd = new FileDispatcherImpl(append);
}
既然是私有的那么我们只能使用Open来获取一个实例,关于参数总的parent 我认为是这个FileChannel对应的FileInputStream之类。
// Used by FileInputStream.getChannel() and RandomAccessFile.getChannel()
public static FileChannel open(FileDescriptor fd,
boolean readable, boolean writable,
Object parent)
{
return new FileChannelImpl(fd, readable, writable, false, parent);
}
// Used by FileOutputStream.getChannel
public static FileChannel open(FileDescriptor fd,
boolean readable, boolean writable,
boolean append, Object parent)
{
return new FileChannelImpl(fd, readable, writable, append, parent);
}
接下来将介绍FileChannel的close 、read 和write position方法
先说close方法是AbstractInterruptibleChannel中定义的方法。一下是关于该类的简单信息
public abstract class AbstractInterruptibleChannel
implements Channel, InterruptibleChannel
该类中close的定义:
public final void close() throws IOException {
synchronized (closeLock) {
if (!open)
return;
open = false;
implCloseChannel();
}
}
先说closeLock, 声明为一个final 类成员 private final Object closeLock = new Object();
再说implCloseChannel() protected abstract void implCloseChannel() throws IOException;显而易见继承该抽象类的类来实现该抽象方法
FileChannelImpl的implCloseChannel() 方法
protected void implCloseChannel() throws IOException {
// Release and invalidate any locks that we still hold
if (fileLockTable != null) {
for (FileLock fl: fileLockTable.removeAll()) {
synchronized (fl) {
if (fl.isValid()) {
nd.release(fd, fl.position(), fl.size());
((FileLockImpl)fl).invalidate();
}
}
}
}
nd.preClose(fd);
threads.signalAndWait();
if (parent != null) {
// Close the fd via the parent stream's close method. The parent
// will reinvoke our close method, which is defined in the
// superclass AbstractInterruptibleChannel, but the isOpen logic in
// that method will prevent this method from being reinvoked.
//
((java.io.Closeable)parent).close();
} else {
nd.close(fd);
}
}