I need to be able to have multiple processes accessing a single file such that there is at most one writer but potentially multiple readers that have the file open at the same time (enforced at file opening time). If the writer decides to update a certain byte range, the readers must not access that byte range until the writer is finished with its update.
I can express the file level sharing in C# using FileAccess.ReadWrite/FileShare.Read when opening the file for writing and FileAccess.Read/FileShare.ReadWrite when opening the file for reading. This also disallows any further writers.
Byte ranges can then be temporarily locked for reading/updating using FileStream.Lock (or, as unfortunately shared locks are not available in the basic C# APIs, calling Window's system API function LockFileEx).
Is there a way to express this behavior in Java (targeting linux) without resorting to introducing a separate lock file to manage file level sharing? I.e. how can I specify sharing when opening a file in Java and then perform separate locking on byte ranges?
I did check various API documentation for RandomAccessFile and FileChannel. While I did find something about byte range locking, there does not appear to be anything about file sharing and access limitations for other processes accessing the same file.