Most visited

Recently visited

Added in API level 8

BackupDataInput

public class BackupDataInput
extends Object

java.lang.Object
   ↳ android.app.backup.BackupDataInput


提供结构化接口, BackupAgent通过其onRestore()方法从备份数据集中读取信息。 数据呈现为一组“实体”,如之前所代理的存储每一个代表一个名为记录onBackup()实施。 一个实体由一个描述性标题和一个字节数组组成,该数组包含保存在远程备份中的原始数据。

代理程序必须使用数据流中的每个实体,否则应用程序的恢复状态将不完整。

Example

典型的 onRestore()实现可能是这样构成的:

 public void onRestore(BackupDataInput data, int appVersionCode,
                       ParcelFileDescriptor newState) {
     while (data.readNextHeader()) {
         String key = data.getKey();
         int dataSize = data.getDataSize();

         if (key.equals(MY_BACKUP_KEY_ONE)) {
             // process this kind of record here
             byte[] buffer = new byte[dataSize];
             data.readEntityData(buffer, 0, dataSize); // reads the entire entity at once

             // now 'buffer' holds the raw data and can be processed however
             // the agent wishes
             processBackupKeyOne(buffer);
         } else if (key.equals(MY_BACKUP_KEY_TO_IGNORE) {
             // a key we recognize but wish to discard
             data.skipEntityData();
         } // ... etc.
    }
 }

Summary

Public methods

int getDataSize()

报告恢复流中与当前实体关联的数据的大小(以字节为单位)。

String getKey()

在恢复流中报告与当前实体关联的密钥

int readEntityData(byte[] data, int offset, int size)

从还原流中读取记录的原始数据。

boolean readNextHeader()

从恢复流中提取下一个实体头。

void skipEntityData()

消费当前实体的数据,而不将其提取到缓冲区中以供进一步处理。

Inherited methods

From class java.lang.Object

Public methods

getDataSize

Added in API level 8
int getDataSize ()

报告恢复流中与当前实体关联的数据的大小(以字节为单位)。

Returns
int The size of the record's raw data, in bytes
Throws
IllegalStateException if the next record header has not yet been read

getKey

Added in API level 8
String getKey ()

在恢复流中报告与当前实体关联的密钥

Returns
String the current entity's key string
Throws
IllegalStateException if the next record header has not yet been read

readEntityData

Added in API level 8
int readEntityData (byte[] data, 
                int offset, 
                int size)

从还原流中读取记录的原始数据。 该记录的标题必须首先由readNextHeader()方法处理。 可以对这个方法进行多次调用,以便以块处理数据; 不是所有的都必须在一次通话中阅读。 一旦读取了当前实体的所有原始数据,对该方法的进一步调用将简单地归零。

Parameters
data byte: An allocated byte array of at least 'size' bytes
offset int: Offset within the 'data' array at which the data will be placed when read from the stream
size int: The number of bytes to read in this pass
Returns
int The number of bytes of data read. Once all of the data for this entity has been read, further calls to this method will return zero.
Throws
IOException if an error occurred when trying to read the restore data stream

readNextHeader

Added in API level 8
boolean readNextHeader ()

从恢复流中提取下一个实体头。 在此方法返回成功之后,可以使用getKey()getDataSize()方法检查现在可供处理的实体。

Returns
boolean true when there is an entity ready for consumption from the restore stream, false if the restore stream has been fully consumed.
Throws
IOException if an error occurred while reading the restore stream

skipEntityData

Added in API level 8
void skipEntityData ()

消费当前实体的数据,而不将其提取到缓冲区中以供进一步处理。 这允许BackupAgent在恢复操作期间有效地丢弃过时的或不感兴趣的记录。

Throws
IOException if an error occurred when trying to read the restore data stream

Hooray!