use of java.nio.channels.FileLock in project PocketHub by pockethub.
the class RequestWriter method write.
/**
* Write request to file
*
* @param request
* @return request
*/
public <V> V write(V request) {
RandomAccessFile dir = null;
FileLock lock = null;
ObjectOutputStream output = null;
try {
createDirectory(handle.getParentFile());
dir = new RandomAccessFile(handle, "rw");
lock = dir.getChannel().lock();
output = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(dir.getFD()), 8192));
output.writeInt(version);
output.writeObject(request);
} catch (IOException e) {
Log.d(TAG, "Exception writing cache " + handle.getName(), e);
return null;
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
Log.d(TAG, "Exception closing stream", e);
}
}
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
Log.d(TAG, "Exception unlocking file", e);
}
}
if (dir != null) {
try {
dir.close();
} catch (IOException e) {
Log.d(TAG, "Exception closing file", e);
}
}
}
return request;
}
use of java.nio.channels.FileLock in project robovm by robovm.
the class FileLockTest method test_channel.
/**
* @tests java.nio.channels.FileLock#channel()
*/
public void test_channel() {
assertSame(readWriteChannel, mockLock.channel());
FileLock lock = new MockFileLock(null, 0, 10, true);
assertNull(lock.channel());
}
use of java.nio.channels.FileLock in project robovm by robovm.
the class FileLockTest method test_isShared.
/**
* @tests java.nio.channels.FileLock#isShared()
*/
public void test_isShared() {
assertFalse(mockLock.isShared());
FileLock lock = new MockFileLock(null, 0, 10, true);
assertTrue(lock.isShared());
}
use of java.nio.channels.FileLock in project robovm by robovm.
the class FileLockTest method test_Constructor_Ljava_nio_channels_FileChannelJJZ.
/**
* @tests java.nio.channels.FileLock#FileLock(FileChannel, long, long,
* boolean)
*/
public void test_Constructor_Ljava_nio_channels_FileChannelJJZ() {
FileLock fileLock1 = new MockFileLock(null, 0, 0, false);
assertNull(fileLock1.channel());
try {
new MockFileLock(readWriteChannel, -1, 0, false);
fail("should throw IllegalArgumentException.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
new MockFileLock(readWriteChannel, 0, -1, false);
fail("should throw IllegalArgumentException.");
} catch (IllegalArgumentException ex) {
// expected
}
// Harmony-682 regression test
try {
new MockFileLock(readWriteChannel, Long.MAX_VALUE, 1, false);
fail("should throw IllegalArgumentException.");
} catch (IllegalArgumentException ex) {
// expected
}
}
use of java.nio.channels.FileLock in project robovm by robovm.
the class OldFileChannelTest method testTryLockOverlapping.
public void testTryLockOverlapping() throws IOException {
FileLock lockOne = readWriteFileChannel.tryLock(0, 10, false);
FileLock lockTwo = readWriteFileChannel.tryLock(10, 20, false);
assertLockFails(0, 10);
lockOne.release();
assertLockFails(5, 10);
lockOne = readWriteFileChannel.tryLock(0, 10, false);
lockTwo.release();
lockOne.release();
}
Aggregations