`
Donald_Draper
  • 浏览: 952519 次
社区版块
存档分类
最新评论

FileDescriptor定义

    博客分类:
  • NIO
阅读更多
SelectionKey定义:http://donald-draper.iteye.com/blog/2369499
    在上面一篇文章中,我们看了SelectionKey,AbstractSelectionKey及SelectionKeyImpl的定义在SelectionKeyImpl的实现中,我们看到key关联的通道实际为SelChImpl,而SelChImpl定义中有一个getFD方法,返回的是FileDescriptor,今天我们就来看一下FileDescriptor
package java.io;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 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
 * {@link FileInputStream} or {@link FileOutputStream} to contain it.
 *一个FileDescriptor用于描述系统底层的特殊的结构句柄,这种底层特殊结构,
 如果一个打开的文件,一个打开的socket,或字节的源和sink。实际上主要用于描述
 文件描述符,创建一个FileInputStream和FileOutputStream时,一般包括文件描述符。
 * <p>Applications should not create their own file descriptors.
 *应用不应该创建自己的文件描述符。
 * @author  Pavani Diwanji
 * @since   JDK1.0
 */
public final class FileDescriptor {

    private int fd;//文件描述值
    private long handle;//初始化文件描述句柄

    /**
     * A use counter for tracking the FIS/FOS/RAF instances that
     * use this FileDescriptor. The FIS/FOS.finalize() will not release
     * the FileDescriptor if it is still under use by any stream.
     记录FileDescriptor的文件输入流,文件输出流和随机访问文件的实例。
     如果文件描述符仍在被其他流所用,调用FIS/FOS.finalize的方法,不会释放FileDescriptor
     */
    private AtomicInteger useCount;

    /**
     * Constructs an (invalid) FileDescriptor
     * object.
     */
    public /**/ FileDescriptor() {
        fd = -1;
        handle = -1;
        useCount = new AtomicInteger();
    }

    // Set up JavaIOFileDescriptorAccess in SharedSecrets
    static {
        sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
            new sun.misc.JavaIOFileDescriptorAccess() {
                public void set(FileDescriptor obj, int fd) {
                    obj.fd = fd;
                }

                public int get(FileDescriptor obj) {
                    return obj.fd;
                }

                public void setHandle(FileDescriptor obj, long handle) {
                    obj.handle = handle;
                }

                public long getHandle(FileDescriptor obj) {
                    return obj.handle;
                }
            }
        );
    }
     static {
        initIDs();
    }
    /* This routine initializes JNI field offsets for the class 
    初始化FileDescriptor的JNI的field offsets
    */
    private static native void initIDs();

    /**
     * A handle to the standard input stream. Usually, this file
     * descriptor is not used directly, but rather via the input stream
     * known as {@code System.in}.
     * 标准输入流的句柄,通常这个文件描述不直接使用,而是通过输入流,比如
      System.in
     * @see     java.lang.System#in
     */
    public static final FileDescriptor in = standardStream(0);

    /**
     * A handle to the standard output stream. Usually, this file
     * descriptor is not used directly, but rather via the output stream
     * known as {@code System.out}.
     标准输出流的句柄,通常这个文件描述不直接使用,而是通过输出流,比如System.out
     * @see     java.lang.System#out
     */
    public static final FileDescriptor out = standardStream(1);

    /**
     * A handle to the standard error stream. Usually, this file
     * descriptor is not used directly, but rather via the output stream
     * known as {@code System.err}.
     *标准错误流的句柄,通常这个文件描述不直接使用,而是通过输出流,比如System.err
     * @see     java.lang.System#err
     */
    public static final FileDescriptor err = standardStream(2);

    /**
     * Tests if this file descriptor object is valid.
     *判断一个文件描述符是否有效,如果handle和fd不为-1,即为有效
     * @return  {@code true} if the file descriptor object represents a
     *          valid, open file, socket, or other active I/O connection;
     *          {@code false} otherwise.
     返回true表示,文件描述对象是一个打开的文件,socket,或者其他激活的IO连接。
     */
    public boolean valid() {
        return ((handle != -1) || (fd != -1));
    }

    /**
     * Force all system buffers to synchronize with the underlying
     * device.  This method returns after all modified data and
     * attributes of this FileDescriptor have been written to the
     * relevant device(s).  In particular, if this FileDescriptor
     * refers to a physical storage medium, such as a file in a file
     * system, sync will not return until all in-memory modified copies
     * of buffers associated with this FileDesecriptor have been
     * written to the physical medium.
     *强制所有的系统缓存与底层设备同步。方法在文件描述句柄将修改的数据或属性
     写到相关设备中时,返回。在一个特殊情况下,如果一个文件描述参考了一个物理存储
     介质,比如文件系统的文件,同步直到与文件描述符相关联的所有缓存的内存修改copy已经
     写到物理介质中。
     * sync is meant to be used by code that requires physical
     * storage (such as a file) to be in a known state  For
     * example, a class that provided a simple transaction facility
     * might use sync to ensure that all changes to a file caused
     * by a given transaction were recorded on a storage medium.
     *需要物理存储(文件)处于一个状态,可以使用同步。比如一个类用同步实现
     简单的事务以保证处于指定事务的文件的改变,记录在存储介质上。
     * sync only affects buffers downstream of this FileDescriptor.  If
     * any in-memory buffering is being done by the application (for
     * example, by a BufferedOutputStream object), those buffers must
     * be flushed into the FileDescriptor (for example, by invoking
     * OutputStream.flush) before that data will be affected by sync.
     *同步会影响文件描述的缓存中已经存在的数据流。如果缓存的内存已经被应用(BufferedOutputStream)
     用完,在数据被同步影响之前,缓存数据必须通过调用输出流的flush,将数据写入到文件描述中。
     * @exception SyncFailedException
     *        Thrown when the buffers cannot be flushed,
     *        or because the system cannot guarantee that all the
     *        buffers have been synchronized with physical media.
     当缓存不能flush或者所有缓存不能同步到物理介质中,则抛出SyncFailedException
     * @since     JDK1.1
     */
    public native void sync() throws SyncFailedException;

    
    
    //根据文件描述符值,初始化文件描述句柄
    private static FileDescriptor standardStream(int fd) {
        FileDescriptor desc = new FileDescriptor();
        desc.handle = set(fd);
        return desc;
    }
    private static native long set(int d);

    // package private methods used by FIS, FOS and RAF.

    int incrementAndGetUseCount() {
        return useCount.incrementAndGet();
    }

    int decrementAndGetUseCount() {
        return useCount.decrementAndGet();
    }
}
0
0
分享到:
评论

相关推荐

    Android 串口 文件描述符

    使用Java的FileDescriptor来操作串口。 ①编写Java文件 ②使用javac将.java编译成.class文件,然后使用javah编译相应的头文件 ③实现相关的.c/.cpp文件(实现头文件中定义的函数) ④编写Android.mk和Aplication.mk...

    test_descriptor:提供方便,易于阅读的API,用于定义和验证测试中的目录结构

    test_descriptor软件包提供了一个方便,易于阅读的API,用于定义和验证测试中的目录结构。 我们建议您使用d前缀导入该库。 和函数是主要的入口点。 他们定义可以使用创建文件系统结构并使用验证 例如: import 'dart...

    xmljava系统源码-AndroidSerialPort:串口程序,android通过RS232与PC或者其他设备进行通讯

    FileDescriptor open(String path, int baudrate, int flags); public native void close(); java 提供了以下两个接口给上层应用使用: public InputStream getInputStream() public OutputStream getOutputStream()...

    WinMount

    支持传统压缩格式转换到MOU格式,同时兼容ZIP RAR等压缩以及支持多种光盘映像格式的挂载功能,包括:ISO、cue/bin、ccd (CloneCD)、bwt (Blindwrite)、mds (Media Descriptor File)、cdi (Discjuggler)、 nrg (Nero)、...

    Maven权威指南 很精典的学习教程,比ANT更好用

    Overview of the Assembly Descriptor 12.4. The Assembly Descriptor 12.4.1. Property References in Assembly Descriptors 12.4.2. Required Assembly Information 12.5. Controlling the Contents of an ...

    SQL21日自学通

    TNS:listener Could Not Resolve SID Given in Connect Descriptor 484 Insufficient Privileges During Grants484 Escape Character in Your Statement--Invalid Character 485 Cannot Create Operating System ...

    WinMount 一款功能强大的Windows小工具

     WinMoun t还支持多种光盘映像格式,包括mou、ISO、 cue/bin、ccd (CloneCD)、bwt (Blindwrite)、mds(Media Descriptor File)、cdi (Discjuggler)、nrg (Nero)、pdi (Instant CD/DVD)、b5t (BlindWrite 5)、isz ...

    GifDecoder

    * File read status: Error decoding file &#40;may be partially decoded&#41; */ public static final int STATUS_FORMAT_ERROR = 1; /** * File read status: Unable to open source. */ public static ...

    21天学习SQL V1.0

    21天学习SQL V1.0.pdf 66 SQL 21 日自学通(V1.0) 翻译人笨猪 ...日期/时间函数............................................................................................................ADD_MONTHS..................

    leveldb实现解析

    19. FileNumber(db/dbformat.h) ...................................................... 6 20. filename (db/filename.cc) ........................................................ 6 21. level-n (db/...

Global site tag (gtag.js) - Google Analytics