Search in sources :

Example 1 with SyncFailedException

use of java.io.SyncFailedException in project KeePassDX by Kunzisoft.

the class Database method SaveData.

public void SaveData(Context ctx, Uri uri) throws IOException, PwDbOutputException {
    if (uri.getScheme().equals("file")) {
        String filename = uri.getPath();
        File tempFile = new File(filename + ".tmp");
        FileOutputStream fos = new FileOutputStream(tempFile);
        // BufferedOutputStream bos = new BufferedOutputStream(fos);
        // PwDbV3Output pmo = new PwDbV3Output(pm, bos, App.getCalendar());
        PwDbOutput pmo = PwDbOutput.getInstance(pm, fos);
        pmo.output();
        // bos.flush();
        // bos.close();
        fos.close();
        // Force data to disk before continuing
        try {
            fos.getFD().sync();
        } catch (SyncFailedException e) {
        // Ignore if fsync fails. We tried.
        }
        File orig = new File(filename);
        if (!tempFile.renameTo(orig)) {
            throw new IOException("Failed to store database.");
        }
    } else {
        OutputStream os;
        try {
            os = ctx.getContentResolver().openOutputStream(uri);
        } catch (Exception e) {
            throw new IOException("Failed to store database.");
        }
        PwDbOutput pmo = PwDbOutput.getInstance(pm, os);
        pmo.output();
        os.close();
    }
    mUri = uri;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SyncFailedException(java.io.SyncFailedException) IOException(java.io.IOException) File(java.io.File) PwDbOutput(com.keepassdroid.database.save.PwDbOutput) InvalidDBException(com.keepassdroid.database.exception.InvalidDBException) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) InvalidPasswordException(com.keepassdroid.database.exception.InvalidPasswordException) ContentFileNotFoundException(com.keepassdroid.database.exception.ContentFileNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SyncFailedException(java.io.SyncFailedException)

Example 2 with SyncFailedException

use of java.io.SyncFailedException in project narayana by jbosstm.

the class ShadowingStore method write_state.

/**
 * write_state saves the ObjectState in a file named by the type and Uid of
 * the ObjectState. If the second argument is SHADOW, then the file name is
 * different so that a subsequent commit_state invocation will rename the
 * file.
 */
protected boolean write_state(Uid objUid, String tName, OutputObjectState state, int ft) throws ObjectStoreException {
    if (tsLogger.logger.isTraceEnabled()) {
        tsLogger.logger.trace("ShadowingStore.write_state(" + objUid + ", " + tName + ", " + StateType.stateTypeString(ft) + ")");
    }
    if (tName != null) {
        String fname = genPathName(objUid, tName, ft);
        File fd = openAndLock(fname, FileLock.F_WRLCK, true);
        int imageSize = (int) state.length();
        if (fd == null) {
            tsLogger.i18NLogger.warn_objectstore_ShadowingStore_18(fname);
            return false;
        }
        FileOutputStream ofile = null;
        if (imageSize > 0) {
            try {
                ofile = new FileOutputStream(fd);
                ofile.write(state.buffer(), 0, imageSize);
                if (synchronousWrites()) {
                    // must flush any in-memory buffering prior to sync
                    ofile.flush();
                    // assume it's
                    FileDescriptor fileDesc = ofile.getFD();
                    // valid!
                    fileDesc.sync();
                }
            } catch (SyncFailedException e) {
                closeAndUnlock(fd, null, ofile);
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed to sync for " + fname, e);
            } catch (FileNotFoundException e) {
                closeAndUnlock(fd, null, ofile);
                e.printStackTrace();
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed to locate file " + fname + ": " + e, e);
            } catch (IOException e) {
                closeAndUnlock(fd, null, ofile);
                e.printStackTrace();
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed for " + fname + ": " + e, e);
            }
        }
        if (!closeAndUnlock(fd, null, ofile)) {
            tsLogger.i18NLogger.warn_objectstore_ShadowingStore_19(fname);
        }
        super.addToCache(fname);
        return true;
    } else
        throw new ObjectStoreException("ShadowStore::write_state - " + tsLogger.i18NLogger.get_objectstore_notypenameuid() + objUid);
}
Also used : ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) SyncFailedException(java.io.SyncFailedException) IOException(java.io.IOException) File(java.io.File) FileDescriptor(java.io.FileDescriptor)

