io nio

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio 
OpenJDK: IO
License:The GNU General Public License (GPL)
URL:https://openjdk.dev.java.net
Description:
Package NameComment
java.io Provides for system input and output through data streams, serialization and the file system. Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown.

Package Specification

Related Documentation

For overviews, tutorials, examples, guides, and tool documentation, please see: @since JDK1.0
java.nio Defines buffers, which are containers for data, and provides an overview of the other NIO packages.

The central abstractions of the NIO APIs are:

  • Buffers, which are containers for data;

  • Charsets and their associated decoders and encoders,
    which translate between bytes and Unicode characters;

  • Channels of various types, which represent connections
    to entities capable of performing I/O operations; and

  • Selectors and selection keys, which together with
    selectable channels define a multiplexed, non-blocking
    I/O
     facility.

The java.nio package defines the buffer classes, which are used throughout the NIO APIs. The charset API is defined in the {@link java.nio.charset} package, and the channel and selector APIs are defined in the {@link java.nio.channels} package. Each of these subpackages has its own service-provider (SPI) subpackage, the contents of which can be used to extend the platform's default implementations or to construct alternative implementations.

Buffers

Description

{@link java.nio.Buffer} Position, limit, and capacity;
clear, flip, rewind, and mark/reset
  {@link java.nio.ByteBuffer} Get/put, compact, views; allocate, wrap
    {@link java.nio.MappedByteBuffer}   A byte buffer mapped to a file
  {@link java.nio.CharBuffer} Get/put, compact; allocate, wrap
  {@link java.nio.DoubleBuffer}     ' '
  {@link java.nio.FloatBuffer}     ' '
  {@link java.nio.IntBuffer}     ' '
  {@link java.nio.LongBuffer}     ' '
  {@link java.nio.ShortBuffer}     ' '
{@link java.nio.ByteOrder} Typesafe enumeration for byte orders

A buffer is a container for a fixed amount of data of a specific primitive type. In addition to its content a buffer has a position, which is the index of the next element to be read or written, and a limit, which is the index of the first element that should not be read or written. The base {@link java.nio.Buffer} class defines these properties as well as methods for clearing, flipping, and rewinding, for marking the current position, and for resetting the position to the previous mark.

There is a buffer class for each non-boolean primitive type. Each class defines a family of get and put methods for moving data out of and in to a buffer, methods for compacting, duplicating, and slicing a buffer, and static methods for allocating a new buffer as well as for wrapping an existing array into a buffer.

