java.nio.channels.FileChannel源码是什么

49次阅读
没有评论

共计 7421 个字符,预计需要花费 19 分钟才能阅读完成。

本篇内容介绍了“java.nio.channels.FileChannel 源码是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

版本:JDK7

package java.nio.channels;

import java.io.; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.spi.AbstractInterruptibleChannel; import java.nio.file.; import java.nio.file.attribute.FileAttribute; import java.nio.file.spi.*; import java.util.Set; import java.util.HashSet; import java.util.Collections;

public abstract class FileChannel extends AbstractInterruptibleChannel implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel {
protected FileChannel() { }
 * Opens or creates a file, returning a file channel to access the file.
 *
 *  p  The {[@code](https://my.oschina.net/codeo) options} parameter determines how the file is opened.
 * The {[@link](https://my.oschina.net/u/393) StandardOpenOption#READ READ} and {[@link](https://my.oschina.net/u/393) StandardOpenOption#WRITE
 * WRITE} options determine if the file should be opened for reading and/or
 * writing. If neither option (or the {[@link](https://my.oschina.net/u/393) StandardOpenOption#APPEND APPEND}
 * option) is contained in the array then the file is opened for reading.
 * By default reading or writing commences at the beginning of the file.
 * [@since](https://my.oschina.net/u/266547) 1.7
 */
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];
 * Opens or creates a file, returning a file channel to access the file.
 * @since 1.7
 */
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);
// -- Channel operations --
 * Reads a sequence of bytes from this channel into the given buffer.
 */
public abstract int read(ByteBuffer dst) throws IOException;
 * Reads a sequence of bytes from this channel into a subsequence of the
 * given buffers.
 */
public abstract long read(ByteBuffer[] dsts, int offset, int length)
 throws IOException;
 * Reads a sequence of bytes from this channel into the given buffers.
 */
public final long read(ByteBuffer[] dsts) throws IOException {
 return read(dsts, 0, dsts.length);
 * Writes a sequence of bytes to this channel from the given buffer.
 */
public abstract int write(ByteBuffer src) throws IOException;
 * Writes a sequence of bytes to this channel from a subsequence of the given buffers.
 */
public abstract long write(ByteBuffer[] srcs, int offset, int length)
 throws IOException;
 * Writes a sequence of bytes to this channel from the given buffers.
 */
public final long write(ByteBuffer[] srcs) throws IOException {
 return write(srcs, 0, srcs.length);
 * Returns this channel s file position.
 */
public abstract long position() throws IOException;
 * Sets this channel s file position.
 *
 *  p  Setting the position to a value that is greater than the file s
 * current size is legal but does not change the size of the file. A later
 * attempt to read bytes at such a position will immediately return an
 * end-of-file indication. A later attempt to write bytes at such a
 * position will cause the file to be grown to accommodate the new bytes;
 * the values of any bytes between the previous end-of-file and the
 * newly-written bytes are unspecified.  /p
 *
 *  如果将位置设置在文件结束符之后,然后试图从文件通道中读取数据,读方法将返回 -1 ——  文件结束标志。
 *  如果将位置设置在文件结束符之后,然后向通道中写数据,文件将撑大到当前位置并 (从当前位置开始) 写入数据。这可能导致“文件空洞”,磁盘上物理文件中写入的数据间有空隙。
 *
 * @param newPosition
 * The new position, a non-negative integer counting
 * the number of bytes from the beginning of the file
 */
public abstract FileChannel position(long newPosition) throws IOException;
 * Returns the current size of this channel s file. 
 */
public abstract long size() throws IOException;
 * Truncates this channel s file to the given size.  截取一个文件
 * 
 *  p  If the given size is less than the file s current size then the file
 * is truncated, discarding any bytes beyond the new end of the file. If
 * the given size is greater than or equal to the file s current size then
 * the file is not modified. In either case, if this channel s file
 * position is greater than the given size then it is set to that size.
 */
public abstract FileChannel truncate(long size) throws IOException;
 * Forces any updates to this channel s file to be written to the storage
 * device that contains it.
 *  将通道里尚未写入磁盘的数据强制写到磁盘上。
 *  说明:
 * 1)操作系统将数据缓存在内存中,所以无法保证写入到 FileChannel 里的数据一定会即时写到磁盘上。
 *  2)如果要保证写入到 FileChannel 里的数据即时写到磁盘上,需要调用 force()方法,参数表示是否将文件的元数据写到磁盘上。
 */
public abstract void force(boolean metaData) throws IOException;
//  通道之间的数据传输
 * Transfers bytes from this channel s file to the given writable byte channel.
 */
public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;
 * Transfers bytes into this channel s file from the given readable byte channel.
 */
public abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException;
 * Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.
 *
 * This method does not modify this channel s position. 
 * If the given position is greater than the file s current size then no bytes are read.
 */
public abstract int read(ByteBuffer dst, long position) throws IOException;
 * Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.
 *
 * This method does not modify this channel s position. 
 * If the given position is greater than the file s current size then the file will be grown to accommodate the new bytes;
 * the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.
 */
public abstract int write(ByteBuffer src, long position) throws IOException;

 * A typesafe enumeration for file-mapping modes.
 *
 * @since 1.4
 *
 * @see java.nio.channels.FileChannel#map
 */
public static class MapMode {
 /**
 * Mode for a read-only mapping.
 */
 public static final MapMode READ_ONLY = new MapMode(READ_ONLY
 /**
 * Mode for a read/write mapping.
 */
 public static final MapMode READ_WRITE = new MapMode(READ_WRITE
 /**
 * Mode for a private (copy-on-write) mapping.
 */
 public static final MapMode PRIVATE = new MapMode(PRIVATE
 private final String name;
 private MapMode(String name) {
 this.name = name;
 }
 public String toString() {
 return name;
 }
 * Maps a region of this channel s file directly into memory.
 *
 * A region of a file may be mapped into memory in one of three modes:
 *
 * Read-only、Read/write、Private
 *
 * @param mode
 * One of the constants {@link MapMode#READ_ONLY READ_ONLY}, {@link
 * MapMode#READ_WRITE READ_WRITE}, or {@link MapMode#PRIVATE
 * PRIVATE} defined in the {@link MapMode} class, according to
 * whether the file is to be mapped read-only, read/write, or
 * privately (copy-on-write), respectively
 *
 * @param position
 * The position within the file at which the mapped region
 * is to start; must be non-negative
 *
 * @param size
 * The size of the region to be mapped; must be non-negative and
 * no greater than {@link java.lang.Integer#MAX_VALUE}
 *
 *
 * @see java.nio.channels.FileChannel.MapMode
 * @see java.nio.MappedByteBuffer
 */
public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException;

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计7421字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)