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();
}
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);
}
}
}
}
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());
}
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());
}
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;
}
Aggregations