本文整理汇总了Java中android.app.backup.BackupDataOutput.writeEntityHeader方法的典型用法代码示例。如果您正苦于以下问题:Java BackupDataOutput.writeEntityHeader方法的具体用法?Java BackupDataOutput.writeEntityHeader怎么用?Java BackupDataOutput.writeEntityHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.app.backup.BackupDataOutput
的用法示例。
在下文中一共展示了BackupDataOutput.writeEntityHeader方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeRowToBackup
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private void writeRowToBackup(Key key, byte[] blob, Journal out,
BackupDataOutput data) throws IOException {
String backupKey = keyToBackupKey(key);
data.writeEntityHeader(backupKey, blob.length);
data.writeEntityData(blob, blob.length);
out.rows++;
out.bytes += blob.length;
if (VERBOSE) Log.v(TAG, "saving " + geKeyType(key) + " " + backupKey + ": " +
getKeyName(key) + "/" + blob.length);
if(DEBUG_PAYLOAD) {
String encoded = Base64.encodeToString(blob, 0, blob.length, Base64.NO_WRAP);
final int chunkSize = 1024;
for (int offset = 0; offset < encoded.length(); offset += chunkSize) {
int end = offset + chunkSize;
end = Math.min(end, encoded.length());
Log.w(TAG, "wrote " + encoded.substring(offset, end));
}
}
}
示例2: backupCursor
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private void backupCursor(BackupDataOutput data,
ContentResolver contentResolver, Cursor cursor) {
StringBuilder backedUp = new StringBuilder();
int idColumn = cursor.getColumnIndex(ID_COLUMN);
while (cursor.moveToNext()) {
int id = cursor.getInt(idColumn);
byte[] serialized = mAdaptor.Serialize(cursor);
try {
data.writeEntityHeader("row_" + id, serialized.length);
data.writeEntityData(serialized, serialized.length);
if (backedUp.length() != 0) backedUp.append(',');
backedUp.append(id);
} catch (IOException e) {
Log.e("AltidroidBackup", "cannot backup: " + id);
e.printStackTrace();
}
}
// Mark all entries that were just backed up as such
if (backedUp.length() > 0) {
ContentValues values = new ContentValues();
values.put("backed_up", true);
contentResolver.update(mUri, values, "_id IN (" + backedUp.toString() + ")", null);
}
}
示例3: writeData
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private void writeData(BackupDataOutput data, String value) throws IOException {
// Create buffer stream and data output stream for our data
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
DataOutputStream outWriter = new DataOutputStream(bufStream);
// Write structured data
outWriter.writeUTF(value);
// Send the data to the Backup Manager via the BackupDataOutput
byte[] buffer = bufStream.toByteArray();
int len = buffer.length;
data.writeEntityHeader(ACCOUNTS_BACKUP_KEY, len);
data.writeEntityData(buffer, len);
}
示例4: writeData
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private void writeData(BackupDataOutput data, String value) throws IOException {
// Create buffer stream and data output stream for our data
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
DataOutputStream outWriter = new DataOutputStream(bufStream);
// Write structured data
outWriter.writeUTF(value);
// Send the data to the Backup Manager via the BackupDataOutput
byte[] buffer = bufStream.toByteArray();
int len = buffer.length;
data.writeEntityHeader(SETTINGS_BACKUP_KEY, len);
data.writeEntityData(buffer, len);
}
示例5: transferIncrementalRestoreData
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private int transferIncrementalRestoreData(String packageName, ParcelFileDescriptor outputFileDescriptor)
throws IOException, InvalidAlgorithmParameterException, InvalidKeyException {
ParcelFileDescriptor inputFileDescriptor = buildInputFileDescriptor();
ZipInputStream inputStream = buildInputStream(inputFileDescriptor);
BackupDataOutput backupDataOutput = new BackupDataOutput(outputFileDescriptor.getFileDescriptor());
Optional<ZipEntry> zipEntryOptional = seekToEntry(inputStream,
configuration.getIncrementalBackupDirectory() + packageName);
while (zipEntryOptional.isPresent()) {
String fileName = new File(zipEntryOptional.get().getName()).getName();
String blobKey = new String(Base64.decode(fileName, Base64.DEFAULT));
byte[] backupData = Streams.readFullyNoClose(inputStream);
backupDataOutput.writeEntityHeader(blobKey, backupData.length);
backupDataOutput.writeEntityData(backupData, backupData.length);
inputStream.closeEntry();
zipEntryOptional = seekToEntry(inputStream, configuration.getIncrementalBackupDirectory() + packageName);
}
IoUtils.closeQuietly(inputFileDescriptor);
IoUtils.closeQuietly(outputFileDescriptor);
return TRANSPORT_OK;
}
示例6: writeRowToBackup
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private void writeRowToBackup(String backupKey, MessageNano proto,
BackupDataOutput data) throws IOException {
byte[] blob = writeCheckedBytes(proto);
data.writeEntityHeader(backupKey, blob.length);
data.writeEntityData(blob, blob.length);
mBackupDataWasUpdated = true;
if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey);
}
示例7: backupPreferences
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private void backupPreferences(BackupDataOutput data,
SharedPreferences preferences) throws IOException {
PreferenceBackupHelper preferenceDumper = createPreferenceBackupHelper();
byte[] dumpedContents = preferenceDumper.exportPreferences(preferences);
data.writeEntityHeader(PREFERENCES_ENTITY, dumpedContents.length);
data.writeEntityData(dumpedContents, dumpedContents.length);
}
示例8: flushBufferData
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private static void flushBufferData(BackupDataOutput paramBackupDataOutput, ByteArrayOutputStream paramByteArrayOutputStream, String paramString)
throws IOException
{
byte[] arrayOfByte = paramByteArrayOutputStream.toByteArray();
paramBackupDataOutput.writeEntityHeader(paramString, arrayOfByte.length);
paramBackupDataOutput.writeEntityData(arrayOfByte, arrayOfByte.length);
paramByteArrayOutputStream.reset();
}
示例9: removeDeletedKeysFromBackup
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private int removeDeletedKeysFromBackup(Set<String> deletedIds, BackupDataOutput data)
throws IOException {
int rows = 0;
for(String deleted: deletedIds) {
if (VERBOSE) Log.v(TAG, "dropping deleted item " + deleted);
data.writeEntityHeader(deleted, -1);
rows++;
}
return rows;
}
示例10: writeRowToBackup
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
private void writeRowToBackup(String backupKey, MessageNano proto,
BackupDataOutput data) throws IOException {
byte[] blob = writeCheckedBytes(proto);
data.writeEntityHeader(backupKey, blob.length);
data.writeEntityData(blob, blob.length);
if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey);
}
示例11: writeBackupEntity
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
void writeBackupEntity(BackupDataOutput data, ByteArrayOutputStream bufStream, String key)
throws IOException {
byte[] buf = bufStream.toByteArray();
data.writeEntityHeader(key, buf.length);
data.writeEntityData(buf, buf.length);
}
示例12: onBackup
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
/**
* The set of data backed up by this application is very small: just
* two booleans and an integer. With such a simple dataset, it's
* easiest to simply store a copy of the backed-up data as the state
* blob describing the last dataset backed up. The state file
* contents can be anything; it is private to the agent class, and
* is never stored off-device.
*
* <p>One thing that an application may wish to do is tag the state
* blob contents with a version number. This is so that if the
* application is upgraded, the next time it attempts to do a backup,
* it can detect that the last backup operation was performed by an
* older version of the agent, and might therefore require different
* handling.
*/
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
ParcelFileDescriptor newState) throws IOException {
// First, get the current data from the application's file. This
// may throw an IOException, but in that case something has gone
// badly wrong with the app's data on disk, and we do not want
// to back up garbage data. If we just let the exception go, the
// Backup Manager will handle it and simply skip the current
// backup operation.
synchronized (BackupRestoreActivity.sDataLock) {
RandomAccessFile file = new RandomAccessFile(mDataFile, "r");
mFilling = file.readInt();
mAddMayo = file.readBoolean();
mAddTomato = file.readBoolean();
}
// If the new state file descriptor is null, this is the first time
// a backup is being performed, so we know we have to write the
// data. If there <em>is</em> a previous state blob, we want to
// double check whether the current data is actually different from
// our last backup, so that we can avoid transmitting redundant
// data to the storage backend.
boolean doBackup = (oldState == null);
if (!doBackup) {
doBackup = compareStateFile(oldState);
}
// If we decided that we do in fact need to write our dataset, go
// ahead and do that. The way this agent backs up the data is to
// flatten it into a single buffer, then write that to the backup
// transport under the single key string.
if (doBackup) {
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
// We use a DataOutputStream to write structured data into
// the buffering stream
DataOutputStream outWriter = new DataOutputStream(bufStream);
outWriter.writeInt(mFilling);
outWriter.writeBoolean(mAddMayo);
outWriter.writeBoolean(mAddTomato);
// Okay, we've flattened the data for transmission. Pull it
// out of the buffering stream object and send it off.
byte[] buffer = bufStream.toByteArray();
int len = buffer.length;
data.writeEntityHeader(APP_DATA_KEY, len);
data.writeEntityData(buffer, len);
}
// Finally, in all cases, we need to write the new state blob
writeStateFile(newState);
}
示例13: performBackup
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
/**
* Back up launcher data so we can restore the user's state on a new device.
*
* <P>The journal is a timestamp and a list of keys that were saved as of that time.
*
* <P>Keys may come back in any order, so each key/value is one complete row of the database.
*
* @param oldState notes from the last backup
* @param data incremental key/value pairs to persist off-device
* @param newState notes for the next backup
*/
@Override
public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
ParcelFileDescriptor newState) {
if (VERBOSE) Log.v(TAG, "onBackup");
Journal in = readJournal(oldState);
if (!launcherIsReady()) {
// Perform backup later.
writeJournal(newState, in);
return;
}
Log.v(TAG, "lastBackupTime = " + in.t);
mKeys.clear();
applyJournal(in);
// Record the time before performing backup so that entries edited while the backup
// was going on, do not get missed in next backup.
long newBackupTime = System.currentTimeMillis();
try {
backupFavorites(data);
backupScreens(data);
backupIcons(data);
backupWidgets(data);
// Delete any key which still exist in the old backup, but is not valid anymore.
HashSet<String> validKeys = new HashSet<String>();
for (Key key : mKeys) {
validKeys.add(keyToBackupKey(key));
}
mExistingKeys.removeAll(validKeys);
// Delete anything left in the existing keys.
for (String deleted: mExistingKeys) {
if (VERBOSE) Log.v(TAG, "dropping deleted item " + deleted);
data.writeEntityHeader(deleted, -1);
}
mExistingKeys.clear();
mLastBackupRestoreTime = newBackupTime;
// We store the journal at two places.
// 1) Storing it in newState allows us to do partial backups by comparing old state
// 2) Storing it in backup data allows us to validate keys during restore
Journal state = getCurrentStateJournal();
writeRowToBackup(JOURNAL_KEY, state, data);
} catch (IOException e) {
Log.e(TAG, "launcher backup has failed", e);
}
writeNewStateDescription(newState);
}
示例14: performBackup
import android.app.backup.BackupDataOutput; //导入方法依赖的package包/类
@Override
public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) {
Log.i(TAG, "-------------------------------------------------");
Log.i(TAG, "--- performBackup : key = " + backupKey);
// State
// oldStateFd can be null
FileDescriptor oldStateFd = oldState != null ? oldState.getFileDescriptor() : null;
FileDescriptor newStateFd = newState.getFileDescriptor();
if (newStateFd == null) {
throw new NullPointerException();
}
// Doing copy
ByteArrayOutputStream dataCopy = copyTableAsBytes();
if (dataCopy != null) {
try {
byte[] dataBytes = dataCopy.toByteArray();
data.writeEntityHeader(backupKey, dataBytes.length);
data.writeEntityData(dataBytes, dataBytes.length);
Log.i(TAG, "performBackup Entity '" + backupKey + "' size=" + dataBytes.length);
} catch (IOException e) {
Log.e(TAG, "Error during Backup Data : " + e.getMessage(), e);
}
}
// Write State
// FileOutputStream outstream = new
// FileOutputStream(newState.getFileDescriptor());
// DataOutputStream out = new DataOutputStream(outstream);
// try {
// long modified = System.currentTimeMillis(); //
// mDataFile.lastModified();
// out.writeLong(modified);
// } catch (IOException e) {
// Log.e(TAG, "Error Write State : " + e.getMessage(), e);
// } finally {
// try {
// out.close();
// outstream.close();
// } catch (IOException e) {
// Log.e(TAG, "Error Closing State : " + e.getMessage(), e);
// }
//
// }
Log.i(TAG, "----- performBackup End : key = " + backupKey);
Log.i(TAG, "-------------------------------------------------");
}