x
Loading
 Loading
Hello, Guest | Login | Register

Non-Blocking Connections

Last month’s column introduced java.nio, one of the most significant new packages found in Java 2 Standard Edition Version 1.4. java.nio finally adds high-performance I/O features — memory-mapped files, non-blocking I/O, and managed buffers — to Java.

Last month’s column introduced java.nio, one of the most significant new packages found in Java 2 Standard Edition Version 1.4. java.nio finally adds high-performance I/O features — memory-mapped files, non-blocking I/O, and managed buffers — to Java.

Last month’s column focused on Buffer, a new set of classes that holds data in linear, sequential storage. Buffer classes can be used to store and retrieve data without incurring the overhead of object allocation and garbage collection. However, a Buffer is useless if it isn’t connected to sources that add data to the Buffer and consumers that extract data. That’s where channels come in: the new java.nio .channels.* classes add data to Buffers and extract data from Buffers.

Changing to Channels

A Channel is simply a connection between a ByteBuffer and an endpoint, either a socket or file. Indeed, the four Channel classes are divided into two sets: SocketChannel, ServerSocketChannel, and DatagramChannel for sockets, and FileChannel for files.

While Channel is similar in purpose to Stream and Reader, Channel is not a replacement for those classes. Instead, Channel joins Stream and Reader as a fundamental Java capability.

In fact, in the case of files, you have to combine Stream and Channel to memory-map files and buffer I/O. Specifically (at least in 1.4), none of the classes in java.io can read from or write to a Buffer. Additionally, you cannot create a FileChannel directly. Instead, you have to combine classes from java.io and java.nio to create, open,…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace