java channel源码_java.nio.channels.FileChannel文件通道源码解析

package java.nio.channels;

public abstract class FileChannel

extends AbstractInterruptibleChannel

implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel

{

/**

* 初始化一个无参构造器.

*/

protected FileChannel() { }

//打开或创建一个文件,返回一个文件通道来访问文件

public static FileChannel open(Path path,

Set extends OpenOption> options,

FileAttribute>... attrs)

throws IOException

{

FileSystemProvider provider = path.getFileSystem().provider();

return provider.newFileChannel(path, options, attrs);

}

private static final FileAttribute>[] NO_ATTRIBUTES = new FileAttribute[0];

//打开或创建一个文件,返回一个文件通道来访问文件

public static FileChannel open(Path path, OpenOption... options)

throws IOException

{

Set set = new HashSet(options.length);

Collections.addAll(set, options);

return open(path, set, NO_ATTRIBUTES);

}

//从这个通道读入一个字节序列到给定的缓冲区

public abstract int read(ByteBuffer dst) throws IOException;

//从这个通道读入指定开始位置和长度的字节序列到给定的缓冲区

public abstract long read(ByteBuffer[] dsts, int offset, int length)

throws IOException;

/**

* 从这个通道读入一个字节序列到给定的缓冲区

*/

public final long read(ByteBuffer[] dsts) throws IOException {

return read(dsts, 0, dsts.length);

}

/**

* 从给定的缓冲区写入字节序列到这个通道

*/

public abstract int write(ByteBuffer src) throws IOException;

/**

* 从给定缓冲区的子序列向该信道写入字节序列

*/

public abstract long write(ByteBuffer[] srcs, int offset, int length)

throws IOException;

/**

* 从给定的缓冲区写入字节序列到这个通道

*/

public final long write(ByteBuffer[] srcs) throws IOException {

return write(srcs, 0, srcs.length);

}

/**

* 返回通道读写缓冲区中的开始位置

*/

public abstract long position() throws IOException;

/**

* 设置通道读写缓冲区中的开始位置

*/

public abstract FileChannel position(long newPosition) throws IOException;

/**

* 返回此通道文件的当前大小

*/

public abstract long size() throws IOException;

/**

* 通过指定的参数size来截取通道的大小

*/

public abstract FileChannel truncate(long size) throws IOException;

/**

* 强制将通道中的更新文件写入到存储设备(磁盘等)中

*/

public abstract void force(boolean metaData) throws IOException;

/**

* 将当前通道中的文件写入到可写字节通道中

* position就是开始写的位置,long就是写的长度

*/

public abstract long transferTo(long position, long count,

WritableByteChannel target)

throws IOException;

/**

* 将当前通道中的文件写入可读字节通道中

* position就是开始写的位置,long就是写的长度

*/

public abstract long transferFrom(ReadableByteChannel src,

long position, long count)

throws IOException;

/**

* 从通道中读取一系列字节到给定的缓冲区中

* 从指定的读取开始位置position处读取

*/

public abstract int read(ByteBuffer dst, long position) throws IOException;

/**

* 从给定的缓冲区写入字节序列到这个通道

* 从指定的读取开始位置position处开始写

*/

public abstract int write(ByteBuffer src, long position) throws IOException;

// -- Memory-mapped buffers --

/**

* 一个文件映射模式类型安全枚举

*/

public static class MapMode {

//只读映射模型

public static final MapMode READ_ONLY

= new MapMode("READ_ONLY");

//读写映射模型

public static final MapMode READ_WRITE

= new MapMode("READ_WRITE");

/**

* 私有模式(复制在写)映射

*/

public static final MapMode PRIVATE

= new MapMode("PRIVATE");

private final String name;

private MapMode(String name) {

this.name = name;

}

}

/**

* 将该通道文件的一个区域直接映射到内存中

*/

public abstract MappedByteBuffer map(MapMode mode,

long position, long size)

throws IOException;

/**

* 获取当前通道文件的给定区域上的锁

* 区域就是从position处开始,size长度

* shared为true代表获取共享锁,false代表获取独占锁

*/

public abstract FileLock lock(long position, long size, boolean shared)

throws IOException;

/**

* 获取当前通道文件上的独占锁

*/

public final FileLock lock() throws IOException {

return lock(0L, Long.MAX_VALUE, false);

}

/**

* 尝试获取给定的通道文件区域上的锁

* 区域就是从position处开始,size长度

* shared为true代表获取共享锁,false代表获取独占锁

*/

public abstract FileLock tryLock(long position, long size, boolean shared)

throws IOException;

/**

* 尝试获取当前通道文件上的独占锁

*/

public final FileLock tryLock() throws IOException {

return tryLock(0L, Long.MAX_VALUE, false);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值