0

I would like to write a file that comes from over a network, so I don't know the size of the file that's comming in. Sometimes the disk on the file server might get filled up and I would like return a message to my client notifying them of this error. I couldn't find any documentation on being able to catch this type of i/o error. FileChannel streams bytes from memory to disk, so it may not be trivial to detect this. Is the saving happening asynchronously? Is it possible to detect disk full?

// Create a new file to write to
RandomAccessFile mFile = new RandomAccessFile(this.mFilePath, "rw");
FileChannel mFileChannel = this.mFile.getChannel();

// wrappedBuffer has my file in it
ByteBuffer wrappedBuffer = ByteBuffer.wrap(fileBuffer);
while(wrappedBuffer.hasRemaining()) {
    bytesWritten += this.mFileChannel.write(wrappedBuffer, this.mBytesProcessed);
}

I figured in the File class, we can do something like this:

// if there is less than 1 mb left on disk
new File(this.mFilePath, "r").getUsableSpace() < 1024; 

But if there a way to throw an except if this.mFileChannel.write() fails because the disk is full?

kenny
  • 75
  • 1
  • 6
  • I'm pretty sure you get a generic `IOException` on disk full. The easiest thing to do would be to try it and see what happens. – Jim Garrison Mar 14 '16 at 18:11
  • 1
    @JimGarrison Ok, so the exception says "There is not enough space on the disk" – kenny Mar 14 '16 at 18:33
  • 1
    That's just the exception message. It doesn't have any special type, and it is not recommended to try and parse the message text. Why not inform your client that there was an I/O exception, and give him the remaining space on the disk, and let him decide if it makes sense that the issue is that the disk is full? – RealSkeptic Mar 14 '16 at 18:55

1 Answers1

1

Even if it's not recommended to parse the error message you could do something like this :

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;

public class SmallDisk {

    final static String SMALL_DISK_PATH = "/Volumes/smallDisk";

    final static Pattern NO_SPACE_LEFT = Pattern.compile(": No space left on device$");

    public static void main(String[] args) throws NoSpaceException {
        Path p = Paths.get(SMALL_DISK_PATH);
        FileStore fs = null;
        try {
            fs = Files.getFileStore(p);
            System.out.println(fs.getUsableSpace());
            Path newFile = Paths.get(SMALL_DISK_PATH + "/newFile");
            Files.createFile(newFile);

        } catch (FileSystemException e) {
            //We catch the "No space left on device" from the FileSystemException and propagate it
            if(NO_SPACE_LEFT.matcher(e.getMessage()).find()){
                throw new NoSpaceException("Not enough space");
            }
            //Propagate exception or deal with it here
        } catch (IOException e) {
            //Propagate exception or deal with it here
        }

    }

    public static class NoSpaceException extends IOException{

        public NoSpaceException(String message) {
            super(message);
        }
    }
}

Another way but it doesn't guaranty that you won't have exception is to use the FileStore to check that you enough space before you write (not enough if you are working with a shared folder or multi threads software)

Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31