Search in sources :

Example 26 with FileLock

use of java.nio.channels.FileLock in project graphhopper by graphhopper.

the class NativeFSLockFactory method main.

public static void main(String[] args) throws IOException {
    // trying FileLock mechanics in different processes
    File file = new File("tmp.lock");
    file.createNewFile();
    FileChannel channel = new RandomAccessFile(file, "r").getChannel();
    boolean shared = true;
    FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared);
    System.out.println("locked " + lock1);
    System.in.read();
    System.out.println("release " + lock1);
    lock1.release();
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 27 with FileLock

use of java.nio.channels.FileLock in project gocd by gocd.

the class GoConfigFileWriter method writeToConfigXmlFile.

public synchronized void writeToConfigXmlFile(String content) {
    FileChannel channel = null;
    FileOutputStream outputStream = null;
    FileLock lock = null;
    try {
        RandomAccessFile randomAccessFile = new RandomAccessFile(fileLocation(), "rw");
        channel = randomAccessFile.getChannel();
        lock = channel.lock();
        randomAccessFile.seek(0);
        randomAccessFile.setLength(0);
        outputStream = new FileOutputStream(randomAccessFile.getFD());
        IOUtils.write(content, outputStream, UTF_8);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (channel != null && lock != null) {
            try {
                lock.release();
                channel.close();
                IOUtils.closeQuietly(outputStream);
            } catch (IOException e) {
                LOGGER.error("Error occured when releasing file lock and closing file.", e);
            }
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) IOException(java.io.IOException)

Example 28 with FileLock

use of java.nio.channels.FileLock in project j2objc by google.

the class FileChannelTest method useFileChannel.

private void useFileChannel() throws IOException {
    File file = File.createTempFile("j2objc", "tmp");
    ChannelGetter channelGetter = new ChannelGetter();
    channelGetter.createChannel(file);
    // The FileOutputStream used to create the channel is released at this point.
    FileChannel channel = channelGetter.get();
    FileLock lock = channel.lock();
    lock.close();
    channel.close();
    assertTrue(file.delete());
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) File(java.io.File)

Example 29 with FileLock

use of java.nio.channels.FileLock in project jimfs by google.

the class JimfsFileChannelTest method testLock.

@Test
public void testLock() throws IOException {
    FileChannel channel = channel(regularFile(10), READ, WRITE);
    assertNotNull(channel.lock());
    assertNotNull(channel.lock(0, 10, false));
    assertNotNull(channel.lock(0, 10, true));
    assertNotNull(channel.tryLock());
    assertNotNull(channel.tryLock(0, 10, false));
    assertNotNull(channel.tryLock(0, 10, true));
    FileLock lock = channel.lock();
    assertTrue(lock.isValid());
    lock.release();
    assertFalse(lock.isValid());
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) Test(org.junit.Test)

Example 30 with FileLock

use of java.nio.channels.FileLock in project j2objc by google.

the class FileChannelImpl method basicLock.

private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
    int accessMode = (mode & O_ACCMODE);
    if (accessMode == O_RDONLY) {
        if (!shared) {
            throw new NonWritableChannelException();
        }
    } else if (accessMode == O_WRONLY) {
        if (shared) {
            throw new NonReadableChannelException();
        }
    }
    if (position < 0 || size < 0) {
        throw new IllegalArgumentException("position=" + position + " size=" + size);
    }
    FileLock pendingLock = new FileLockImpl(this, position, size, shared);
    addLock(pendingLock);
    StructFlock flock = new StructFlock();
    flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
    flock.l_whence = (short) SEEK_SET;
    flock.l_start = position;
    flock.l_len = translateLockLength(size);
    boolean success = false;
    try {
        success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
    } catch (ErrnoException errnoException) {
        throw errnoException.rethrowAsIOException();
    } finally {
        if (!success) {
            removeLock(pendingLock);
        }
    }
    return success ? pendingLock : null;
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ErrnoException(libcore.io.ErrnoException) StructFlock(libcore.io.StructFlock) FileLock(java.nio.channels.FileLock) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

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