1

Is there any way of reliably "allocating" (reserving) hard disk space via "standard" Java (J2SE 5 or later)?

Take for example the case of a multithreaded application, executing in a thread pool, where every thread downloads files. How can the application make sure that its download won't be interrupted as a result of disk space exhaustion?

At least, if it knows beforehand the size of the file it is downloading, can it do some sort of "reservation", which would guarantee file download, irrespective of what the other threads are doing?

(There is a similar question in StackOverflow, but it does not discuss multithreading and also uses NIO.)

EDIT: After some further testing, the solution proposed on the similar question does not seem to work, as one can set ANY allowed length via the suggested RandomAccessFile approach, irrespective of the underlying hard disk space. For example, on a partition with only a few gigabytes available, I was able to create TB (terrabyte!) files at will.

The solution would have been sufficient if the getFreeSpace() method of the File class reported a decreased amount of available space every time one created a new file, but it actually doesn't, thus confirming the zero length which, in practice, these files seem to have.

These are at least the results I am seeing on a CentOS 5.6 virtual machine, running in VMWare Player 4.0.0.

Community
  • 1
  • 1
PNS
  • 19,295
  • 32
  • 96
  • 143
  • 3
    What's wrong with the answer to the "similar question" you linked? It will work just as well in a multi-threaded situation. You should be able to adapt the principles in that answer to your use-case. – Jim Garrison Oct 17 '11 at 20:11
  • Basically the same, but the answers in the other question don't necessarily reserve space. They might create a sparse file, depending on system behavior, and that wouldn't have all the space preallocated to it. You might need to write dummy bytes out (maybe 1 per block for speed). – Andrew Janke Oct 17 '11 at 20:53
  • 1
    possible duplicate of [Create file with given size in Java](http://stackoverflow.com/questions/245251/create-file-with-given-size-in-java) – user207421 Oct 18 '11 at 00:52
  • From the multithreading and the NIO perspectives, it doesn't seem as a duplicate to me, especially as I was looking for something doable with the "old" I/O API. Besides, I wouldn't have spent time typing this question, just to point to the "original" one. :-) – PNS Oct 18 '11 at 01:20
  • An answer with the old API is given in the thread. Multithreading has no apparent relevance. – user207421 Oct 18 '11 at 01:40
  • This does not seem to be a duplicate, simply because the other question is "file with size X" whereas this is asking to "reserve X bytes on the file system". Those are actually quite different things - I can easily make a 100TB file with the solutions from the other question, but that would be of no help here as I cannot possibly store such a large file... – Steven Schlansker Oct 21 '11 at 07:53
  • @Steven Schlansker It answers the OP's third paragraph. Pre-extension of a file is the only reservation mechanism for disk space that I have ever seen on any operating system in 35 years. Your final sentence seems self-contradictory to me, and also irrelevant. – user207421 Oct 21 '11 at 23:05

1 Answers1

1

Write zeros to the file. That will ensure you have allocated disk space (unless drive compression or some other variable-size encoding of the file is in use).

You might get away with writing a single zero for every block, but determining the blocksize may not be trivial.

This is to avoid the creation of a sparse file which does not have all your space allocated.

Steven Schlansker
  • 37,580
  • 14
  • 81
  • 100
  • This is my conclusion, as well. Any ideas as to how one could use that space for progressively storing (overwriting) a file over its area? – PNS Oct 21 '11 at 21:30