2008. 7. 25. 13:10

New I/O APIs

Java

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 the java.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

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 the java.util.regex package. The String, StringBuffer, and java.nio.CharBuffer classes implement this interface.

Note: The new InetSocketAddress and SocketAddress classes, while not strictly part of the NIO APIs, are used by them.

Revised classes and interfaces:

  • In the FileInputStream and FileOutputStream classes, getChannel methods have been added to return the underlying FileChannel object, and the close methods have been revised to close the underlying channel.
  • Similarly, in the RandomAccessFile class, a getChannel method has been added and the close method has been revised to close the underlying channel. The RandomAccessFile 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 and OutputStreamWriter classes, constructors have been added that take a stream and a Charset object, and the getEncoding methods have been revised to return the historical name of the encoding being used. Constructors to InputStreamReader and OutputStreamWriter have been added that take existing CharsetDecoder and CharsetEncoder objects, respectively.
  • In the String class, two split methods are new. The subSequence method has been added, as required by the CharSequence interface that String now implements. Three additional methods have been added: matches, replaceAll, and replaceFirst.
  • In the StringBuffer class, the subSequence method has been added, as required by the CharSequence interface that StringBuffer now implements.
  • In the Thread class, the interrupt method has been revised to interrupt threads blocked in channel-I/O operations.
  • In the DatagramSocket class, the receive and send methods have been revised to throw a java.nio.channels.IllegalBlockingModeException if the socket has a channel and the channel is in non-blocking mode. The close method now closes the socket's channel, if it has one, and the new getChannel method returns the underlying channel, if any.
  • In the ServerSocket class, the accept and implAccept methods have been revised to throw a java.nio.IllegalBlockingModeException if the socket has a channel and the channel is in non-blocking mode. The getChannel method has been added and close revised, for the same reasons as in DatagramSocket.
  • In the Socket class, the getInputStream and getOutputStream methods have been revised to delegate all I/O operations to the socket's channel, if it has one. The connect methods have been revised to throw a java.nio.IllegalBlockingModeException if the socket has a channel and the channel is in non-blocking mode. The getChannel method has been added and close revised, for the same reasons as in DatagramSocket.

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


Copyright © 2002 Sun Microsystems, Inc. All Rights Reserved.

Please send comments to: java-io@java.sun.com
Sun
Java Software