use of java.nio.channels.FileLock in project android_frameworks_base by ResurrectionRemix.
the class MiniThumbFile method getMiniThumbFromFile.
/**
* Gallery app can use this method to retrieve mini-thumbnail. Full size
* images share the same IDs with their corresponding thumbnails.
*
* @param id the ID of the image (same of full size image).
* @param data the buffer to store mini-thumbnail.
*/
public synchronized byte[] getMiniThumbFromFile(long id, byte[] data) {
RandomAccessFile r = miniThumbDataFile();
if (r == null)
return null;
long pos = getIndex(id, false);
if (pos < 0)
return null;
pos *= BYTES_PER_MINTHUMB;
FileLock lock = null;
try {
mBuffer.clear();
lock = mChannel.lock(pos, BYTES_PER_MINTHUMB, true);
int size = mChannel.read(mBuffer, pos);
if (size > 1 + 8 + 4) {
// flag, magic, length
mBuffer.position(0);
byte flag = mBuffer.get();
long magic = mBuffer.getLong();
int length = mBuffer.getInt();
if (size >= 1 + 8 + 4 + length && length != 0 && magic != 0 && flag == 1 && data.length >= length) {
mBuffer.get(data, 0, length);
return data;
}
}
} catch (IOException ex) {
Log.w(TAG, "got exception when reading thumbnail id=" + id + ", exception: " + ex);
} catch (RuntimeException ex) {
// Other NIO related exception like disk full, read only channel..etc
Log.e(TAG, "Got exception when reading thumbnail, id = " + id + ", disk full or mount read-only? " + ex.getClass());
} finally {
try {
if (lock != null)
lock.release();
} catch (IOException ex) {
// ignore it.
}
}
return null;
}
use of java.nio.channels.FileLock in project android_frameworks_base by ResurrectionRemix.
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 = getIndex(id, false);
if (pos < 0)
return 0;
pos *= 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;
}
use of java.nio.channels.FileLock in project ats-framework by Axway.
the class LocalFileSystemOperations method lockFile.
/**
* <pre>
* Acquires an exclusive lock on a file
*
* <b>Platform dependencies</b>
*
* - In Windows it works as expected
* - In Linux it depends on the locking mechanism of the system. The file locking types are two - advisory and mandatory:
*
* a) <b>Advisory locking</b> - advisory locking will work, only if the participating process are cooperative.
* Advisory locking sometimes also called as "unenforced" locking.
*
* b) <b>Mandatory locking</b> - mandatory locking doesn’t require cooperation from the participating processes.
* It causes the kernel to check every open, read and write to verify that the calling process isn’t
* violating a lock on the given file. To enable mandatory locking in Linux, you need to enable it on
* a file system level and also on the individual files. The steps to be followed are:
* 1. Mount the file system with "<i>-o mand</i>" option
* 2. For the lock_file, turn on the set-group-ID bit and turn off the group-execute bit, to enable
* mandatory locking on that particular file. (This way has been chosen because when you turn off
* the group-execute bit, set-group-ID has no real meaning to it )
*
* How to do mandatory locking:
* Note: You need to be root to execute the below command
* <i># mount -oremount,mand /</i>
* <i># touch mandatory.txt</i>
* <i># chmod g+s,g-x mandatory.txt</i>
* </pre>
*
* @param fileName file name
*/
@Override
public void lockFile(String fileName) {
synchronized (lockedFiles) {
if (lockedFiles.containsKey(fileName)) {
log.warn("File '" + fileName + "' is already locked");
} else {
try {
File fileToLock = new File(fileName);
@SuppressWarnings("resource") FileChannel //keep lock to the file
channel = new RandomAccessFile(fileToLock, "rw").getChannel();
FileLock fileLock = channel.lock();
lockedFiles.put(fileName, fileLock);
} catch (FileNotFoundException fnfe) {
throw new FileSystemOperationException("File '" + fileName + "' is not found", fnfe);
} catch (OverlappingFileLockException ofle) {
throw new FileSystemOperationException("File '" + fileName + "' is already locked in the current JVM" + ", but not from this class, so we can't unlock it later.", ofle);
} catch (Exception e) {
throw new FileSystemOperationException("Could not lock file '" + fileName + "'", e);
}
}
}
}
use of java.nio.channels.FileLock in project ats-framework by Axway.
the class LocalFileSystemOperations method unlockFile.
/**
* Unlock file already locked with {@link #lockFile(String) lockFile()} method
*
* @param fileName file name
*/
@Override
public void unlockFile(String fileName) {
synchronized (lockedFiles) {
FileLock fileLock = lockedFiles.get(fileName);
if (fileLock != null) {
try {
fileLock.release();
} catch (Exception e) {
throw new FileSystemOperationException("Could not unlock file '" + fileName + "'", e);
} finally {
IoUtils.closeStream(fileLock.channel());
lockedFiles.remove(fileName);
}
} else {
log.warn("File '" + fileName + "' is not locked, so we will not try to unlock it");
}
}
}
use of java.nio.channels.FileLock in project android_frameworks_base by DirtyUnicorns.
the class MiniThumbFile method getIndex.
/**
* Get the index of thumbnail, which is the real saving location.
* @param id the raw id in Mediaprovider database.
* @param create when you want to create a new thumbnail, set to true; when generally query
* thumbnail saved index, set to false.
*/
private long getIndex(long id, boolean create) {
RandomAccessFile r = miniThumbIndexFile();
ByteBuffer buf = ByteBuffer.allocateDirect(BYTES_PER_MINTHUMB_INDEX);
if (r != null) {
long pos = 0;
// (id+1)*BYTES_PER_MINTHUMB_INDEX.
if (!create) {
pos = (id + 1) * BYTES_PER_MINTHUMB_INDEX;
}
FileLock lock = null;
try {
buf.clear();
buf.limit(BYTES_PER_MINTHUMB_INDEX);
lock = mIndexChannel.lock(pos, BYTES_PER_MINTHUMB_INDEX, false);
//check that we can read the following 8 bytes
//which is the index position of thumbnail.
int read = mIndexChannel.read(buf, pos);
if (read == BYTES_PER_MINTHUMB_INDEX) {
buf.position(0);
if (create) {
//first, write next index.
long now = buf.getLong();
buf.clear();
buf.position(0);
buf.putLong(++now);
buf.flip();
int write = mIndexChannel.write(buf, pos);
//second, write this id's index
if (BYTES_PER_MINTHUMB_INDEX == write) {
if (lock != null)
lock.release();
pos = (id + 1) * BYTES_PER_MINTHUMB_INDEX;
lock = mIndexChannel.lock(pos, BYTES_PER_MINTHUMB_INDEX, false);
buf.flip();
write = mIndexChannel.write(buf, pos);
if (debug)
Log.d(TAG, "getIndex with create. index: " + now + "corresponding id: " + id + ", index is: " + pos);
}
return now;
} else {
long p = buf.getLong();
if (debug)
Log.d(TAG, "getIndex with no create. index: " + p);
return p;
}
} else if (-1 == read) {
//If the index file is empty, initialize first index to 0.
if (0 == r.length()) {
buf.clear();
buf.position(0);
buf.putLong(0);
buf.flip();
int write = mIndexChannel.write(buf, 0);
if (debug)
Log.d(TAG, "initialize first index");
if (BYTES_PER_MINTHUMB_INDEX == write)
return 0;
}
}
} catch (IOException ex) {
Log.e(TAG, "Got exception checking file index: ", ex);
} catch (RuntimeException ex) {
// Other NIO related exception like disk full, read only channel..etc
Log.e(TAG, "Got exception when reading index, id = " + id + ", disk full or mount read-only? " + ex.getClass());
} finally {
try {
if (lock != null)
lock.release();
} catch (IOException ex) {
// ignore it.
Log.e(TAG, "release lock: ", ex);
}
}
}
return 0;
}
Aggregations