use of android.util.AtomicFile in project android_frameworks_base by AOSPA.
the class SettingsState method doWriteState.
private void doWriteState() {
if (DEBUG_PERSISTENCE) {
Slog.i(LOG_TAG, "[PERSIST START]");
}
AtomicFile destination = new AtomicFile(mStatePersistFile);
final int version;
final ArrayMap<String, Setting> settings;
synchronized (mLock) {
version = mVersion;
settings = new ArrayMap<>(mSettings);
mDirty = false;
mWriteScheduled = false;
}
FileOutputStream out = null;
try {
out = destination.startWrite();
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(out, StandardCharsets.UTF_8.name());
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startDocument(null, true);
serializer.startTag(null, TAG_SETTINGS);
serializer.attribute(null, ATTR_VERSION, String.valueOf(version));
final int settingCount = settings.size();
for (int i = 0; i < settingCount; i++) {
Setting setting = settings.valueAt(i);
writeSingleSetting(mVersion, serializer, setting.getId(), setting.getName(), setting.getValue(), setting.getPackageName());
if (DEBUG_PERSISTENCE) {
Slog.i(LOG_TAG, "[PERSISTED]" + setting.getName() + "=" + setting.getValue());
}
}
serializer.endTag(null, TAG_SETTINGS);
serializer.endDocument();
destination.finishWrite(out);
synchronized (mLock) {
addHistoricalOperationLocked(HISTORICAL_OPERATION_PERSIST, null);
}
if (DEBUG_PERSISTENCE) {
Slog.i(LOG_TAG, "[PERSIST END]");
}
} catch (Throwable t) {
Slog.wtf(LOG_TAG, "Failed to write settings, restoring backup", t);
destination.failWrite(out);
} finally {
IoUtils.closeQuietly(out);
}
}
use of android.util.AtomicFile in project android_frameworks_base by AOSPA.
the class NetworkStatsCollection method readLegacyUid.
@Deprecated
public void readLegacyUid(File file, boolean onlyTags) throws IOException {
final AtomicFile inputFile = new AtomicFile(file);
DataInputStream in = null;
try {
in = new DataInputStream(new BufferedInputStream(inputFile.openRead()));
// verify file magic header intact
final int magic = in.readInt();
if (magic != FILE_MAGIC) {
throw new ProtocolException("unexpected magic: " + magic);
}
final int version = in.readInt();
switch(version) {
case VERSION_UID_INIT:
{
// mapping into NetworkIdentitySet.
break;
}
case VERSION_UID_WITH_IDENT:
{
// for a short time.
break;
}
case VERSION_UID_WITH_TAG:
case VERSION_UID_WITH_SET:
{
// uid := size *(NetworkIdentitySet size *(uid set tag NetworkStatsHistory))
final int identSize = in.readInt();
for (int i = 0; i < identSize; i++) {
final NetworkIdentitySet ident = new NetworkIdentitySet(in);
final int size = in.readInt();
for (int j = 0; j < size; j++) {
final int uid = in.readInt();
final int set = (version >= VERSION_UID_WITH_SET) ? in.readInt() : SET_DEFAULT;
final int tag = in.readInt();
final Key key = new Key(ident, uid, set, tag);
final NetworkStatsHistory history = new NetworkStatsHistory(in);
if ((tag == TAG_NONE) != onlyTags) {
recordHistory(key, history);
}
}
}
break;
}
default:
{
throw new ProtocolException("unexpected version: " + version);
}
}
} catch (FileNotFoundException e) {
// missing stats is okay, probably first boot
} finally {
IoUtils.closeQuietly(in);
}
}
use of android.util.AtomicFile in project android_frameworks_base by AOSPA.
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);
}
}
use of android.util.AtomicFile in project android_frameworks_base by AOSPA.
the class EphemeralApplicationRegistry method parseMetadataFile.
private static UninstalledEphemeralAppState parseMetadataFile(File metadataFile) {
if (!metadataFile.exists()) {
return null;
}
FileInputStream in;
try {
in = new AtomicFile(metadataFile).openRead();
} catch (FileNotFoundException fnfe) {
Slog.i(LOG_TAG, "No ephemeral metadata file");
return null;
}
final File ephemeralDir = metadataFile.getParentFile();
final long timestamp = metadataFile.lastModified();
final String packageName = ephemeralDir.getName();
try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, StandardCharsets.UTF_8.name());
return new UninstalledEphemeralAppState(parseMetadata(parser, packageName), timestamp);
} catch (XmlPullParserException | IOException e) {
throw new IllegalStateException("Failed parsing ephemeral" + " metadata file: " + metadataFile, e);
} finally {
IoUtils.closeQuietly(in);
}
}
use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.
the class RegisteredServicesCache method createFileForUser.
private AtomicFile createFileForUser(int userId) {
File userDir = getUserSystemDirectory(userId);
File userFile = new File(userDir, REGISTERED_SERVICES_DIR + "/" + mInterfaceName + ".xml");
return new AtomicFile(userFile);
}
Aggregations