New I/O APIs |
Documentation Contents |
The new I/O (NIO) APIs introduced in v 1.4 provide new features and improved performance in the areas of buffer management, scalable network and file I/O, character-set support, and regular-expression matching. The NIO APIs supplement the I/O facilities in thejava.io
package.Features
The NIO APIs include the following features:
- Buffers for data of primitive types
- Character-set encoders and decoders
- A pattern-matching facility based on Perl-style regular expressions
- Channels, a new primitive I/O abstraction
- A file interface that supports locks and memory mapping
- A multiplexed, non-blocking I/O facility for writing scalable servers
Enhancements
J2SE 5.0 Enhancement - the JDK introduced a new class namedjavax.net.ssl.SSLEngine
. The existing J2SDK 1.4.0 secure communication infrastructure,javax.net.ssl.SSLSocket
, was based on the I/O model ofjava.net.Socket
. Thejavax.net.ssl.SSLEngine
class abstracts away the I/O model, and leaves developers free to secure communication pathways using alternate types of I/O.SSLEngine
can be combined with the New I/O APIs to create implementations such as secure blocking or non-blockingSocketChannel
s. For details see the J2SE 5.0 Enhancements.Examples
API References
New packages, classes, and interfaces:
java.nio
package: Buffers, which are used throughout the NIO APIs.java.nio.channels
package: Channels and selectors.java.nio.charset
package: Character encodings.java.nio.channels.spi
package: Service-provider classes for channels.java.nio.charset.spi
package: Service-provider classes for charsets.java.util.regex
package: Classes for matching character sequences against patterns specified by regular expressions.java.lang.CharSequence
interface: The interface implemented by objects that can be passed as arguments to methods in thejava.util.regex
package. TheString
,StringBuffer
, andjava.nio.CharBuffer
classes implement this interface.Note: The new
InetSocketAddress
andSocketAddress
classes, while not strictly part of the NIO APIs, are used by them.Revised classes and interfaces:
- In the
FileInputStream
andFileOutputStream
classes,getChannel
methods have been added to return the underlyingFileChannel
object, and theclose
methods have been revised to close the underlying channel.- Similarly, in the
RandomAccessFile
class, agetChannel
method has been added and theclose
method has been revised to close the underlying channel. TheRandomAccessFile
constructors now support the mode characters "s" and "d" to allow the specification, at file-open time, of synchronous writes or synchronous-data writes.- In the
InputStreamReader
andOutputStreamWriter
classes, constructors have been added that take a stream and aCharset
object, and thegetEncoding
methods have been revised to return the historical name of the encoding being used. Constructors toInputStreamReader
andOutputStreamWriter
have been added that take existingCharsetDecoder
andCharsetEncoder
objects, respectively.- In the
String
class, twosplit
methods are new. ThesubSequence
method has been added, as required by theCharSequence
interface thatString
now implements. Three additional methods have been added:matches
,replaceAll
, andreplaceFirst
.- In the
StringBuffer
class, thesubSequence
method has been added, as required by theCharSequence
interface thatStringBuffer
now implements.- In the
Thread
class, theinterrupt
method has been revised to interrupt threads blocked in channel-I/O operations.- In the
DatagramSocket
class, thereceive
andsend
methods have been revised to throw ajava.nio.channels.IllegalBlockingModeException
if the socket has a channel and the channel is in non-blocking mode. Theclose
method now closes the socket's channel, if it has one, and the newgetChannel
method returns the underlying channel, if any.- In the
ServerSocket
class, theaccept
andimplAccept
methods have been revised to throw ajava.nio.IllegalBlockingModeException
if the socket has a channel and the channel is in non-blocking mode. ThegetChannel
method has been added andclose
revised, for the same reasons as inDatagramSocket
.- In the
Socket
class, thegetInputStream
andgetOutputStream
methods have been revised to delegate all I/O operations to the socket's channel, if it has one. Theconnect
methods have been revised to throw ajava.nio.IllegalBlockingModeException
if the socket has a channel and the channel is in non-blocking mode. ThegetChannel
method has been added andclose
revised, for the same reasons as inDatagramSocket
.Three new functions have been added to the Java Native Interface (JNI) to support direct buffers. For information, see JNI Enhancements in v 1.4.
For More Information
- New I/O: Additional Documentation
- Supported Encodings
- JSR-51: New I/O APIs for the Java Platform (Note: v 1.4 has a subset of the changes proposed in this specification request.)
Copyright © 2002 Sun Microsystems, Inc. All Rights Reserved. Please send comments to: java-io@java.sun.com |
Java Software |