Interface Underlock.LockedFile
- All Superinterfaces:
AutoCloseable
- Enclosing class:
- Underlock
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases the lock and updates the locked file with the changes staged throughedit(com.atlassian.secrets.service.dao.IoBiConsumer<? super java.io.BufferedReader, ? super java.io.Writer>, boolean),editRaw(com.atlassian.secrets.service.dao.IoBiConsumer<? super java.io.InputStream, ? super java.io.OutputStream>, boolean)orwrite(java.nio.channels.ReadableByteChannel)(unless they have beenrolled back).voidedit(IoBiConsumer<? super BufferedReader, ? super Writer> mutator, boolean withBackup) Edits the file, under lock, to prevent concurrent edits by other threads or processes.voideditRaw(IoBiConsumer<? super InputStream, ? super OutputStream> mutator, boolean withBackup) Edits the file, under lock, to prevent concurrent edits by other threads or processes.voidrollBack()Rolls back any changes to the file content staged throughedit(IoBiConsumer, boolean),editRaw(IoBiConsumer, boolean)orwrite(ReadableByteChannel)voidwrite(ReadableByteChannel source) Acquires a lock on the file, and then writes the contents of the providedReadableByteChannelto it.
-
Method Details
-
close
Releases the lock and updates the locked file with the changes staged throughedit(com.atlassian.secrets.service.dao.IoBiConsumer<? super java.io.BufferedReader, ? super java.io.Writer>, boolean),editRaw(com.atlassian.secrets.service.dao.IoBiConsumer<? super java.io.InputStream, ? super java.io.OutputStream>, boolean)orwrite(java.nio.channels.ReadableByteChannel)(unless they have beenrolled back).- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- if an I/O error occurs while applying staged changes or releasing the lock
-
edit
void edit(IoBiConsumer<? super BufferedReader, ? super Writer> mutator, boolean withBackup) throws IOExceptionEdits the file, under lock, to prevent concurrent edits by other threads or processes.If the file already exists, the
mutatorreceives anReaderfor it. Otherwise, the first argument will benull. Themutatoralways receives anWriter, for emitting the new contents.The changes to the file content are staged and will not be visible until the lock is released. If
rollBack()is called before the lock isclose()released, the staged changes are discarded and the file contents remain unchanged.The primary use case for this method is guarding an edit with a lock.
- Parameters:
mutator- a function to use for mutating the filewithBackup- whether the old version of the file should be saved with a.bakextension- Throws:
IOException- if themutatorfails, or if the file cannot be lockedIllegalStateException- if called after the lock has beenreleased
-
editRaw
void editRaw(IoBiConsumer<? super InputStream, ? super OutputStream> mutator, boolean withBackup) throws IOExceptionEdits the file, under lock, to prevent concurrent edits by other threads or processes.If the file already exists, the
mutatorreceives anInputStreamfor it. Otherwise, the first argument will benull. Themutatoralways receives anOutputStream, for emitting the new contents.The changes to the file content are staged and will not be visible until the lock is released. If
rollBack()is called before the lock isclose()released, the staged changes are discarded and the file contents remain unchanged.The primary use case for this method is guarding an edit with a lock.
- Parameters:
mutator- a function to use for mutating the filewithBackup- whether the old version of the file should be saved with a.bakextension- Throws:
IOException- if themutatorfails, or if the file cannot be lockedIllegalStateException- if called after the lock has beenreleased
-
rollBack
void rollBack()Rolls back any changes to the file content staged throughedit(IoBiConsumer, boolean),editRaw(IoBiConsumer, boolean)orwrite(ReadableByteChannel)- Throws:
IllegalStateException- if called after the lock has beenreleased
-
write
Acquires a lock on the file, and then writes the contents of the providedReadableByteChannelto it. Contents are written to the lock first and then renamed into place to produce an (effectively) atomic write even on NFS.The changes to the file content are staged and will not be visible until the lock is released. If
rollBack()is called before the lock isclose()released, the staged changes are discarded and the file contents remain unchanged.- Parameters:
source- the contents to write- Throws:
IOException- if the file cannot be locked, or the contents cannot be writtenIllegalStateException- if called after the lock has beenreleased
-