use of com.helger.commons.io.file.FileIOError in project ph-commons by phax.
the class FileIntIDFactory method readAndUpdateIDCounter.
/*
* Note: this method must only be called from within a locked section!
*/
@Override
@MustBeLocked(ELockType.WRITE)
protected final int readAndUpdateIDCounter(@Nonnegative final int nReserveCount) {
// Read the old content
final String sContent = SimpleFileIO.getFileAsString(m_aFile, CHARSET_TO_USE);
final int nRead = sContent != null ? StringParser.parseInt(sContent.trim(), 0) : 0;
// Write the new content to the new file
// This will fail, if the disk is full
SimpleFileIO.writeFile(m_aNewFile, Integer.toString(nRead + nReserveCount), CHARSET_TO_USE);
FileIOError aIOError;
boolean bRenamedToPrev = false;
if (m_aFile.exists()) {
// Rename the existing file to the prev file
aIOError = FileOperationManager.INSTANCE.renameFile(m_aFile, m_aPrevFile);
bRenamedToPrev = true;
} else
aIOError = new FileIOError(EFileIOOperation.RENAME_FILE, EFileIOErrorCode.NO_ERROR);
if (aIOError.isSuccess()) {
// Rename the new file to the destination file
aIOError = FileOperationManager.INSTANCE.renameFile(m_aNewFile, m_aFile);
if (aIOError.isSuccess()) {
// Finally delete the prev file (may not be existing for fresh
// instances)
aIOError = FileOperationManager.INSTANCE.deleteFileIfExisting(m_aPrevFile);
} else {
// -> Revert original rename to stay as consistent as possible
if (bRenamedToPrev)
FileOperationManager.INSTANCE.renameFile(m_aPrevFile, m_aFile);
}
}
if (aIOError.isFailure())
throw new IllegalStateException("Error on rename(existing-old)/rename(new-existing)/delete(old): " + aIOError);
return nRead;
}
use of com.helger.commons.io.file.FileIOError in project ph-commons by phax.
the class FileLongIDFactory method readAndUpdateIDCounter.
/*
* Note: this method must only be called from within a locked section!
*/
@Override
protected final long readAndUpdateIDCounter(@Nonnegative final int nReserveCount) {
final String sContent = SimpleFileIO.getFileAsString(m_aFile, CHARSET_TO_USE);
final long nRead = sContent != null ? StringParser.parseLong(sContent.trim(), 0) : 0;
// Write the new content to the new file
// This will fail, if the disk is full
SimpleFileIO.writeFile(m_aNewFile, Long.toString(nRead + nReserveCount), CHARSET_TO_USE);
FileIOError aIOError;
boolean bRenamedToPrev = false;
if (m_aFile.exists()) {
// Rename the existing file to the prev file
aIOError = FileOperationManager.INSTANCE.renameFile(m_aFile, m_aPrevFile);
bRenamedToPrev = true;
} else
aIOError = new FileIOError(EFileIOOperation.RENAME_FILE, EFileIOErrorCode.NO_ERROR);
if (aIOError.isSuccess()) {
// Rename the new file to the destination file
aIOError = FileOperationManager.INSTANCE.renameFile(m_aNewFile, m_aFile);
if (aIOError.isSuccess()) {
// Finally delete the prev file (may not be existing for fresh
// instances)
aIOError = FileOperationManager.INSTANCE.deleteFileIfExisting(m_aPrevFile);
} else {
// -> Revert original rename to stay as consistent as possible
if (bRenamedToPrev)
FileOperationManager.INSTANCE.renameFile(m_aPrevFile, m_aFile);
}
}
if (aIOError.isFailure())
throw new IllegalStateException("Error on rename(existing-old)/rename(new-existing)/delete(old): " + aIOError);
return nRead;
}
Aggregations