Search in sources :

Example 11 with FileLock

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;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 12 with FileLock

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());
}
Also used : FileLock(java.nio.channels.FileLock)

Example 13 with FileLock

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());
}
Also used : FileLock(java.nio.channels.FileLock)

Example 14 with FileLock

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
    }
}
Also used : FileLock(java.nio.channels.FileLock)

Example 15 with FileLock

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();
}
Also used : FileLock(java.nio.channels.FileLock)

Aggregations

FileLock (java.nio.channels.FileLock)130 RandomAccessFile (java.io.RandomAccessFile)69 IOException (java.io.IOException)68 File (java.io.File)44 FileChannel (java.nio.channels.FileChannel)33 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)22 Test (org.junit.Test)12 FileOutputStream (java.io.FileOutputStream)10 FileInputStream (java.io.FileInputStream)5 ByteBuffer (java.nio.ByteBuffer)4 NonWritableChannelException (java.nio.channels.NonWritableChannelException)4 FileNotFoundException (java.io.FileNotFoundException)3 NonReadableChannelException (java.nio.channels.NonReadableChannelException)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 Path (java.nio.file.Path)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 Properties (java.util.Properties)3 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)2 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)2 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)2