// file path should be specific since the different file path determine whether your system support direct io
public static directiolib directiolib = directiolib.getlibforpath("/");
// you should always write into your disk the integer-multiple of block size through direct io.
// in most system, the block size is 4kb
private static final int block_size = 4 * 1024;
direct io 写
private static void write() throws ioexception {
if (directiolib.binit) {
bytebuffer bytebuffer = directioutils.allocatefordirectio(directiolib, 4 * block_size);
for (int i = 0; i < block_size; i++) {
bytebuffer.putint(i);
}
bytebuffer.flip();
directrandomaccessfile directrandomaccessfile = new directrandomaccessfile(new file("./database.data"), "rw");
directrandomaccessfile.write(bytebuffer, 0);
} else {
throw new runtimeexception("your system do not support direct io");
}
}
direct io 读
public static void read() throws ioexception {
if (directiolib.binit) {
bytebuffer bytebuffer = directioutils.allocatefordirectio(directiolib, 4 * block_size);
directrandomaccessfile directrandomaccessfile = new directrandomaccessfile(new file("./database.data"), "rw");
directrandomaccessfile.read(bytebuffer, 0);
bytebuffer.flip();
for (int i = 0; i < block_size; i++) {
system.out.print(bytebuffer.getint() + " ");
}
} else {
throw new runtimeexception("your system do not support direct io");
}
}