Byte buffers are distinguished in that they can be used as the sources and targets of I/O operations. They also support several features not found in the other buffer classes:

  • A byte buffer can be allocated as a direct buffer, in which case the Java virtual machine will make a best effort to perform native I/O operations directly upon it.

  • A byte buffer can be created by {@link java.nio.channels.FileChannel#map mapping} a region of a file directly into memory, in which case a few additional file-related operations defined in the {@link java.nio.MappedByteBuffer} class are available.

  • A byte buffer provides access to its content as either a heterogeneous or homogeneous sequence of binary data of any non-boolean primitive type, in either big-endian or little-endian byte order.

Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a {@link java.lang.NullPointerException NullPointerException} to be thrown. @since 1.4 @version 1.18, 07/05/05 @author Mark Reinhold @author JSR-51 Expert Group

java.nio.channels Defines channels, which represent connections to entities that are capable of performing I/O operations, such as files and sockets; defines selectors, for multiplexed, non-blocking I/O operations.

Channels

Description

{@link java.nio.channels.Channel} A nexus for I/O operations
  {@link java.nio.channels.ReadableByteChannel} Can read into a buffer
    {@link java.nio.channels.ScatteringByteChannel}   Can read into a sequence of buffers
  {@link java.nio.channels.WritableByteChannel} Can write from a buffer
    {@link java.nio.channels.GatheringByteChannel} Can write from a sequence of buffers
  {@link java.nio.channels.ByteChannel} Can read/write to/from a buffer
{@link java.nio.channels.Channels} Utility methods for channel/stream interoperation

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. As specified in the {@link java.nio.channels.Channel} interface, channels are either open or closed, and they are both asynchronously closeable and interruptible.

The {@link java.nio.channels.Channel} interface is extended by several other interfaces, each of which specifies a new I/O operation.

The {@link java.nio.channels.ReadableByteChannel} interface specifies a {@link java.nio.channels.ReadableByteChannel#read read} method that reads bytes from the channel into a buffer; similarly, the {@link java.nio.channels.WritableByteChannel} interface specifies a {@link java.nio.channels.WritableByteChannel#write write} method that writes bytes from a buffer to the channel. The {@link java.nio.channels.ByteChannel} interface unifies these two interfaces for the common case of channels that can both read and write bytes.

The {@link java.nio.channels.ScatteringByteChannel} and {@link java.nio.channels.GatheringByteChannel} interfaces extend the {@link java.nio.channels.ReadableByteChannel} and {@link java.nio.channels.WritableByteChannel} interfaces, respectively, adding {@link java.nio.channels.ScatteringByteChannel#read read} and {@link java.nio.channels.GatheringByteChannel#write write} methods that take a sequence of buffers rather than a single buffer.

The {@link java.nio.channels.Channels} utility class defines static methods that support the interoperation of the stream classes of the {@link java.io} package with the channel classes of this package. An appropriate channel can be constructed from an {@link java.io.InputStream} or an {@link java.io.OutputStream}, and conversely an {@link java.io.InputStream} or an {@link java.io.OutputStream} can be constructed from a channel. A {@link java.io.Reader} can be constructed that uses a given charset to decode bytes from a given readable byte channel, and conversely a {@link java.io.Writer} can be constructed that uses a given charset to encode characters into bytes and write them to a given writable byte channel.

File channels

Description

{@link java.nio.channels.FileChannel} Reads, writes, maps, and manipulates files
{@link java.nio.channels.FileLock} A lock on a (region of a) file
{@link java.nio.MappedByteBuffer}   A direct byte buffer mapped to a region of a file

The {@link java.nio.channels.FileChannel} class supports the usual operations of reading bytes from, and writing bytes to, a channel connected to a file, as well as those of querying and modifying the current file position and truncating the file to a specific size. It defines methods for acquiring locks on the whole file or on a specific region of a file; these methods return instances of the {@link java.nio.channels.FileLock} class. Finally, it defines methods for forcing updates to the file to be written to the storage device that contains it, for efficiently transferring bytes between the file and other channels, and for mapping a region of the file directly into memory. This last operation creates an instance of the {@link java.nio.MappedByteBuffer} class, which extends the {@link java.nio.ByteBuffer} class with several file-related operations.

A getChannel method has been added to each of the {@link java.io.FileInputStream#getChannel FileInputStream}, {@link java.io.FileOutputStream#getChannel FileOutputStream}, and {@link java.io.RandomAccessFile#getChannel RandomAccessFile} classes of the {@link java.io java.io} package. Invoking this method upon an instance of one of these classes will return a file channel connected to the underlying file.

Multiplexed, non-blocking I/O

Description

{@link java.nio.channels.SelectableChannel} A channel that can be multiplexed
  {@link java.nio.channels.DatagramChannel} A channel for a {@link java.net.DatagramSocket java.net.DatagramSocket}
  {@link java.nio.channels.Pipe.SinkChannel} The write end of a pipe
  {@link java.nio.channels.Pipe.SourceChannel} The read end of a pipe
  {@link java.nio.channels.ServerSocketChannel}   A channel for a {@link java.net.ServerSocket java.net.ServerSocket}
  {@link java.nio.channels.SocketChannel} A channel for a {@link java.net.Socket java.net.Socket}
{@link java.nio.channels.Selector} A multiplexor of selectable channels
{@link java.nio.channels.SelectionKey} A token representing the registration
of a channel with a selector
{@link java.nio.channels.Pipe} Two channels that form a unidirectional pipe

Multiplexed, non-blocking I/O, which is much more scalable than thread-oriented, blocking I/O, is provided by selectors, selectable channels, and selection keys.

A selector is a multiplexor of selectable channels, which in turn are a special type of channel that can be put into non-blocking mode. To perform multiplexed I/O operations, one or more selectable channels are first created, put into non-blocking mode, and {@link java.nio.channels.SelectableChannel#register registered} with a selector. Registering a channel specifies the set of I/O operations that will be tested for readiness by the selector, and returns a selection key that represents the registration.

Once some channels have been registered with a selector, a selection operation can be performed in order to discover which channels, if any, have become ready to perform one or more of the operations in which interest was previously declared. If a channel is ready then the key returned when it was registered will be added to the selector's selected-key set. The key set, and the keys within it, can be examined in order to determine the operations for which each channel is ready. From each key one can retrieve the corresponding channel in order to perform whatever I/O operations are required.

That a selection key indicates that its channel is ready for some operation is a hint, but not a guarantee, that such an operation can be performed by a thread without causing the thread to block. It is imperative that code that performs multiplexed I/O be written so as to ignore these hints when they prove to be incorrect.

This package defines selectable-channel classes corresponding to the {@link java.net.DatagramSocket}, {@link java.net.ServerSocket}, and {@link java.net.Socket} classes defined in the {@link java.net} package. Minor changes to these classes have been made in order to support sockets that are associated with channels. This package also defines a simple class that implements unidirectional pipes. In all cases, a new selectable channel is created by invoking the static open method of the corresponding class. If a channel needs an associated socket then a socket will be created as a side effect of this operation.

The implementation of selectors, selectable channels, and selection keys can be replaced by "plugging in" an alternative definition or instance of the {@link java.nio.channels.spi.SelectorProvider} class defined in the {@link java.nio.channels.spi} package. It is not expected that many developers will actually make use of this facility; it is provided primarily so that sophisticated users can take advantage of operating-system-specific I/O-multiplexing mechanisms when very high performance is required.

Much of the bookkeeping and synchronization required to implement the multiplexed-I/O abstractions is performed by the {@link java.nio.channels.spi.AbstractInterruptibleChannel}, {@link java.nio.channels.spi.AbstractSelectableChannel}, {@link java.nio.channels.spi.AbstractSelectionKey}, and {@link java.nio.channels.spi.AbstractSelector} classes in the {@link java.nio.channels.spi} package. When defining a custom selector provider, only the {@link java.nio.channels.spi.AbstractSelector} and {@link java.nio.channels.spi.AbstractSelectionKey} classes should be subclassed directly; custom channel classes should extend the appropriate {@link java.nio.channels.SelectableChannel} subclasses defined in this package.

Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a {@link java.lang.NullPointerException NullPointerException} to be thrown. @since 1.4 @version 1.10, 07/05/05 @author Mark Reinhold @author JSR-51 Expert Group

java.nio.channels.spi Service-provider classes for the {@link java.nio.channels} package.

Only developers who are defining new selector providers should need to make direct use of this package.

Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a {@link java.lang.NullPointerException NullPointerException} to be thrown. @since 1.4 @version 1.9, 07/05/05 @author Mark Reinhold @author JSR-51 Expert Group

java.nio.charset Defines charsets, decoders, and encoders, for translating between bytes and Unicode characters.

Class name

Description

{@link java.nio.charset.Charset} A named mapping between characters
and bytes
{@link java.nio.charset.CharsetDecoder} Decodes bytes into characters
{@link java.nio.charset.CharsetEncoder}   Encodes characters into bytes
{@link java.nio.charset.CoderResult}   Describes coder results
{@link java.nio.charset.CodingErrorAction}   Describes actions to take when
coding errors are detected

A charset is named mapping between sequences of sixteen-bit Unicode characters and sequences of bytes, in the sense defined in RFC 2278. A decoder is an engine which transforms bytes in a specific charset into characters, and an encoder is an engine which transforms characters into bytes. Encoders and decoders operate on byte and character buffers. They are collectively referred to as coders.

The {@link java.nio.charset.Charset} class defines methods for creating coders for a given charset and for retrieving the various names associated with a charset. It also defines static methods for testing whether a particular charset is supported, for locating charset instances by name, and for constructing a map that contains every charset for which support is available in the current Java virtual machine.

Most users will not use these classes directly; instead they will use the existing charset-related constructors and methods in the {@link java.lang.String} class, together with the existing {@link java.io.InputStreamReader} and {@link java.io.OutputStreamWriter} classes, all of whose implementations have been reworked to make use of the charset facilities defined in this package. A small number of changes have been made to the {@link java.io.InputStreamReader} and {@link java.io.OutputStreamWriter} classes in order to allow explicit charset objects to be specified in the construction of instances of those classes.

Support for new charsets can be made available via the interface defined in the {@link java.nio.charset.spi.CharsetProvider} class in the {@link java.nio.charset.spi} package.

Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a {@link java.lang.NullPointerException NullPointerException} to be thrown. @since 1.4 @version 1.8, 07/05/05 @author Mark Reinhold @author JSR-51 Expert Group

java.nio.charset.spi Service-provider classes for the {@link java.nio.charset} package.

Only developers who are defining new charsets should need to make direct use of this package.

Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a {@link java.lang.NullPointerException NullPointerException} to be thrown. @since 1.4 @version 1.6, 07/05/05 @author Mark Reinhold @author JSR-51 Expert Group

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.