use of java.io.RandomAccessFile in project android_frameworks_base by DirtyUnicorns.
the class AbstractService method run.
@Override
public void run() {
Context applicationContext = getApplicationContext();
File resultFile = new File(applicationContext.getFilesDir(), getId());
try {
// Append a constant value in result file, if services crashed and is relaunched, size
// of the result file will be too big.
RandomAccessFile raf = new RandomAccessFile(resultFile, "rw");
raf.seek(raf.length());
Log.i(TAG, "Writing 0x42434445 at " + raf.length() + " in " + resultFile.getPath());
raf.writeInt(0x42434445);
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
MultiDex.install(applicationContext);
Log.i(TAG, "Multi dex installation done.");
int value = getValue();
Log.i(TAG, "Saving the result (" + value + ") to " + resultFile.getPath());
try {
// Append the check value in result file, keeping the constant values already written.
RandomAccessFile raf = new RandomAccessFile(resultFile, "rw");
raf.seek(raf.length());
Log.i(TAG, "Writing result at " + raf.length() + " in " + resultFile.getPath());
raf.writeInt(value);
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Writing end of processing flags, the existence of the file is the criteria
RandomAccessFile raf = new RandomAccessFile(new File(applicationContext.getFilesDir(), getId() + ".complete"), "rw");
Log.i(TAG, "creating complete file " + resultFile.getPath());
raf.writeInt(0x32333435);
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of java.io.RandomAccessFile in project android_frameworks_base by DirtyUnicorns.
the class MiniThumbFile method miniThumbIndexFile.
private RandomAccessFile miniThumbIndexFile() {
if (mMiniThumbIndexFile == null) {
removeOldIndexFile();
String path = randomAccessIndexFilePath(MINI_THUMB_DATA_FILE_VERSION);
File directory = new File(path).getParentFile();
if (!directory.isDirectory()) {
if (!directory.mkdirs()) {
Log.e(TAG, "Unable to create .thumbnails directory " + directory.toString());
}
}
File f = new File(path);
try {
mMiniThumbIndexFile = new RandomAccessFile(f, "rw");
} catch (IOException ex) {
// thumbnails.
try {
mMiniThumbIndexFile = new RandomAccessFile(f, "r");
} catch (IOException ex2) {
// ignore exception
Log.e(TAG, "miniThumbIndexFile open r exception: " + f);
}
}
if (mMiniThumbIndexFile != null) {
mIndexChannel = mMiniThumbIndexFile.getChannel();
}
}
return mMiniThumbIndexFile;
}
use of java.io.RandomAccessFile 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;
}
use of java.io.RandomAccessFile 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.io.RandomAccessFile 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;
}
Aggregations