Path: blob/master/src/java.base/share/classes/java/nio/channels/package-info.java
41159 views
/*1* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26* Defines channels, which represent connections to entities that are capable of27* performing I/O operations, such as files and sockets; defines selectors, for28* multiplexed, non-blocking I/O operations.29*30* <a id="channels"></a>31*32* <table class="striped" style="text-align:left; margin-left:2em">33* <caption style="display:none">Lists channels and their descriptions</caption>34* <thead>35* <tr><th scope="col">Channels</th>36* <th scope="col">Description</th></tr>37* </thead>38* <tbody>39* <tr><th scope="row"><i>{@link java.nio.channels.Channel}</i></th>40* <td>A nexus for I/O operations</td></tr>41* <tr><th scope="row">42* <span style="padding-left:1em"><i>{@link java.nio.channels.ReadableByteChannel}</i></span></th>43* <td>Can read into a buffer</td></tr>44* <tr><th scope="row">45* <span style="padding-left:2em"><i>{@link java.nio.channels.ScatteringByteChannel}</i></span></th>46* <td>Can read into a sequence of buffers</td></tr>47* <tr><th scope="row">48* <span style="padding-left:1em"><i>{@link java.nio.channels.WritableByteChannel}</i></span></th>49* <td>Can write from a buffer</td></tr>50* <tr><th scope="row">51* <span style="padding-left:2em"><i>{@link java.nio.channels.GatheringByteChannel}</i></span></th>52* <td>Can write from a sequence of buffers</td></tr>53* <tr><th scope="row">54* <span style="padding-left:1em"><i>{@link java.nio.channels.ByteChannel}</i></span></th>55* <td>Can read/write to/from a buffer</td></tr>56* <tr><th scope="row">57* <span style="padding-left:2em"><i>{@link java.nio.channels.SeekableByteChannel}</i></span></th>58* <td>A {@code ByteChannel} connected to an entity that contains a variable-length59* sequence of bytes</td></tr>60* <tr><th scope="row">61* <span style="padding-left:1em"><i>{@link java.nio.channels.AsynchronousChannel}</i></span></th>62* <td>Supports asynchronous I/O operations.</td></tr>63* <tr><th scope="row">64* <span style="padding-left:2em"><i>{@link java.nio.channels.AsynchronousByteChannel}</i></span></th>65* <td>Can read and write bytes asynchronously</td></tr>66* <tr><th scope="row">67* <span style="padding-left:1em"><i>{@link java.nio.channels.NetworkChannel}</i></span></th>68* <td>A channel to a network socket</td></tr>69* <tr><th scope="row">70* <span style="padding-left:2em"><i>{@link java.nio.channels.MulticastChannel}</i></span></th>71* <td>Can join Internet Protocol (IP) multicast groups</td></tr>72* <tr><th scope="row">{@link java.nio.channels.Channels}</th>73* <td>Utility methods for channel/stream interoperation</td></tr>74* </tbody>75* </table>76*77* <p> A <i>channel</i> represents an open connection to an entity such as a78* hardware device, a file, a network socket, or a program component that is79* capable of performing one or more distinct I/O operations, for example reading80* or writing. As specified in the {@link java.nio.channels.Channel} interface,81* channels are either open or closed, and they are both <i>asynchronously82* closeable</i> and <i>interruptible</i>.83*84* <p> The {@link java.nio.channels.Channel} interface is extended by several85* other interfaces.86*87* <p> The {@link java.nio.channels.ReadableByteChannel} interface specifies a88* {@link java.nio.channels.ReadableByteChannel#read read} method that reads bytes89* from the channel into a buffer; similarly, the {@link90* java.nio.channels.WritableByteChannel} interface specifies a {@link91* java.nio.channels.WritableByteChannel#write write} method that writes bytes92* from a buffer to the channel. The {@link java.nio.channels.ByteChannel}93* interface unifies these two interfaces for the common case of channels that can94* both read and write bytes. The {@link java.nio.channels.SeekableByteChannel}95* interface extends the {@code ByteChannel} interface with methods to {@link96* java.nio.channels.SeekableByteChannel#position() query} and {@link97* java.nio.channels.SeekableByteChannel#position(long) modify} the channel's98* current position, and its {@link java.nio.channels.SeekableByteChannel#size99* size}.100*101* <p> The {@link java.nio.channels.ScatteringByteChannel} and {@link102* java.nio.channels.GatheringByteChannel} interfaces extend the {@link103* java.nio.channels.ReadableByteChannel} and {@link104* java.nio.channels.WritableByteChannel} interfaces, respectively, adding {@link105* java.nio.channels.ScatteringByteChannel#read read} and {@link106* java.nio.channels.GatheringByteChannel#write write} methods that take a107* sequence of buffers rather than a single buffer.108*109* <p> The {@link java.nio.channels.NetworkChannel} interface specifies methods110* to {@link java.nio.channels.NetworkChannel#bind bind} the channel's socket,111* obtain the address to which the socket is bound, and methods to {@link112* java.nio.channels.NetworkChannel#getOption get} and {@link113* java.nio.channels.NetworkChannel#setOption set} socket options. The {@link114* java.nio.channels.MulticastChannel} interface specifies methods to join115* Internet Protocol (IP) multicast groups.116*117* <p> The {@link java.nio.channels.Channels} utility class defines static methods118* that support the interoperation of the stream classes of the {@link119* java.io} package with the channel classes of this package. An appropriate120* channel can be constructed from an {@link java.io.InputStream} or an {@link121* java.io.OutputStream}, and conversely an {@link java.io.InputStream} or an122* {@link java.io.OutputStream} can be constructed from a channel. A {@link123* java.io.Reader} can be constructed that uses a given charset to decode bytes124* from a given readable byte channel, and conversely a {@link java.io.Writer} can125* be constructed that uses a given charset to encode characters into bytes and126* write them to a given writable byte channel.127*128* <table class="striped" style="margin-left:2em; text-align:left">129* <caption style="display:none">130* Lists file channels and their descriptions</caption>131* <thead>132* <tr><th scope="col">File channels</th>133* <th scope="col">Description</th></tr>134* </thead>135* <tbody>136* <tr><th scope="row">137* {@link java.nio.channels.FileChannel}</th>138* <td>Reads, writes, maps, and manipulates files</td></tr>139* <tr><th scope="row">140* {@link java.nio.channels.FileLock}</th>141* <td>A lock on a (region of a) file</td></tr>142* <tr><th scope="row">143* {@link java.nio.MappedByteBuffer}</th>144* <td>A direct byte buffer mapped to a region of a file</td></tr>145* </tbody>146* </table>147*148* <p> The {@link java.nio.channels.FileChannel} class supports the usual149* operations of reading bytes from, and writing bytes to, a channel connected to150* a file, as well as those of querying and modifying the current file position151* and truncating the file to a specific size. It defines methods for acquiring152* locks on the whole file or on a specific region of a file; these methods return153* instances of the {@link java.nio.channels.FileLock} class. Finally, it defines154* methods for forcing updates to the file to be written to the storage device that155* contains it, for efficiently transferring bytes between the file and other156* channels, and for mapping a region of the file directly into memory.157*158* <p> A {@code FileChannel} is created by invoking one of its static {@link159* java.nio.channels.FileChannel#open open} methods, or by invoking the {@code160* getChannel} method of a {@link java.io.FileInputStream}, {@link161* java.io.FileOutputStream}, or {@link java.io.RandomAccessFile} to return a162* file channel connected to the same underlying file as the {@link java.io}163* class.164*165* <a id="multiplex"></a>166* <table class="striped" style="margin-left:2em; text-align:left">167* <caption style="display:none">168* Lists multiplexed, non-blocking channels and their descriptions</caption>169* <thead>170* <tr><th scope="col">Multiplexed, non-blocking I/O</th>171* <th scope="col">Description</th></tr>172* </thead>173* <tbody>174* <tr><th scope="row">{@link java.nio.channels.SelectableChannel}</th>175* <td>A channel that can be multiplexed</td></tr>176* <tr><th scope="row">177* <span style="padding-left:2em">{@link java.nio.channels.DatagramChannel}</span></th>178* <td>A channel to a datagram-oriented socket</td></tr>179* <tr><th scope="row">180* <span style="padding-left:2em">{@link java.nio.channels.Pipe.SinkChannel}</span></th>181* <td>The write end of a pipe</td></tr>182* <tr><th scope="row">183* <span style="padding-left:2em">{@link java.nio.channels.Pipe.SourceChannel}</span></th>184* <td>The read end of a pipe</td></tr>185* <tr><th scope="row">186* <span style="padding-left:2em">{@link java.nio.channels.ServerSocketChannel}</span></th>187* <td>A channel to a stream-oriented listening socket</td></tr>188* <tr><th scope="row">189* <span style="padding-left:2em">{@link java.nio.channels.SocketChannel}</span></th>190* <td>A channel for a stream-oriented connecting socket</td></tr>191* <tr><th scope="row">{@link java.nio.channels.Selector}</th>192* <td>A multiplexor of selectable channels</td></tr>193* <tr><th scope="row">{@link java.nio.channels.SelectionKey}</th>194* <td>A token representing the registration of a channel195* with a selector</td></tr>196* <tr><th scope="row">{@link java.nio.channels.Pipe}</th>197* <td>Two channels that form a unidirectional pipe</td></tr>198* </tbody>199* </table>200*201* <p> Multiplexed, non-blocking I/O, which is much more scalable than202* thread-oriented, blocking I/O, is provided by <i>selectors</i>, <i>selectable203* channels</i>, and <i>selection keys</i>.204*205* <p> A <a href="Selector.html"><i>selector</i></a> is a multiplexor of <a206* href="SelectableChannel.html"><i>selectable channels</i></a>, which in turn are207* a special type of channel that can be put into <a208* href="SelectableChannel.html#bm"><i>non-blocking mode</i></a>. To perform209* multiplexed I/O operations, one or more selectable channels are first created,210* put into non-blocking mode, and {@link211* java.nio.channels.SelectableChannel#register <i>registered</i>}212* with a selector. Registering a channel specifies the set of I/O operations213* that will be tested for readiness by the selector, and returns a <a214* href="SelectionKey.html"><i>selection key</i></a> that represents the215* registration.216*217* <p> Once some channels have been registered with a selector, a <a218* href="Selector.html#selop"><i>selection operation</i></a> can be performed in219* order to discover which channels, if any, have become ready to perform one or220* more of the operations in which interest was previously declared. If a channel221* is ready then the key returned when it was registered will be added to the222* selector's <i>selected-key set</i>. The key set, and the keys within it, can223* be examined in order to determine the operations for which each channel is224* ready. From each key one can retrieve the corresponding channel in order to225* perform whatever I/O operations are required.226*227* <p> That a selection key indicates that its channel is ready for some operation228* is a hint, but not a guarantee, that such an operation can be performed by a229* thread without causing the thread to block. It is imperative that code that230* performs multiplexed I/O be written so as to ignore these hints when they prove231* to be incorrect.232*233* <p> This package defines selectable-channel classes corresponding to the {@link234* java.net.DatagramSocket}, {@link java.net.ServerSocket}, and {@link235* java.net.Socket} classes defined in the {@link java.net} package.236* Minor changes to these classes have been made in order to support sockets that237* are associated with channels. This package also defines a simple class that238* implements unidirectional pipes. In all cases, a new selectable channel is239* created by invoking the static {@code open} method of the corresponding class.240* If a channel needs an associated socket then a socket will be created as a side241* effect of this operation.242*243* <p> {@link java.nio.channels.DatagramChannel},244* {@link java.nio.channels.SocketChannel} and245* {@link java.nio.channels.ServerSocketChannel}s can be created246* with different {@link java.net.ProtocolFamily protocol families}. The standard247* family types are specified in {@link java.net.StandardProtocolFamily}.248*249* <p> Channels for <i>Internet Protocol</i> sockets are created using the250* {@link java.net.StandardProtocolFamily#INET INET} or {@link251* java.net.StandardProtocolFamily#INET6 INET6} protocol families. <i>Internet252* Protocol</i> sockets support network communication using TCP and UDP and are253* addressed using {@link java.net.InetSocketAddress}es which encapsulate an IP254* address and port number. <i>Internet Protocol</i> sockets are also the default255* type created, when a protocol family is not specified in the channel factory256* creation method.257*258* <p> Channels for <a id="unixdomain"></a><i>Unix Domain</i> sockets are created259* using the {@link java.net.StandardProtocolFamily#UNIX UNIX} protocol family.260* <i>Unix Domain</i> sockets support local inter-process261* communication on the same host, and are addressed using {@link262* java.net.UnixDomainSocketAddress}es which encapsulate a filesystem pathname263* on the local system.264*265* <p> The implementation of selectors, selectable channels, and selection keys266* can be replaced by "plugging in" an alternative definition or instance of the267* {@link java.nio.channels.spi.SelectorProvider} class defined in the {@link268* java.nio.channels.spi} package. It is not expected that many developers269* will actually make use of this facility; it is provided primarily so that270* sophisticated users can take advantage of operating-system-specific271* I/O-multiplexing mechanisms when very high performance is required.272*273* <p> Much of the bookkeeping and synchronization required to implement the274* multiplexed-I/O abstractions is performed by the {@link275* java.nio.channels.spi.AbstractInterruptibleChannel}, {@link276* java.nio.channels.spi.AbstractSelectableChannel}, {@link277* java.nio.channels.spi.AbstractSelectionKey}, and {@link278* java.nio.channels.spi.AbstractSelector} classes in the {@link279* java.nio.channels.spi} package. When defining a custom selector provider,280* only the {@link java.nio.channels.spi.AbstractSelector} and {@link281* java.nio.channels.spi.AbstractSelectionKey} classes should be subclassed282* directly; custom channel classes should extend the appropriate {@link283* java.nio.channels.SelectableChannel} subclasses defined in this package.284*285* <a id="async"></a>286*287* <table class="striped" style="padding-left:2em; text-align:left">288* <caption style="display:none">289* Lists asynchronous channels and their descriptions</caption>290* <thead>291* <tr><th scope="col">Asynchronous I/O</th>292* <th scope="col">Description</th></tr>293* </thead>294* <tbody>295* <tr><th scope="row">296* {@link java.nio.channels.AsynchronousFileChannel}</th>297* <td>An asynchronous channel for reading, writing, and manipulating a file</td></tr>298* <tr><th scope="row">299* {@link java.nio.channels.AsynchronousSocketChannel}</th>300* <td>An asynchronous channel to a stream-oriented connecting socket</td></tr>301* <tr><th scope="row">302* {@link java.nio.channels.AsynchronousServerSocketChannel}</th>303* <td>An asynchronous channel to a stream-oriented listening socket</td></tr>304* <tr><th scope="row">305* {@link java.nio.channels.CompletionHandler}</th>306* <td>A handler for consuming the result of an asynchronous operation</td></tr>307* <tr><th scope="row">308* {@link java.nio.channels.AsynchronousChannelGroup}</th>309* <td>A grouping of asynchronous channels for the purpose of resource sharing</td></tr>310* </tbody>311* </table>312*313* <p> {@link java.nio.channels.AsynchronousChannel Asynchronous channels} are a314* special type of channel capable of asynchronous I/O operations. Asynchronous315* channels are non-blocking and define methods to initiate asynchronous316* operations, returning a {@link java.util.concurrent.Future} representing the317* pending result of each operation. The {@code Future} can be used to poll or318* wait for the result of the operation. Asynchronous I/O operations can also319* specify a {@link java.nio.channels.CompletionHandler} to invoke when the320* operation completes. A completion handler is user provided code that is executed321* to consume the result of I/O operation.322*323* <p> This package defines asynchronous-channel classes that are connected to324* a stream-oriented connecting or listening socket, or a datagram-oriented socket.325* It also defines the {@link java.nio.channels.AsynchronousFileChannel} class326* for asynchronous reading, writing, and manipulating a file. As with the {@link327* java.nio.channels.FileChannel} it supports operations to truncate the file328* to a specific size, force updates to the file to be written to the storage329* device, or acquire locks on the whole file or on a specific region of the file.330* Unlike the {@code FileChannel} it does not define methods for mapping a331* region of the file directly into memory. Where memory mapped I/O is required,332* then a {@code FileChannel} can be used.333*334* <p> Asynchronous channels are bound to an asynchronous channel group for the335* purpose of resource sharing. A group has an associated {@link336* java.util.concurrent.ExecutorService} to which tasks are submitted to handle337* I/O events and dispatch to completion handlers that consume the result of338* asynchronous operations performed on channels in the group. The group can339* optionally be specified when creating the channel or the channel can be bound340* to a <em>default group</em>. Sophisticated users may wish to create their341* own asynchronous channel groups or configure the {@code ExecutorService}342* that will be used for the default group.343*344* <p> As with selectors, the implementation of asynchronous channels can be345* replaced by "plugging in" an alternative definition or instance of the {@link346* java.nio.channels.spi.AsynchronousChannelProvider} class defined in the347* {@link java.nio.channels.spi} package. It is not expected that many348* developers will actually make use of this facility; it is provided primarily349* so that sophisticated users can take advantage of operating-system-specific350* asynchronous I/O mechanisms when very high performance is required.351*352* <p> Unless otherwise noted, passing a {@code null} argument to a constructor353* or method in any class or interface in this package will cause a {@link354* java.lang.NullPointerException NullPointerException} to be thrown.355*356* @since 1.4357* @author Mark Reinhold358* @author JSR-51 Expert Group359*/360361package java.nio.channels;362363364