KB-20071122-035

From Triled Wiki

Jump to: navigation, search

Code is taken from original post

public void testFileLock() throws Exception {

   String fileName = getDataDir() + "/fileLock.test";
   RandomAccessFile raf = new RandomAccessFile(fileName, "rw");

   FileLock lock = raf.getChannel().tryLock();
   raf.write("hello,world".getBytes());
   lock.release();
   raf.close();
}

Unexpected behaviour: throws IOException.

[edit] Temporarily solution

suggested by Andrew Zhang.

insert "raf.seek(0)" before unlocking:

   raf.write("hello,world".getBytes());
   raf.seek(0); // workaround
   lock.release(); 
Note:
Need confirmation on newer SDK.
Personal tools