use of android.app.backup.BackupDataOutput in project android_frameworks_base by ParanoidAndroid.
the class LocalTransport method getRestoreData.
public int getRestoreData(ParcelFileDescriptor outFd) {
if (mRestorePackages == null)
throw new IllegalStateException("startRestore not called");
if (mRestorePackage < 0)
throw new IllegalStateException("nextRestorePackage not called");
File packageDir = new File(mDataDir, mRestorePackages[mRestorePackage].packageName);
// The restore set is the concatenation of the individual record blobs,
// each of which is a file in the package's directory
File[] blobs = packageDir.listFiles();
if (blobs == null) {
// nextRestorePackage() ensures the dir exists, so this is an error
Log.e(TAG, "Error listing directory: " + packageDir);
return BackupConstants.TRANSPORT_ERROR;
}
// We expect at least some data if the directory exists in the first place
if (DEBUG)
Log.v(TAG, " getRestoreData() found " + blobs.length + " key files");
BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
try {
for (File f : blobs) {
FileInputStream in = new FileInputStream(f);
try {
int size = (int) f.length();
byte[] buf = new byte[size];
in.read(buf);
String key = new String(Base64.decode(f.getName()));
if (DEBUG)
Log.v(TAG, " ... key=" + key + " size=" + size);
out.writeEntityHeader(key, size);
out.writeEntityData(buf, size);
} finally {
in.close();
}
}
return BackupConstants.TRANSPORT_OK;
} catch (IOException e) {
Log.e(TAG, "Unable to read backup records", e);
return BackupConstants.TRANSPORT_ERROR;
}
}
use of android.app.backup.BackupDataOutput in project platform_frameworks_base by android.
the class BackupDataTest method testDelete.
public void testDelete() throws IOException {
mFile = new File(mDirectory, "backup_delete_test.dat");
openForWriting();
BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
for (int i = 0; i < KEYS.length; i++) {
deleteEntity(bdo, KEYS[i]);
}
mDataFile.close();
openForReading();
BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
int count = 0;
while (bdi.readNextHeader()) {
readAndVerifyDeletedEntity(bdi, KEYS[count]);
count++;
}
assertEquals("four deletes in this stream", KEYS.length, count);
}
use of android.app.backup.BackupDataOutput in project android_frameworks_base by DirtyUnicorns.
the class LocalTransport method getRestoreData.
@Override
public int getRestoreData(ParcelFileDescriptor outFd) {
if (mRestorePackages == null)
throw new IllegalStateException("startRestore not called");
if (mRestorePackage < 0)
throw new IllegalStateException("nextRestorePackage not called");
if (mRestoreType != RestoreDescription.TYPE_KEY_VALUE) {
throw new IllegalStateException("getRestoreData(fd) for non-key/value dataset");
}
File packageDir = new File(mRestoreSetIncrementalDir, mRestorePackages[mRestorePackage].packageName);
// The restore set is the concatenation of the individual record blobs,
// each of which is a file in the package's directory. We return the
// data in lexical order sorted by key, so that apps which use synthetic
// keys like BLOB_1, BLOB_2, etc will see the date in the most obvious
// order.
ArrayList<DecodedFilename> blobs = contentsByKey(packageDir);
if (blobs == null) {
// nextRestorePackage() ensures the dir exists, so this is an error
Log.e(TAG, "No keys for package: " + packageDir);
return TRANSPORT_ERROR;
}
// We expect at least some data if the directory exists in the first place
if (DEBUG)
Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files");
BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
try {
for (DecodedFilename keyEntry : blobs) {
File f = keyEntry.file;
FileInputStream in = new FileInputStream(f);
try {
int size = (int) f.length();
byte[] buf = new byte[size];
in.read(buf);
if (DEBUG)
Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size);
out.writeEntityHeader(keyEntry.key, size);
out.writeEntityData(buf, size);
} finally {
in.close();
}
}
return TRANSPORT_OK;
} catch (IOException e) {
Log.e(TAG, "Unable to read backup records", e);
return TRANSPORT_ERROR;
}
}
use of android.app.backup.BackupDataOutput in project android_frameworks_base by DirtyUnicorns.
the class BackupDataTest method testMixed.
public void testMixed() throws IOException {
mFile = new File(mDirectory, "backup_mixed_test.dat");
openForWriting();
BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
int i = 0;
deleteEntity(bdo, KEYS[i]);
i++;
writeEntity(bdo, KEYS[i], DATA[i].getBytes());
i++;
writeEntity(bdo, KEYS[i], DATA[i].getBytes());
i++;
deleteEntity(bdo, KEYS[i]);
i++;
mDataFile.close();
openForReading();
BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
int out = 0;
assertTrue(bdi.readNextHeader());
readAndVerifyDeletedEntity(bdi, KEYS[out]);
out++;
assertTrue(bdi.readNextHeader());
readAndVerifyEntity(bdi, KEYS[out], DATA[out].getBytes());
out++;
assertTrue(bdi.readNextHeader());
readAndVerifyEntity(bdi, KEYS[out], DATA[out].getBytes());
out++;
assertTrue(bdi.readNextHeader());
readAndVerifyDeletedEntity(bdi, KEYS[out]);
out++;
assertFalse("four items in this stream", bdi.readNextHeader());
}
use of android.app.backup.BackupDataOutput in project android_frameworks_base by DirtyUnicorns.
the class BackupDataTest method testDelete.
public void testDelete() throws IOException {
mFile = new File(mDirectory, "backup_delete_test.dat");
openForWriting();
BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
for (int i = 0; i < KEYS.length; i++) {
deleteEntity(bdo, KEYS[i]);
}
mDataFile.close();
openForReading();
BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
int count = 0;
while (bdi.readNextHeader()) {
readAndVerifyDeletedEntity(bdi, KEYS[count]);
count++;
}
assertEquals("four deletes in this stream", KEYS.length, count);
}
Aggregations