Search in sources :

Example 86 with FileLock

use of java.nio.channels.FileLock in project cryptofs by cryptomator.

the class OpenCryptoFileTest method testLockDelegatesToChannel.

@Theory
public void testLockDelegatesToChannel(boolean shared) throws IOException {
    long position = 383872;
    long size = 48483;
    FileLock lock = mock(FileLock.class);
    when(channel.lock(position, size, shared)).thenReturn(lock);
    assertThat(inTest.lock(position, size, shared), is(lock));
}
Also used : FileLock(java.nio.channels.FileLock) Theory(org.junit.experimental.theories.Theory)

Example 87 with FileLock

use of java.nio.channels.FileLock in project cryptofs by cryptomator.

the class CryptoFileChannel method tryLock.

@Override
public FileLock tryLock(long position, long size, boolean shared) throws IOException {
    assertOpen();
    FileLock delegate = openCryptoFile.tryLock(position, size, shared);
    if (delegate == null) {
        return null;
    }
    CryptoFileLock result = // 
    CryptoFileLock.builder().withDelegate(// 
    delegate).withChannel(// 
    this).withPosition(// 
    position).withSize(// 
    size).thatIsShared(shared).build();
    return result;
}
Also used : FileLock(java.nio.channels.FileLock)

Example 88 with FileLock

use of java.nio.channels.FileLock in project cryptofs by cryptomator.

the class CryptoFileChannel method lock.

@Override
public FileLock lock(long position, long size, boolean shared) throws IOException {
    assertOpen();
    return blockingIo(() -> {
        FileLock delegate = openCryptoFile.lock(position, size, shared);
        CryptoFileLock result = // 
        CryptoFileLock.builder().withDelegate(// 
        delegate).withChannel(// 
        this).withPosition(// 
        position).withSize(// 
        size).thatIsShared(shared).build();
        return result;
    });
}
Also used : FileLock(java.nio.channels.FileLock)

Example 89 with FileLock

use of java.nio.channels.FileLock in project cryptofs by cryptomator.

the class AsyncDelegatingFileChannelTest method testTryLock.

@Test
public void testTryLock() throws IOException {
    Mockito.when(channel.tryLock(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(null);
    Assert.assertNull(asyncChannel.tryLock(0l, 42l, true));
    FileLock lock = Mockito.mock(FileLock.class);
    Mockito.when(channel.tryLock(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(lock);
    Assert.assertEquals(lock, asyncChannel.tryLock(0l, 42l, true));
}
Also used : FileLock(java.nio.channels.FileLock) Test(org.junit.Test)

Example 90 with FileLock

use of java.nio.channels.FileLock in project activemq-artemis by apache.

the class FileLockNodeManager method isBackupLive.

@Override
public boolean isBackupLive() throws Exception {
    FileLock liveAttemptLock;
    liveAttemptLock = tryLock(FileLockNodeManager.LIVE_LOCK_POS);
    if (liveAttemptLock == null) {
        return true;
    } else {
        liveAttemptLock.release();
        return false;
    }
}
Also used : FileLock(java.nio.channels.FileLock)

Aggregations

FileLock (java.nio.channels.FileLock)246 IOException (java.io.IOException)127 RandomAccessFile (java.io.RandomAccessFile)99 FileChannel (java.nio.channels.FileChannel)83 File (java.io.File)77 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)37 FileOutputStream (java.io.FileOutputStream)29 Test (org.junit.Test)19 Path (java.nio.file.Path)16 FileInputStream (java.io.FileInputStream)13 FileNotFoundException (java.io.FileNotFoundException)12 ByteBuffer (java.nio.ByteBuffer)10 InputStream (java.io.InputStream)5 Properties (java.util.Properties)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 NonWritableChannelException (java.nio.channels.NonWritableChannelException)4 NoSuchFileException (java.nio.file.NoSuchFileException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3