Search in sources :

Example 26 with BackupDataOutput

use of android.app.backup.BackupDataOutput in project android_frameworks_base by crdroidandroid.

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());
}
Also used : BackupDataInput(android.app.backup.BackupDataInput) File(java.io.File) BackupDataOutput(android.app.backup.BackupDataOutput)

Example 27 with BackupDataOutput

use of android.app.backup.BackupDataOutput in project android_frameworks_base by crdroidandroid.

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;
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) BackupDataOutput(android.app.backup.BackupDataOutput) FileInputStream(java.io.FileInputStream)

Aggregations

BackupDataOutput (android.app.backup.BackupDataOutput)27 File (java.io.File)27 BackupDataInput (android.app.backup.BackupDataInput)20 FileInputStream (java.io.FileInputStream)7 IOException (java.io.IOException)7