上文已经说了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);
}

}

【原创】java NIO FileChannel 学习笔记 FileChannel实现分析 即FileChannelImpl分析的更多相关文章

  1. 【原创】java NIO FileChannel 学习笔记 FileChannel 简介

    java NIO 中FileChannel 的实现类是  FileChannelImpl,FileChannel本身是一个抽象类. 先介绍FileChannel File Channels 是线程安全 ...

  2. Java NIO 完全学习笔记(转)

    本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java N ...

  3. Java NIO 核心组件学习笔记

    背景知识 同步.异步.阻塞.非阻塞 首先,这几个概念非常容易搞混淆,但NIO中又有涉及,所以总结一下[1]. 同步:API调用返回时调用者就知道操作的结果如何了(实际读取/写入了多少字节). 异步:相 ...

  4. 【原创】java NIO selector 学习笔记 一

    能力有限,仅仅是自己看源码的一些笔记. 主要介绍 可选通道 和 选择器 选择键(SelectableChannel  和 Selector SelectionKey) 选择器(Selector) 选择 ...

  5. Java NIO 缓冲区学习笔记

    Buffer其实就是是一个容器对象,它包含一些要写入或者刚读出的数据.在NIO中加入Buffer对象,体现了新库与原I/O的一个重要区别.在面向流的I/O中,您将数据直接写入或者将数据直接读到Stre ...

  6. NIO模型学习笔记

    NIO模型学习笔记 简介 Non-blocking I/O 或New I/O 自JDK1.4开始使用 应用场景:高并发网络服务器支持 概念理解 模型:对事物共性的抽象 编程模型:对编程共性的抽象 BI ...

  7. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  8. Java多线程技术学习笔记(二)

    目录: 线程间的通信示例 等待唤醒机制 等待唤醒机制的优化 线程间通信经典问题:多生产者多消费者问题 多生产多消费问题的解决 JDK1.5之后的新加锁方式 多生产多消费问题的新解决办法 sleep和w ...

  9. Java安全防御学习笔记V1.0

    Java安全防御学习笔记V1.0http://www.docin.com/p-766808938.html

随机推荐

  1. DOM操作基本用法

    本文列举了js中DOM选取的基本用法,在列表中没有id的情况下如何选取到需要的一项,代码如下: <h2>获取Jerry的js代码</h2> <ul id="fi ...

  2. 项目实战7—Mysql实现企业级数据库主从复制架构实战

    Mysql实现企业级数据库主从复制架构实战 环境背景:公司规模已经形成,用户数据已成为公司的核心命脉,一次老王一不小心把数据库文件删除,通过mysqldump备份策略恢复用了两个小时,在这两小时中,公 ...

  3. GIT常用命令(图片版)

    Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. 本来想着只把最有用.最常用的 Git 命令记下来,但是总觉得这个也挺有用.那个也用 ...

  4. form表单提交引发的血案

    最近,公司某条产品线上的一个功能出了问题:点击查询的时候,该页面在IE上直接卡死,chrome上会卡顿一段时间候提交表单进行查询.拿到这个bug单子以后,简单重现了下,基本上定位到是查询操作中的问题, ...

  5. Python面向对象解析

    面向对象概述 什么是面向对象:从简单来说,如果程序中的所有功能都是用 类 和 对象 来实现,那么就是面向对象编程了. 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无 ...

  6. ios7 以后准确获取iphone设备的MAC(物理地址)

    通过参考 钉钉 项目,知道是通过wifi拿到路由的MAC地址.那么可不可以拿到iphone 设备的MAC 地址呢? 经过一番搜索,发现所有文章都是针对 ios 7 以前 可以拿到. 而且方法也都是同一 ...

  7. Golang 探索对Goroutine的控制方法

    前言 在golang中,只需要在函数调用前加上关键字go即可创建一个并发任务单元,而这个新建的任务会被放入队列中,等待调度器安排.相比系统的MB级别线程栈,goroutine的自定义栈只有2KB,这使 ...

  8. 数据库文件*.sdf文件定时备份,但是大小的增量在不断增长的问题排查

    在某项目上,使用SQL Server数据库,现场反馈每天定时备份数据库文件,每天的数据量是400多个申请单的量.之前每天增长量是50M,但是后来两天增长量是80M,每天的数据量差不多. 到底从什么地方 ...

  9. 相似QQ对话框上下部分可拖动代码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  10. AntiXSS的作用

    XSS跨站脚本攻击        是指用户输入HTML编码对网站进行跨站攻击.            通过使用FCKeditor.FreeTextBox.Rich TextBox.Cute Edito ...