Search in sources :

Example 1 with BackupDataOutput

use of android.app.backup.BackupDataOutput in project platform_frameworks_base by android.

the class BackupDataTest method testMultiple.

public void testMultiple() throws IOException {
    mFile = new File(mDirectory, "backup_multiple_test.dat");
    openForWriting();
    BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
    for (int i = 0; i < KEYS.length; i++) {
        writeEntity(bdo, KEYS[i], DATA[i].getBytes());
    }
    mDataFile.close();
    openForReading();
    BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
    int count = 0;
    while (bdi.readNextHeader()) {
        readAndVerifyEntity(bdi, KEYS[count], DATA[count].getBytes());
        count++;
    }
    assertEquals("four entities in this stream", KEYS.length, count);
}
Also used : BackupDataInput(android.app.backup.BackupDataInput) File(java.io.File) BackupDataOutput(android.app.backup.BackupDataOutput)

Example 2 with BackupDataOutput

use of android.app.backup.BackupDataOutput in project platform_frameworks_base by android.

the class BackupDataTest method testSingle.

public void testSingle() throws IOException {
    mFile = new File(mDirectory, "backup_mixed_sinlge.dat");
    openForWriting();
    BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
    writeEntity(bdo, KEY1, DATA1.getBytes());
    mDataFile.close();
    openForReading();
    BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
    int count = 0;
    while (bdi.readNextHeader()) {
        readAndVerifyEntity(bdi, KEY1, DATA1.getBytes());
        count++;
    }
    assertEquals("only one entity in this stream", 1, count);
}
Also used : BackupDataInput(android.app.backup.BackupDataInput) File(java.io.File) BackupDataOutput(android.app.backup.BackupDataOutput)

Example 3 with BackupDataOutput

use of android.app.backup.BackupDataOutput in project platform_frameworks_base by android.

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 4 with BackupDataOutput

use of android.app.backup.BackupDataOutput in project platform_frameworks_base by android.

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)

Example 5 with BackupDataOutput

use of android.app.backup.BackupDataOutput in project XobotOS by xamarin.

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;
    }
}
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