質問

I recently added filelocks to my downloader asynctask:

FileOutputStream file = new FileOutputStream(_outFile);
file.getChannel().lock();

and after download completes, file.close() to release lock.

From a called BroadcastReceiver (different thread), I need to go through the files and see which are downloaded and which are still locked. I started with trylock:

for (int i=0; i<files.length; i++) {
    try {
        System.out.print((files[i]).getName());
        test = new FileOutputStream(files[i]);
        FileLock lock = test.getChannel().tryLock();
        if (lock != null) {
            lock.release();
            //Not a partial download. Do stuff.
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        test.close();
    }
}

Unfortunately I read the file is truncated (0 bytes) when the FileOutputStream is created. I set it to append, but the lock doesn't seem to take effect, all appear to be un-locked (fully downloaded)

Is there another way to check if a write-lock is applied to the file currently, or am I using the wrong methods here? Also, is there a way to debug file-locks, from the ADB terminal or Eclipse?

役に立ちましたか?

解決

None of this is going to work. Check the Javadoc. Locks are held on behalf of the entire process, i.e. the JVM, not by individual threads.

他のヒント

My first thought would be to open it for append per the javadocs

test = new FileOutputStream(files[i], true); // the true specifies for append
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top