本文整理汇总了Java中android.app.backup.BackupDataInput.readNextHeader方法的典型用法代码示例。如果您正苦于以下问题:Java BackupDataInput.readNextHeader方法的具体用法?Java BackupDataInput.readNextHeader怎么用?Java BackupDataInput.readNextHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.app.backup.BackupDataInput
的用法示例。
在下文中一共展示了BackupDataInput.readNextHeader方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onRestore
import android.app.backup.BackupDataInput; //导入方法依赖的package包/类
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
Log.i(TAG, "Restoring from backup");
while (data.readNextHeader()) {
String key = data.getKey();
Log.d(TAG, "Restoring entity " + key);
if (key.equals(PREFERENCES_ENTITY)) {
restorePreferences(data);
} else {
Log.e(TAG, "Found unknown backup entity: " + key);
data.skipEntityData();
}
}
Log.i(TAG, "Done restoring from backup");
}
示例2: transferIncrementalBackupData
import android.app.backup.BackupDataInput; //导入方法依赖的package包/类
private int transferIncrementalBackupData(BackupDataInput backupDataInput)
throws IOException, InvalidAlgorithmParameterException, InvalidKeyException {
ZipOutputStream outputStream = backupState.getOutputStream();
int bufferSize = INITIAL_BUFFER_SIZE;
byte[] buffer = new byte[bufferSize];
while (backupDataInput.readNextHeader()) {
String chunkFileName = Base64.encodeToString(backupDataInput.getKey().getBytes(), Base64.DEFAULT);
int dataSize = backupDataInput.getDataSize();
if (dataSize >= 0) {
ZipEntry zipEntry = new ZipEntry(configuration.getIncrementalBackupDirectory() +
backupState.getPackageName() + "/" + chunkFileName);
outputStream.putNextEntry(zipEntry);
if (dataSize > bufferSize) {
bufferSize = dataSize;
buffer = new byte[bufferSize];
}
backupDataInput.readEntityData(buffer, 0, dataSize);
try {
outputStream.write(buffer, 0, dataSize);
} catch (Exception ex) {
Log.e(TAG, "Error performing incremental backup for " + backupState.getPackageName() + ": ", ex);
clearBackupState(true);
return TRANSPORT_ERROR;
}
}
}
return TRANSPORT_OK;
}
示例3: onRestore
import android.app.backup.BackupDataInput; //导入方法依赖的package包/类
/**
* On restore, we pull the various bits of data out of the restore stream,
* then reconstruct the application's data file inside the shared lock. A
* restore data set will always be the full set of records supplied by the
* application's backup operations.
*/
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
// Consume the restore data set, remembering each bit of application state
// that we see along the way
while (data.readNextHeader()) {
String key = data.getKey();
int dataSize = data.getDataSize();
// In this implementation, we trust that we won't see any record keys
// that we don't understand. Since we expect to handle them all, we
// go ahead and extract the data for each record before deciding how
// it will be handled.
byte[] dataBuf = new byte[dataSize];
data.readEntityData(dataBuf, 0, dataSize);
ByteArrayInputStream instream = new ByteArrayInputStream(dataBuf);
DataInputStream in = new DataInputStream(instream);
if (FILLING_KEY.equals(key)) {
mFilling = in.readInt();
} else if (MAYO_KEY.equals(key)) {
mAddMayo = in.readBoolean();
} else if (TOMATO_KEY.equals(key)) {
mAddTomato = in.readBoolean();
}
}
// Now we're ready to write out a full new dataset for the application. Note that
// the restore process is intended to *replace* any existing or default data, so
// we can just go ahead and overwrite it all.
synchronized (BackupRestoreActivity.sDataLock) {
RandomAccessFile file = new RandomAccessFile(mDataFile, "rw");
file.setLength(0L);
file.writeInt(mFilling);
file.writeBoolean(mAddMayo);
file.writeBoolean(mAddTomato);
}
// Finally, write the state file that describes our data as of this restore pass.
writeStateFile(newState);
}
示例4: onRestore
import android.app.backup.BackupDataInput; //导入方法依赖的package包/类
/**
* This application does not do any "live" restores of its own data,
* so the only time a restore will happen is when the application is
* installed. This means that the activity itself is not going to
* be running while we change its data out from under it. That, in
* turn, means that there is no need to send out any sort of notification
* of the new data: we only need to read the data from the stream
* provided here, build the application's new data file, and then
* write our new backup state blob that will be consulted at the next
* backup operation.
*
* <p>We don't bother checking the versionCode of the app who originated
* the data because we have never revised the backup data format. If
* we had, the 'appVersionCode' parameter would tell us how we should
* interpret the data we're about to read.
*/
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
// We should only see one entity in the data stream, but the safest
// way to consume it is using a while() loop
while (data.readNextHeader()) {
String key = data.getKey();
int dataSize = data.getDataSize();
if (APP_DATA_KEY.equals(key)) {
// It's our saved data, a flattened chunk of data all in
// one buffer. Use some handy structured I/O classes to
// extract it.
byte[] dataBuf = new byte[dataSize];
data.readEntityData(dataBuf, 0, dataSize);
ByteArrayInputStream baStream = new ByteArrayInputStream(dataBuf);
DataInputStream in = new DataInputStream(baStream);
mFilling = in.readInt();
mAddMayo = in.readBoolean();
mAddTomato = in.readBoolean();
// Now we are ready to construct the app's data file based
// on the data we are restoring from.
synchronized (BackupRestoreActivity.sDataLock) {
RandomAccessFile file = new RandomAccessFile(mDataFile, "rw");
file.setLength(0L);
file.writeInt(mFilling);
file.writeBoolean(mAddMayo);
file.writeBoolean(mAddTomato);
}
} else {
// Curious! This entity is data under a key we do not
// understand how to process. Just skip it.
data.skipEntityData();
}
}
// The last thing to do is write the state blob that describes the
// app's data as restored from backup.
writeStateFile(newState);
}