Child pages
  • Asynchronous File IO in Java
Skip to end of metadata
Go to start of metadata

Asynchronous File I/O in Java

Asynchronous File I/O in Java is a framework I created because I got tired of waiting for JSR 203 and IBM's AIO4J doesn't have support for asynchronous file access with Linux. The framework is still in very early development. It currently only supports Linux. The API is in flux. And the framework hasn't received a great deal of testing. I expect all this to change for the better in the near future. I've make the project available so that others can help with testing and hopefully there are others out there who would like to contribute to the project. I will try to post examples and additional documentation here as the framework progresses.

The source code for the project can be found in my subversion sandbox as part of the Apache Mina project

To build the project use Maven 2 and run "mvn package". This will produce two artifacts. A Java .jar file containing the Java classes and which contains the the native binaries that make the POSIX AIO calls to do asynchronous file IO in Java.


Below is a simple example of how to use Asynchronous File I/O in Java.

import java.nio.ByteBuffer;

import org.apache.aio.AioFutureListener;
import org.apache.aio.AioFutureReadWrite;
import org.apache.aio.AsynchronousFileChannel;

public class AIOTest {

	public static void main(String[] args) throws Exception {
		FileInputStream in = new FileInputStream("/etc/passwd");

		AsynchronousFileChannel achannel = new AsynchronousFileChannel(in.getFD());
		AioFutureListener<AioFutureReadWrite> listener = new AioFutureListener<AioFutureReadWrite>() {
					public void onCompletion(AioFutureReadWrite ioFuture) {
						System.out.println("In callback");
						byte[] data = new byte[ioFuture.getBuffer().limit() - ioFuture.getBuffer().position()];
						System.out.println("  Buffer contains: " + new String(data));
		ByteBuffer buffer = ByteBuffer.allocateDirect(4096);
		AioFutureReadWrite future =, 0);

Linux Only

Asynchronous File I/O in Java currently only works with the Linux operating system. I use Ubuntu 6.10 and haven't tested the code on other distros yet. If others would like to port the code to other operating systems and/or make sure the code works on other distributions of Linux, that would be a huge help!

  • No labels

1 Comment

  1. After my initial post to the mina-dev list about aioj, someone forwarded my post to Alan Bateman, the spec lead for JSR-203. He replied to me with the following:

    I work in Sun's Java SE team and I'm currently leading JSR-203 that is
    defining the next phase of NIO. We started on this JSR just under a year
    ago (although this JSR had been submitted ages ago and long before I got
    involved in NIO). Just so you know, we have defined an asynchronous I/O
    API in this JSR. I am hoping that we will publish a draft soon. In
    addition to asynchronous I/O we also have a new file system API and
    other topics carried over from the original NIO JSR (called JSR-51).
    Your mail mentions file I/O and we have an AsynchronousFileChannel that
    is an asynchronous form of FileChannel. This may or may not be
    interesting to your project but when we publish our API perhaps you
    might get time to review it and send feedback.

    This is very good news! Apparently JSR 203 won't get pushed off again and will see the light of day in Java 7. I'm hoping to model my asynchronous file API around the JSR 203 API. It would be ideal to have something that's as compatible as possible with what's going to be released in Java 7 that we can use now with Java 5 and 6.