Search in sources :

Example 16 with FileLock

use of java.nio.channels.FileLock in project robovm by robovm.

the class FileChannelLockingTest method test_illegalLockParameters.

public void test_illegalLockParameters() throws IOException {
    // Cannot lock negative positions
    try {
        readOnlyChannel.lock(-1, 10, true);
        fail("Passing illegal args to lock should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    try {
        writeOnlyChannel.lock(-1, 10, false);
        fail("Passing illegal args to lock should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    try {
        readWriteChannel.lock(-1, 10, false);
        fail("Passing illegal args to lock should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    // Lock a range at the front, shared.
    FileLock flock1 = readWriteChannel.lock(22, 110, true);
    // Try to acquire an overlapping lock.
    try {
        readWriteChannel.lock(75, 210, true);
    } catch (OverlappingFileLockException exception) {
        // expected
        flock1.release();
    }
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 17 with FileLock

use of java.nio.channels.FileLock in project robovm by robovm.

the class FileChannelLockingTest method test_tryLockLLZ.

public void test_tryLockLLZ() throws IOException {
    // It is illegal to request an exclusive lock on a read-only channel
    try {
        readOnlyChannel.tryLock(0, 99, false);
        fail("Acquiring exclusive lock on read-only channel should fail");
    } catch (NonWritableChannelException ex) {
    // Expected
    }
    // It is invalid to request a lock starting before the file start
    try {
        readOnlyChannel.tryLock(-99, 0, true);
        fail("Acquiring an illegal lock value should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    // Acquire a valid lock
    FileLock tmpLock = readOnlyChannel.tryLock(0, 10, true);
    assertTrue(tmpLock.isValid());
    tmpLock.release();
    // Acquire another valid lock -- and don't release it yet
    FileLock lock = readOnlyChannel.tryLock(10, 788, true);
    assertTrue(lock.isValid());
    // Overlapping locks are illegal
    try {
        readOnlyChannel.tryLock(1, 23, true);
        fail("Acquiring an overlapping lock should fail.");
    } catch (OverlappingFileLockException ex) {
    // Expected
    }
    // Adjacent locks are legal
    FileLock adjacentLock = readOnlyChannel.tryLock(1, 3, true);
    assertTrue(adjacentLock.isValid());
    adjacentLock.release();
    // Release longer lived lock
    lock.release();
}
Also used : FileLock(java.nio.channels.FileLock) NonWritableChannelException(java.nio.channels.NonWritableChannelException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 18 with FileLock

use of java.nio.channels.FileLock in project robovm by robovm.

the class FileChannelImpl method addLock.

/**
     * Add a new pending lock to the manager. Throws an exception if the lock
     * would overlap an existing lock. Once the lock is acquired it remains in
     * this set as an acquired lock.
     */
private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
    long lockEnd = lock.position() + lock.size();
    for (FileLock existingLock : locks) {
        if (existingLock.position() > lockEnd) {
            // cannot overlap).
            break;
        }
        if (existingLock.overlaps(lock.position(), lock.size())) {
            throw new OverlappingFileLockException();
        }
    }
    locks.add(lock);
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 19 with FileLock

use of java.nio.channels.FileLock in project helios by spotify.

the class JobPrefixFile method tryFromExistingFile.

/**
   * Attempts to lock the given file, and create a JobPrefixFile for it. A new JobPrefixFile
   * instance will be returned if a lock can be obtained for the file. Null will be returned if the
   * lock is already held by either this process or another. For all other cases, an exception will
   * be thrown.
   * @param file the path to the file
   * @return a new JobPrefixFile if a file lock can be obtained. Null if a lock for the file is
   *         already held by either this process or another.
   */
public static JobPrefixFile tryFromExistingFile(final Path file) throws IOException {
    Preconditions.checkNotNull(file);
    final FileChannel channel = FileChannel.open(file, WRITE);
    final FileLock lock;
    try {
        // We want to return JobPrefixFile if we can obtain the lock, null if someone already holds
        // the lock, and throw an exception in all other cases. tryLock makes this a little tricky.
        // It's behavior this:
        //   - returns a FileLock if one can be obtained
        //   - returns null if another process holds the lock
        //   - throws OverlappingFileLockException if the lock is already held by this process
        //   - throws a various exceptions for other errors
        lock = channel.tryLock();
    } catch (OverlappingFileLockException e) {
        // If the lock is already held by this process, close the channel and return null.
        close(channel);
        return null;
    } catch (Exception e) {
        // If an unexpected error occurred, close the channel and rethrow.
        close(channel);
        throw e;
    }
    // If another process hold the lock, close the channel and return null.
    if (lock == null) {
        close(channel);
        return null;
    }
    // If we've obtained the lock, return a new JobPrefixFile
    return new JobPrefixFile(file, channel, lock);
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) IOException(java.io.IOException)

Example 20 with FileLock

use of java.nio.channels.FileLock in project platform_frameworks_base by android.

the class MiniThumbFile method getMagic.

// Get the magic number for the specified id in the mini-thumb file.
// Returns 0 if the magic is not available.
public synchronized long getMagic(long id) {
    // check the mini thumb file for the right data.  Right is
    // defined as having the right magic number at the offset
    // reserved for this "id".
    RandomAccessFile r = miniThumbDataFile();
    if (r != null) {
        long pos = id * BYTES_PER_MINTHUMB;
        FileLock lock = null;
        try {
            mBuffer.clear();
            mBuffer.limit(1 + 8);
            lock = mChannel.lock(pos, 1 + 8, true);
            // (1 for the "status" and 8 for the long)
            if (mChannel.read(mBuffer, pos) == 9) {
                mBuffer.position(0);
                if (mBuffer.get() == 1) {
                    return mBuffer.getLong();
                }
            }
        } catch (IOException ex) {
            Log.v(TAG, "Got exception checking file magic: ", ex);
        } catch (RuntimeException ex) {
            // Other NIO related exception like disk full, read only channel..etc
            Log.e(TAG, "Got exception when reading magic, id = " + id + ", disk full or mount read-only? " + ex.getClass());
        } finally {
            try {
                if (lock != null)
                    lock.release();
            } catch (IOException ex) {
            // ignore it.
            }
        }
    }
    return 0;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Aggregations

FileLock (java.nio.channels.FileLock)130 RandomAccessFile (java.io.RandomAccessFile)69 IOException (java.io.IOException)68 File (java.io.File)44 FileChannel (java.nio.channels.FileChannel)33 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)22 Test (org.junit.Test)12 FileOutputStream (java.io.FileOutputStream)10 FileInputStream (java.io.FileInputStream)5 ByteBuffer (java.nio.ByteBuffer)4 NonWritableChannelException (java.nio.channels.NonWritableChannelException)4 FileNotFoundException (java.io.FileNotFoundException)3 NonReadableChannelException (java.nio.channels.NonReadableChannelException)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 Path (java.nio.file.Path)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 Properties (java.util.Properties)3 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)2 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)2 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)2