Java FileLock isFileLocked(File file)

Here you can find the source of isFileLocked(File file)

Description

is File Locked

License

Open Source License

Declaration

public static boolean isFileLocked(File file) 

Method Source Code


//package com.java2s;
/*/* w  w w .  j  a v  a2  s  .c om*/
 * Syncany, www.syncany.org
 * Copyright (C) 2011-2015 Philipp C. Heckel <philipp.heckel@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption;

import java.nio.file.Paths;

public class Main {
    public static boolean isFileLocked(File file) {
        if (!file.exists()) {
            return false;
        }

        if (file.isDirectory()) {
            return false;
        }

        if (isSymlink(file)) {
            return false;
        }

        RandomAccessFile randomAccessFile = null;
        boolean fileLocked = false;

        try {
            // Test 1 missing permissions or locked file parts. If File is not readable == locked
            randomAccessFile = new RandomAccessFile(file, "r");
            randomAccessFile.close();
        } catch (Exception e) {
            fileLocked = true;
        }

        if (!fileLocked && file.canWrite()) {
            try {
                // Test 2:Locked file parts
                randomAccessFile = new RandomAccessFile(file, "rw");

                // Test 3: Set lock and release it again
                FileLock fileLock = randomAccessFile.getChannel().tryLock();

                if (fileLock == null) {
                    fileLocked = true;
                } else {
                    try {
                        fileLock.release();
                    } catch (Exception e) { /* Nothing */
                    }
                }
            } catch (Exception e) {
                fileLocked = true;
            }

            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) { /* Nothing */
                }
            }
        }

        return fileLocked;
    }

    /**
     * Replaces the <tt>exists()</tt> method in the <tt>File</tt> class by taking symlinks into account.
     * The method returns <tt>true</tt> if the file exists, <tt>false</tt> otherwise.
     *
     * <p>Note: The method returns <tt>true</tt>, if a symlink exists, but points to a
     * non-existing target. This behavior is different from the classic
     * {@link #exists(File) exists()}-method in the <tt>File</tt> class.
     *
     * @param file A file
     * @return Returns <tt>true</tt> if a file exists (even if its symlink target does not), <tt>false</tt> otherwise
     */
    public static boolean exists(File file) {
        try {
            return Files.exists(Paths.get(file.getAbsolutePath()), LinkOption.NOFOLLOW_LINKS);
        } catch (InvalidPathException e) {
            return false;
        }
    }

    public static boolean isDirectory(File file) {
        try {
            return Files.isDirectory(Paths.get(file.getAbsolutePath()), LinkOption.NOFOLLOW_LINKS);
        } catch (InvalidPathException e) {
            return false;
        }
    }

    public static boolean isSymlink(File file) {
        return Files.isSymbolicLink(Paths.get(file.getAbsolutePath()));
    }
}

Related

  1. isBlocking(Channel channel)
  2. isFileLocked(File file)
  3. isLocked(File file)
  4. isLocked(File file)
  5. lock(File file)
  6. lockFile(File file)