Exception When Writing To A Full Disk? Not In Java!

Have you ever hoped to get an exception when writing to a full disk in Java? Get ready to be disappointed.

Execute this code on a full disk (on Linux/Unix, you can simulate it with hard quota):

FileOutputStream ostream = new FileOutputStream("out");
ostream.write
("hello".getBytes());
ostream.close
();

No exception is thrown. All you get is a zero-byte-sized file. I don't know about you, but this is not what I expected.

After hours of googling and searching the bug parade, I found a workaround based on FileDescriptor. Every FileOutputStream has an associated file descriptor. With it's sync()-method, one can force all system buffers to synchronize with the underlying device. The following code -- executed on a full disk -- raises a SyncFailedException:

FileOutputStream ostream = new FileOutputStream("out");
ostream.write
("hello".getBytes());
ostream.getFD
().sync();
ostream.close
();

I don't know the performance implications yet. But I'd rather suffer from a small performance loss than from data loss.

One thing, though, I have to admit: even if it makes my software look bad, bugs like 4338871 on bug parade have a certain beauty.

 

Fri, 17 Dec 2004, 22:57 | Java | PermaLink
« Rockstars vs. Unit Tests | Home | And Now To Something Stupid »