Search in sources :

Example 11 with FileIOError

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;
}
Also used : FileIOError(com.helger.commons.io.file.FileIOError) MustBeLocked(com.helger.commons.annotation.MustBeLocked)

Example 12 with FileIOError

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;
}
Also used : FileIOError(com.helger.commons.io.file.FileIOError)

Aggregations

FileIOError (com.helger.commons.io.file.FileIOError)12 File (java.io.File)10 Nonnull (javax.annotation.Nonnull)3 MustBeLocked (com.helger.commons.annotation.MustBeLocked)2 DAOException (com.helger.dao.DAOException)2 Closeable (java.io.Closeable)2 IOException (java.io.IOException)2 AS2Exception (com.helger.as2lib.exception.AS2Exception)1 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)1 IMessage (com.helger.as2lib.message.IMessage)1 CompositeParameters (com.helger.as2lib.params.CompositeParameters)1 DateParameters (com.helger.as2lib.params.DateParameters)1 MessageParameters (com.helger.as2lib.params.MessageParameters)1 PDTToString (com.helger.commons.datetime.PDTToString)1 StopWatch (com.helger.commons.timing.StopWatch)1 AbstractSVRLMessage (com.helger.schematron.svrl.AbstractSVRLMessage)1 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)1 SchematronOutputType (com.helger.schematron.svrl.jaxb.SchematronOutputType)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 IXMLWriterSettings (com.helger.xml.serialize.write.IXMLWriterSettings)1