use of java.nio.channels.FileLock in project nifi-registry by apache.
the class FileUtils method copyFile.
/**
* Copies the given source file to the given destination file. The given destination will be overwritten if it already exists.
*
* @param source the file to copy
* @param destination the file to copy to
* @param lockInputFile if true will lock input file during copy; if false will not
* @param lockOutputFile if true will lock output file during copy; if false will not
* @param move if true will perform what is effectively a move operation rather than a pure copy. This allows for potentially highly efficient movement of the file but if not possible this will
* revert to a copy then delete behavior. If false, then the file is copied and the source file is retained. If a true rename/move occurs then no lock is held during that time.
* @param logger if failures occur, they will be logged to this logger if possible. If this logger is null, an IOException will instead be thrown, indicating the problem.
* @return long number of bytes copied
* @throws FileNotFoundException if the source file could not be found
* @throws IOException if unable to read or write the underlying streams
* @throws SecurityException if a security manager denies the needed file operations
*/
public static long copyFile(final File source, final File destination, final boolean lockInputFile, final boolean lockOutputFile, final boolean move, final Logger logger) throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
FileLock inLock = null;
FileLock outLock = null;
long fileSize = 0L;
if (!source.canRead()) {
throw new IOException("Must at least have read permission");
}
if (move && source.renameTo(destination)) {
fileSize = destination.length();
} else {
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(destination);
final FileChannel in = fis.getChannel();
final FileChannel out = fos.getChannel();
if (lockInputFile) {
inLock = in.tryLock(0, Long.MAX_VALUE, true);
if (null == inLock) {
throw new IOException("Unable to obtain shared file lock for: " + source.getAbsolutePath());
}
}
if (lockOutputFile) {
outLock = out.tryLock(0, Long.MAX_VALUE, false);
if (null == outLock) {
throw new IOException("Unable to obtain exclusive file lock for: " + destination.getAbsolutePath());
}
}
long bytesWritten = 0;
do {
bytesWritten += out.transferFrom(in, bytesWritten, TRANSFER_CHUNK_SIZE_BYTES);
fileSize = in.size();
} while (bytesWritten < fileSize);
out.force(false);
FileUtils.closeQuietly(fos);
FileUtils.closeQuietly(fis);
fos = null;
fis = null;
if (move && !FileUtils.deleteFile(source, null, 5)) {
if (logger == null) {
FileUtils.deleteFile(destination, null, 5);
throw new IOException("Could not remove file " + source.getAbsolutePath());
} else {
logger.warn("Configured to delete source file when renaming/move not successful. However, unable to delete file at: " + source.getAbsolutePath());
}
}
} finally {
FileUtils.releaseQuietly(inLock);
FileUtils.releaseQuietly(outLock);
FileUtils.closeQuietly(fos);
FileUtils.closeQuietly(fis);
}
}
return fileSize;
}
use of java.nio.channels.FileLock in project Chronicle-Bytes by OpenHFT.
the class MappedFile method acquireByteStore.
@NotNull
public <T extends MappedBytesStore> T acquireByteStore(long position, @NotNull MappedBytesStoreFactory<T> mappedBytesStoreFactory) throws IOException, IllegalArgumentException, IllegalStateException {
if (closed.get())
throw new IOException("Closed");
if (position < 0)
throw new IOException("Attempt to access a negative position: " + position);
int chunk = (int) (position / chunkSize);
synchronized (stores) {
while (stores.size() <= chunk) {
stores.add(null);
}
WeakReference<MappedBytesStore> mbsRef = stores.get(chunk);
if (mbsRef != null) {
@NotNull T mbs = (T) mbsRef.get();
if (mbs != null && mbs.tryReserve()) {
return mbs;
}
}
long start = System.nanoTime();
long minSize = (chunk + 1L) * chunkSize + overlapSize;
long size = fileChannel.size();
if (size < minSize && !readOnly) {
// handle a possible race condition between processes.
try {
synchronized (GLOBAL_FILE_LOCK) {
size = fileChannel.size();
if (size < minSize) {
try (FileLock ignore = fileChannel.lock()) {
size = fileChannel.size();
if (size < minSize) {
raf.setLength(minSize);
}
}
}
}
} catch (IOException ioe) {
throw new IOException("Failed to resize to " + minSize, ioe);
}
}
long mappedSize = chunkSize + overlapSize;
FileChannel.MapMode mode = readOnly ? FileChannel.MapMode.READ_ONLY : FileChannel.MapMode.READ_WRITE;
long address;
try {
address = OS.map(fileChannel, mode, chunk * chunkSize, mappedSize);
} catch (IOException e) {
// sometimes on Windows it doesn't like read only, but not always or on all systems.
if (readOnly && e.getMessage().equals("Not enough storage is available to process this command")) {
Jvm.warn().on(getClass(), "Mapping " + file + " as READ_ONLY failed, switching to READ_WRITE");
address = OS.map(fileChannel, FileChannel.MapMode.READ_WRITE, chunk * chunkSize, mappedSize);
readOnly = false;
} else {
throw e;
}
}
final long safeCapacity = this.chunkSize + overlapSize / 2;
T mbs2 = mappedBytesStoreFactory.create(this, chunk * this.chunkSize, address, mappedSize, safeCapacity);
stores.set(chunk, new WeakReference<>(mbs2));
if (newChunkListener != null)
newChunkListener.onNewChunk(file.getPath(), chunk, (System.nanoTime() - start) / 1000);
// new Throwable("chunk "+chunk).printStackTrace();
return mbs2;
}
}
use of java.nio.channels.FileLock in project tycho by eclipse.
the class LockProcess method main.
public static void main(String[] args) throws Exception {
File file = new File(args[0]);
long wait = Long.valueOf(args[1]);
RandomAccessFile raFile = new RandomAccessFile(file, "rw");
FileLock lock = raFile.getChannel().lock(0, 1, false);
System.out.println(LOCK_ACQUIRED_MSG);
Thread.sleep(wait);
lock.release();
raFile.close();
}
use of java.nio.channels.FileLock in project mule by mulesoft.
the class FreePortFinder method releasePort.
/**
* Indicates that the port is free from the point of view of the caller.
* <p/>
* Checks that the port was released, if it was not, then it would be marked as in use, so no other client receives the same
* port again.
*
* @param port the port number to release.
*/
public synchronized void releasePort(int port) {
if (isPortFree(port) && locks.containsKey(port) && files.containsKey(port)) {
FileLock lock = locks.remove(port);
FileChannel file = files.remove(port);
try {
lock.release();
file.close();
} catch (IOException e) {
// Ignore
}
} else {
if (logger.isInfoEnabled()) {
logger.info(format("Port %d was not correctly released", port));
}
}
}
use of java.nio.channels.FileLock in project bgpcep by opendaylight.
the class ConfigLoaderImpl method parseDefaultConfigFile.
private NormalizedNode<?, ?> parseDefaultConfigFile(final ConfigFileProcessor config, final String filename) throws IOException, XMLStreamException {
final NormalizedNodeResult result = new NormalizedNodeResult();
final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
final File newFile = new File(this.path, filename);
FileChannel channel = new RandomAccessFile(newFile, READ).getChannel();
FileLock lock = null;
final Stopwatch stopwatch = Stopwatch.createStarted();
while (lock == null || stopwatch.elapsed(TimeUnit.SECONDS) > TIMEOUT_SECONDS) {
try {
lock = channel.tryLock();
} catch (final IllegalStateException e) {
// Ignore
}
if (lock == null) {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
LOG.warn("Failed to lock xml", e);
}
}
}
try (InputStream resourceAsStream = new FileInputStream(newFile)) {
final XMLInputFactory factory = XMLInputFactory.newInstance();
final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
final SchemaNode schemaNode = SchemaContextUtil.findDataSchemaNode(this.schemaContext, config.getSchemaPath());
try (XmlParserStream xmlParser = XmlParserStream.create(streamWriter, this.schemaContext, schemaNode)) {
xmlParser.parse(reader);
} catch (final URISyntaxException | XMLStreamException | IOException | ParserConfigurationException | SAXException e) {
LOG.warn("Failed to parse xml", e);
} finally {
reader.close();
channel.close();
}
}
return result.getResult();
}
Aggregations