Chapter 14. Buffers

Traditional synchronous I/O is designed for traditional applications. Such applications have the following characteristics:

  • Files may be large but not huge. It’s possible to read an entire file into memory.

  • An application reads from or writes to only a few files or network connections at the same time, ideally using only one stream at a time.

  • The application is sequential. It won’t be able to do much until it’s finished reading or writing a file.

As long as these characteristics hold, stream-based I/O is reasonably quick and operates fairly efficiently. However, if these prerequisites are violated, the standard I/O model begins to show some weaknesses. For example, web servers often need to service hundreds or thousands of connections simultaneously. Scientific, engineering, and multimedia applications often need to manipulate datasets that are gigabytes in size.

Java 1.4 introduced a new model for I/O that is designed more for these sorts of applications and less for the more traditional applications that don’t have to do so much I/O. The classes that make up this new I/O library are all found in the java.nio package and its subpackages. The new I/O model does not replace traditional, stream-based I/O. Indeed, several parts of the new I/O API are based on streams. However, the new I/O model is much more efficient for certain types of I/O-bound applications.

Whereas the traditional I/O model is based on streams, the new I/O model is based on buffers and channels. A ...

Get Java I/O, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.