As you all know Java has no facility to find out the space available on a volume.
I am writing code that enables a client to upload a file to the server but I need a method of checking the volume's free space to ensure that the file will fit when I write it from memory to disk.
I cannot use any native methods or anything non-platform independant (like Runtime.exec("df -k") etc.).
I would like to catch the fact that there is not enough space on the target volume before the client uploads and I try to write the data to file. I don't want the client to have to wait for upload / write IO to happen only to get an error because I am 1byte over the available volume size.
The only solutions I have come up with are:
1. Write a temporary file until the volume is full at regular intervals and keep a record of the amount of data successfully written so the server can take an educated guess at whether the upload will succeed when a client reports the size of the file it wants to upload.
2. Do 1. on demand which means a client wait while the file is written
3. Hope for the best, catch the exception if the write fails and report back to the client that their 1Gb upload was in vain.
All of these solutions are at best ugly and at worst just plain dumb so I am reaching out to the ranchers to see if anyone has any ideas about how to tackle this.
Thanks in advance.
p.s. I am assuming that Sun haven't added volume reporting in Tiger - I certainly couldn't find anything new to do that - but if so perhaps there is a solution already available.
Mmm. Seems to me unless you got find some way to yoke all file systems under one command structure or one common server-side standard for reporting, you're out of luck. Choice of application programming language has nothing to do with it -- it's a systems issue through and through.
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
There's no way to do that without a native call in Java 5.X or earlier.
Java 6.0 will require implementations to support storage device metrics and you can simply call File.getFreeSpace(). If you can live with a beta version of Java 6.0 you're home free, 'less they change it prior to the official release.
Can you 'reserve' space for the file just as the upload starts. So you know the size of the upload and then you create a zero-filled file of that size at the destination. Then you slowly overwrite it byte by byte as you upload. Is your data so large that this would be too slow?
posted 15 years ago
Thanks for your advice.
Michael: Mmm. Seems to me unless you got find some way to yoke all file systems under one command structure or one common server-side standard for reporting, you're out of luck
I could probably devise a platform dependant method for the big 4 OS's and then use a System property to determine which method to use. My concern would be that versioning of OS's as fluid as Linux have a habit of changing command output which would tie my application to a version specific command.
Rick: If you can live with a beta version
Unfortunately this is production so I have to stick with Tiger. It is good news that this is being added to Java 6 - it must be a very difficult problem to address for it to have been excluded from the standard libraries for all this time.
Stuart: Can you 'reserve' space for the file just as the upload starts. So you know the size of the upload and then you create a zero-filled file of that size at the destination. Then you slowly overwrite it byte by byte as you upload. Is your data so large that this would be too slow?
Yeah, this was what I was thinking with 2. I guess this is going to be my best bet at the moment - the overhead of writing the junk file will probably be the minimum I can achieve.