JDK Source Code & java.nio

java.nio is very useful and a pretty good supplement for java.io, today's series I will exploring java.nio key interface and class in below figures:



 Channel: A channel represents an open connection to an entity such as a hardware device, a file, a network socket, or a program component that is capable of performing one or more distinct I/O operations, for example reading or writing. A channel is either open or closed.  A channel is open upon creation, and once closed it remains closed. Once a channel is closed, any attempt to invoke an I/O operation upon it will cause a ClosedChannelException to be thrown.  Whether or not a channel is open may be tested by invoking its isOpen method.

 

SelectableChannel: A selectable channel is either in blocking mode or in non-blocking mode. In blocking mode, every I/O operation invoked upon the channel will block until it completes. In non-blocking mode an I/O operation will never block and may transfer fewer bytes than were requested. Newly-created selectable channels are always in blocking mode. Non-blocking mode is most useful in conjunction with selector-based multiplexing.

 

ReadableByteChannel: A channel that can read bytes. Only one read operation upon a readable channel may be in progress at any given time.  If one thread initiates a read operation upon a channel then any other thread that attempts to initiate another read operation will block until the first operation is complete.  Whether or not other kinds of I/O operations may proceed concurrently with a read operation depends upon the type of the channel.

 

WritableByteChannel: A channel that can write bytes. Only one write operation upon a writable channel may be in progress at any given time.  If one thread initiates a write operation upon a channel then any other thread that attempts to initiate another write operation will block until the first operation is complete.  Whether or not other kinds of I/O operations may proceed concurrently with a write operation depends upon the type of the channel.

 

ScatteringByteChannel: A channel that can read bytes into a sequence of buffers. A scattering read operation reads, in a single invocation, a sequence of bytes into one or more of a given sequence of buffers. Scattering reads are often useful when implementing network protocols or file formats that, for example, group data into segments consisting of one or more fixed-length headers followed by a variable-length body.

 

GatheringByteChannel: A channel that can write bytes from a sequence of buffers. A gathering write operation writes, in a single invocation, a sequence of bytes from one or more of a given sequence of buffers. Gathering writes are often useful when implementing network protocols or file formats that, for example, group data into segments consisting of one or more fixed-length headers followed by a variable-length body.

 

ByteChannel: A interface extends ReadableByteChannel, WritableByteChannel that can read and write bytes. This interface simply unifies ReadableByteChannel and WritableByteChannel, it does not specify any new operations. 

 

AbstractSelectableChannel: Base implementation class for selectable channels. This class defines methods that handle the mechanics of channel registration, deregistration, and closing. It maintains the current blocking mode of this channel as well as its current set of selection keys. It performs all of the synchronization required to implement the SelectableChannel specification. Implementations of the abstract protected methods defined in this class need not synchronize against other threads that might be engaged in the same operations.

 

ServerSocketChannel: A selectable channel for stream-oriented listening sockets. Server-socket channels are not a complete abstraction of listening network sockets.  Binding and the manipulation of socket options must be done through an associated java.net.ServerSocket object obtained by invoking the socket() method.

 

SocketChannel: A selectable channel for stream-oriented connecting sockets. A socket channel is created by invoking one of the open methods of this class.  A newly-created socket channel is open but not yet connected.  An attempt to invoke an I/O operation upon an unconnected channel will cause a NotYetConnectedException to be thrown. A socket channel can be connected by invoking its connect method; once connected, a socket channel remains connected until it is closed. Whether or not a socket channel is connected may be determined by invoking its isConnected method. 

 

FileChannel: A channel for reading, writing, mapping, and manipulating a file. A file channel has a current position within its file which can be both position(). The file itself contains a variable-length sequence of bytes that can be read and written and whose current size can be queried. The size of the file increases when bytes are written beyond its current size; the size of the file decreases when it is truncated. The file may also have some associated metadata such as access permissions, content type, and last-modification time; this class does not define methods for metadata access. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值