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();
}
}
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();
}
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);
}
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);
}
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;
}
Aggregations