您的位置:首页 > 编程语言 > Java开发

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

2018-02-28 16:40 656 查看
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<OpenOption> set = new HashSet<OpenOption>(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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: