This post originated from an RSS feed registered with Java Buzz
by Janek Schwarz.
Original Post: Exception When Writing To A Full Disk? Not In Java!
Feed Title: The Wannabe Java Rockstar
Feed URL: http://weblog.janek.org/Archive/Categories/javablogs.rss.xml
Feed Description: The Wannabe Java Rockstar: Janek's weblog where all posts go to Eleven
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.