Java Code Examples for java.nio.channels.FileLock#close()

The following examples show how to use java.nio.channels.FileLock#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtil.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
/**
 * Returns if the given {@code file } is used by any process (i.e.
 * has the file open for reading or writing).
 *
 * @param    file to check
 * @return   if the given {@code file } is used by any process
 */
// Todo: Here is a candidate for Windows. Verify that it works
private static FileState stateWindows(@NotNull File file) {
    if (!file.exists()) return FileState.NON_EXISTENT;
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
         FileChannel fileChannel = randomAccessFile.getChannel()) {

        final FileLock fileLock = fileChannel.tryLock();
        if (fileLock != null) {
            fileLock.close();
            return FileState.CLOSED;
        }
        return FileState.OPEN;
    } catch (IOException ignored) {
        // Do nothing
    }
    return FileState.UNDETERMINED;
}
 
Example 2
Source File: StreamerUtils.java    From WandFix with MIT License 5 votes vote down vote up
public static void safeClose(FileLock closeable) {
    if (closeable != null) {
        try {
            if (Build.VERSION.SDK_INT > 18) {
                closeable.close();
            } else {
                closeable.release();
            }
        } catch (Throwable e) {
        }
    }
}
 
Example 3
Source File: RootDirectorySystem.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
synchronized boolean isFree()
{
  if (_lock != null) {
    return false;
  }
  
  if (! Files.isReadable(_path)) {
    return true;
  }
  
  try (FileOutputStream fOs = (FileOutputStream) Files.newOutputStream(_path)) {
    FileLock lock = fOs.getChannel().tryLock();
    
    if (lock != null) {
      lock.close();
      
      return true;
    }
    else {
      return false;
    }
  } catch (Exception e) {
    log.finer(_path + ": isFree " + e);
    log.log(Level.FINEST, e.toString(), e);

    return true;
  }
}
 
Example 4
Source File: TempFileManager.java    From pumpernickel with MIT License 5 votes vote down vote up
private static boolean isLocked(File file) {
	try {
		FileLock lock = IOUtils.getFileLock(file, true);
		if (lock == null)
			return true;
		lock.close();
		return false;
	} catch (IOException e) {
		return true;
	}
}
 
Example 5
Source File: FileChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void useFileChannel() throws IOException {
  File file = File.createTempFile("j2objc", "tmp");
  ChannelGetter channelGetter = new ChannelGetter();

  channelGetter.createChannel(file);
  // The FileOutputStream used to create the channel is released at this point.

  FileChannel channel = channelGetter.get();
  FileLock lock = channel.lock();
  lock.close();
  channel.close();
  assertTrue(file.delete());
}
 
Example 6
Source File: CleanupUnusedQueueFilesTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
boolean isNotOpenByAnyProcess(@NotNull File file) {
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
         FileChannel fileChannel = randomAccessFile.getChannel()) {

        final FileLock fileLock = fileChannel.tryLock();
        if (fileLock != null) {
            fileLock.close();
            return true;
        }
    } catch (IOException ignored) {
    }
    return false;
}