Example 3 with SyncFailedException

use of java.io.SyncFailedException in project fitpay-android-sdk by fitpay.

the class PaymentDeviceConnector method sendApduExecutionResult.

/**
 * Send apdu execution result to the server
 *
 * @param apduExecutionResult apdu execution result
 */
public void sendApduExecutionResult(final ApduExecutionResult apduExecutionResult) {
    EventCallback.Builder builder = new EventCallback.Builder().setCommand(EventCallback.APDU_COMMANDS_SENT).setStatus(EventCallback.STATUS_OK).setTimestamp(apduExecutionResult.getExecutedTsEpoch());
    if (!apduExecutionResult.getState().equals(ResponseState.PROCESSED)) {
        builder.setReason(apduExecutionResult.getErrorReason());
        builder.setStatus(EventCallback.STATUS_FAILED);
    }
    builder.build().send();
    if (apduExecutionResult.getState().equals(ResponseState.NOT_PROCESSED)) {
        commitProcessed(CommitResult.FAILED, new Exception("apdu command doesn't executed"));
        return;
    }
    if (null != currentCommit) {
        currentCommit.confirm(apduExecutionResult, new ApiCallback<Void>() {

            @Override
            public void onSuccess(Void serverResult) {
                @ResponseState.ApduState String state = apduExecutionResult.getState();
                switch(state) {
                    case ResponseState.PROCESSED:
                        commitProcessed(CommitResult.SUCCESS, null);
                        break;
                    case ResponseState.EXPIRED:
                    case ResponseState.FAILED:
                    case ResponseState.ERROR:
                        commitProcessed(CommitResult.FAILED, new Throwable(apduExecutionResult.getErrorReason()));
                        break;
                }
            }

            @Override
            public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
                // TODO: determine what to do here, a failure reporting the confirmation to
                // FitPay isn't really a commit failure, it's something that needs to be
                // managed properly... i.e. the commit was applied, it was successful, it was
                // the reporting to FitPay that wasn't, is that part of the commit?
                FPLog.e(TAG, "Could not post apduExecutionResult. " + errorCode + ": " + errorMessage);
                commitProcessed(CommitResult.FAILED, new SyncFailedException("Could not send adpu confirmation.  cause: " + errorMessage));
            }
        });
    } else {
        FPLog.e(TAG, "Unexpected state - current commit is null but should be populated");
    }
}
Also used : EventCallback(com.fitpay.android.utils.EventCallback) SyncFailedException(java.io.SyncFailedException) ParseException(java.text.ParseException) SyncFailedException(java.io.SyncFailedException) ApduExecException(com.fitpay.android.paymentdevice.utils.ApduExecException)

Example 4 with SyncFailedException

use of java.io.SyncFailedException in project android_packages_apps_DSPManager by CyanogenMod.

the class Utils method writeValue.

/**
 * Write a string value to the specified file.
 *
 * @param filename The filename
 * @param value The value
 */
