use of java.nio.channels.FileLock in project android_frameworks_base by DirtyUnicorns.
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 DirtyUnicorns.
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 android_frameworks_base by DirtyUnicorns.
the class MiniThumbFile method saveMiniThumbToFile.
public synchronized void saveMiniThumbToFile(byte[] data, long id, long magic) throws IOException {
RandomAccessFile r = miniThumbDataFile();
if (r == null)
return;
long pos = getIndex(id, true);
if (pos < 0)
return;
pos *= BYTES_PER_MINTHUMB;
FileLock lock = null;
try {
if (data != null) {
if (data.length > BYTES_PER_MINTHUMB - HEADER_SIZE) {
// not enough space to store it.
return;
}
mBuffer.clear();
mBuffer.put((byte) 1);
mBuffer.putLong(magic);
mBuffer.putInt(data.length);
mBuffer.put(data);
mBuffer.flip();
lock = mChannel.lock(pos, BYTES_PER_MINTHUMB, false);
mChannel.write(mBuffer, pos);
}
} catch (IOException ex) {
Log.e(TAG, "couldn't save mini thumbnail data for " + id + "; ", ex);
throw ex;
} catch (RuntimeException ex) {
// Other NIO related exception like disk full, read only channel..etc
Log.e(TAG, "couldn't save mini thumbnail data for " + id + "; disk full or mount read-only? " + ex.getClass());
} finally {
try {
if (lock != null)
lock.release();
} catch (IOException ex) {
// ignore it.
}
}
}
use of java.nio.channels.FileLock in project azure-tools-for-java by Microsoft.
the class FileTokenCache method write.
private void write() throws IOException, InterruptedException {
RandomAccessFile out = new RandomAccessFile(filePath.toString(), "rw");
try {
// in case of multiprocess file access
FileLock lock = acquireLock(out);
if (lock != null) {
log.log(Level.FINEST, "Locking file cache for writing");
try {
byte[] data = serialize();
log.log(Level.FINEST, "Writing file...");
out.write(data);
} finally {
log.log(Level.FINEST, "Unocking file cache");
lock.release();
}
} else {
throw new IOException("Can't lock file token cache for writing");
}
} finally {
out.close();
}
}
use of java.nio.channels.FileLock in project azure-tools-for-java by Microsoft.
the class FileTokenCache method read.
private void read() throws IOException, InterruptedException {
RandomAccessFile in = new RandomAccessFile(filePath.toString(), "rw");
try {
// in case of multiprocess file access
FileLock lock = acquireLock(in);
if (lock != null) {
log.log(Level.FINEST, "Locking file cache for reading...");
try {
int length = (int) new File(filePath.toString()).length();
byte[] data = new byte[length];
log.log(Level.FINEST, "Reading data...");
in.read(data);
deserialize(data);
} finally {
log.log(Level.FINEST, "Unocking file cache");
lock.release();
}
} else {
throw new IOException("Can't lock file token cache for reading");
}
} finally {
in.close();
}
}
Aggregations