Search in sources :

Example 86 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class ProcessStatsService method performWriteState.

void performWriteState() {
    if (DEBUG)
        Slog.d(TAG, "Performing write to " + mFile.getBaseFile());
    Parcel data;
    AtomicFile file;
    synchronized (mPendingWriteLock) {
        data = mPendingWrite;
        file = mPendingWriteFile;
        mPendingWriteCommitted = false;
        if (data == null) {
            return;
        }
        mPendingWrite = null;
        mPendingWriteFile = null;
        mWriteLock.lock();
    }
    FileOutputStream stream = null;
    try {
        stream = file.startWrite();
        stream.write(data.marshall());
        stream.flush();
        file.finishWrite(stream);
        if (DEBUG)
            Slog.d(TAG, "Write completed successfully!");
    } catch (IOException e) {
        Slog.w(TAG, "Error writing process statistics", e);
        file.failWrite(stream);
    } finally {
        data.recycle();
        trimHistoricStatesWriteLocked();
        mWriteLock.unlock();
    }
}
Also used : AtomicFile(android.util.AtomicFile) Parcel(android.os.Parcel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 87 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class ProcessStatsService method updateFile.

private void updateFile() {
    mFile = new AtomicFile(new File(mBaseDir, STATE_FILE_PREFIX + mProcessStats.mTimePeriodStartClockStr + STATE_FILE_SUFFIX));
    mLastWriteTime = SystemClock.uptimeMillis();
}
Also used : AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 88 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class SettingsState method readStateSyncLocked.

private void readStateSyncLocked() {
    FileInputStream in;
    if (!mStatePersistFile.exists()) {
        Slog.i(LOG_TAG, "No settings state " + mStatePersistFile);
        addHistoricalOperationLocked(HISTORICAL_OPERATION_INITIALIZE, null);
        return;
    }
    try {
        in = new AtomicFile(mStatePersistFile).openRead();
    } catch (FileNotFoundException fnfe) {
        String message = "No settings state " + mStatePersistFile;
        Slog.wtf(LOG_TAG, message);
        Slog.i(LOG_TAG, message);
        return;
    }
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, StandardCharsets.UTF_8.name());
        parseStateLocked(parser);
    } catch (XmlPullParserException | IOException e) {
        String message = "Failed parsing settings file: " + mStatePersistFile;
        Slog.wtf(LOG_TAG, message);
        throw new IllegalStateException(message, e);
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileNotFoundException(java.io.FileNotFoundException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 89 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class CompilerStats method writeInternal.

@Override
protected void writeInternal(Void data) {
    AtomicFile file = getFile();
    FileOutputStream f = null;
    try {
        f = file.startWrite();
        OutputStreamWriter osw = new OutputStreamWriter(f);
        write(osw);
        osw.flush();
        file.finishWrite(f);
    } catch (IOException e) {
        if (f != null) {
            file.failWrite(f);
        }
        Log.e(PackageManagerService.TAG, "Failed to write compiler stats", e);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 90 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class AppWidgetServiceImpl method loadGroupStateLocked.

// only call from initialization -- it assumes that the data structures are all empty
private void loadGroupStateLocked(int[] profileIds) {
    // We can bind the widgets to host and providers only after
    // reading the host and providers for all users since a widget
    // can have a host and a provider in different users.
    List<LoadedWidgetState> loadedWidgets = new ArrayList<>();
    int version = 0;
    final int profileIdCount = profileIds.length;
    for (int i = 0; i < profileIdCount; i++) {
        final int profileId = profileIds[i];
        // No file written for this user - nothing to do.
        AtomicFile file = getSavedStateFile(profileId);
        try {
            FileInputStream stream = file.openRead();
            version = readProfileStateFromFileLocked(stream, profileId, loadedWidgets);
            IoUtils.closeQuietly(stream);
        } catch (FileNotFoundException e) {
            Slog.w(TAG, "Failed to read state: " + e);
        }
    }
    if (version >= 0) {
        // Hooke'm up...
        bindLoadedWidgetsLocked(loadedWidgets);
        // upgrade the database if needed
        performUpgradeLocked(version);
    } else {
        // failed reading, clean up
        Slog.w(TAG, "Failed to read state, clearing widgets and hosts.");
        clearWidgetsLocked();
        mHosts.clear();
        final int N = mProviders.size();
        for (int i = 0; i < N; i++) {
            mProviders.get(i).widgets.clear();
        }
    }
}
Also used : AtomicFile(android.util.AtomicFile) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Point(android.graphics.Point) FileInputStream(java.io.FileInputStream)

Aggregations

AtomicFile (android.util.AtomicFile)231 IOException (java.io.IOException)135 File (java.io.File)106 FileOutputStream (java.io.FileOutputStream)73 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)70 FileNotFoundException (java.io.FileNotFoundException)61 FileInputStream (java.io.FileInputStream)39 XmlSerializer (org.xmlpull.v1.XmlSerializer)35 XmlPullParser (org.xmlpull.v1.XmlPullParser)33 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)25 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)24 BufferedOutputStream (java.io.BufferedOutputStream)19 BufferedInputStream (java.io.BufferedInputStream)16 UserInfo (android.content.pm.UserInfo)15 Bundle (android.os.Bundle)15 RemoteException (android.os.RemoteException)15 ArrayList (java.util.ArrayList)15 InputStream (java.io.InputStream)14 NetworkStatsHistory (android.net.NetworkStatsHistory)12 ErrnoException (android.system.ErrnoException)12