public static void writeValue(String filename, String value) {
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(filename), false);
        fos.write(value.getBytes());
        fos.flush();
    } catch (FileNotFoundException ex) {
        Log.w(TAG, "File " + filename + " not found: " + ex);
    } catch (SyncFailedException ex) {
        Log.w(TAG, "File " + filename + " sync failed: " + ex);
    } catch (IOException ex) {
        Log.w(TAG, "IOException trying to sync " + filename + ": " + ex);
    } catch (RuntimeException ex) {
        Log.w(TAG, "RuntimeException while syncing file: ", ex);
    } finally {
        if (fos != null) {
            try {
                Log.w(TAG_WRITE, "File " + filename + ": " + value);
                fos.close();
            } catch (IOException ex) {
                Log.w(TAG, "IOException while closing synced file: ", ex);
            } catch (RuntimeException ex) {
                Log.w(TAG, "RuntimeException while closing file: ", ex);
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) SyncFailedException(java.io.SyncFailedException) IOException(java.io.IOException) File(java.io.File)

Example 5 with SyncFailedException

use of java.io.SyncFailedException in project narayana by jbosstm.

the class LogStore method write_state.

/**
 * write_state saves the ObjectState in a file named by the type and Uid of
 * the ObjectState. If the second argument is SHADOW, then the file name is
 * different so that a subsequent commit_state invocation will rename the
 * file.
 *
 * We need to make sure that each entry is written to the next empty location
 * in the log even if there's already an entry for this tx.
 */
protected boolean write_state(Uid objUid, String tName, OutputObjectState state, int ft) throws ObjectStoreException {
    if (tsLogger.logger.isTraceEnabled()) {
        tsLogger.logger.trace("ShadowingStore.write_state(" + objUid + ", " + tName + ", " + StateType.stateTypeString(ft) + ")");
    }
    String fname = null;
    File fd = null;
    if (tName != null) {
        int imageSize = (int) state.length();
        byte[] uidString = objUid.stringForm().getBytes(StandardCharsets.UTF_8);
        // don't put in endOfLog since we keep overwriting that.
        int buffSize = _redzone.length + uidString.length + imageSize + 8;
        RandomAccessFile ofile = null;
        java.nio.channels.FileLock lock = null;
        if (imageSize > 0) {
            // always adds entry to log
            TransactionData theLogEntry = getLogName(objUid, tName, buffSize);
            LogInstance theLog = theLogEntry.container;
            if (theLog == null)
                throw new ObjectStoreException();
            fname = genPathName(theLog.getName(), tName, ft);
            fd = openAndLock(fname, FileLock.F_WRLCK, true);
            if (fd == null) {
                tsLogger.i18NLogger.warn_objectstore_ShadowingStore_18(fname);
                return false;
            }
            boolean setLength = !fd.exists();
            try {
                ofile = new RandomAccessFile(fd, FILE_MODE);
                if (setLength) {
                    ofile.setLength(_maxFileSize);
                } else {
                    if (theLog.remaining() < buffSize) {
                        long size = ofile.length() + buffSize - theLog.remaining();
                        ofile.setLength(size);
                        theLog.resize(size);
                    }
                }
                java.nio.ByteBuffer buff = java.nio.ByteBuffer.allocate(buffSize);
                buff.put(_redzone);
                buff.putInt(uidString.length);
                buff.put(uidString);
                buff.putInt(imageSize);
                buff.put(state.buffer());
                synchronized (_lock) {
                    ofile.seek(theLogEntry.offset);
                    ofile.write(buff.array());
                }
            } catch (SyncFailedException e) {
                unlockAndClose(fd, ofile);
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed to sync for " + fname, e);
            } catch (FileNotFoundException e) {
                unlockAndClose(fd, ofile);
                e.printStackTrace();
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed to locate file " + fname + ": " + e, e);
            } catch (IOException e) {
                unlockAndClose(fd, ofile);
                e.printStackTrace();
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed for " + fname + ": " + e, e);
            } finally {
                try {
                    if (lock != null)
                        lock.release();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        if (!unlockAndClose(fd, ofile)) {
            tsLogger.i18NLogger.warn_objectstore_ShadowingStore_19(fname);
        }
        super.addToCache(fname);
        return true;
    } else
        throw new ObjectStoreException("ShadowStore::write_state - " + tsLogger.i18NLogger.get_objectstore_notypenameuid() + objUid);
}
Also used : ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) FileNotFoundException(java.io.FileNotFoundException) SyncFailedException(java.io.SyncFailedException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) TransactionData(com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

SyncFailedException (java.io.SyncFailedException)6 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 ObjectStoreException (com.arjuna.ats.arjuna.exceptions.ObjectStoreException)2 TransactionData (com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData)1 ApduExecException (com.fitpay.android.paymentdevice.utils.ApduExecException)1 EventCallback (com.fitpay.android.utils.EventCallback)1 ContentFileNotFoundException (com.keepassdroid.database.exception.ContentFileNotFoundException)1 InvalidDBException (com.keepassdroid.database.exception.InvalidDBException)1 InvalidPasswordException (com.keepassdroid.database.exception.InvalidPasswordException)1 PwDbOutputException (com.keepassdroid.database.exception.PwDbOutputException)1 PwDbOutput (com.keepassdroid.database.save.PwDbOutput)1 FileDescriptor (java.io.FileDescriptor)1 OutputStream (java.io.OutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 ParseException (java.text.ParseException)1