Code example for BackupDataInputStream

Methods: getKeyreadsize

0
     * @see android.app.backup.BackupHelper#restoreEntity(android.app.backup. 
     * BackupDataInputStream) 
     */ 
    @Override 
    public void restoreEntity(BackupDataInputStream data) {
        if (ACCOUNTS_BACKUP_KEY.equalsIgnoreCase(data.getKey())) {
            try { 
                String profilesStr = readData(data);
                JSONArray accounts = new JSONArray(profilesStr);
                if (accounts != null && accounts.length() > 0) {
                    SipProfileJson.restoreSipAccounts(mContext, accounts);
                } 
            } catch (IOException e) {
                Log.e(THIS_FILE, "Cannot restore backup entry", e);
            } catch (JSONException e) {
                Log.e(THIS_FILE, "Cannot parse backup entry", e);
            } 
        } 
    } 
 
    /* 
     * (non-Javadoc) 
     * @see android.app.backup.BackupHelper#writeNewStateDescription(android.os. 
     * ParcelFileDescriptor) 
     */ 
    @Override 
    public void writeNewStateDescription(ParcelFileDescriptor newState) {
        long fileModified = databaseFile.lastModified();
        try { 
            FileOutputStream outstream = new FileOutputStream(newState.getFileDescriptor());
            DataOutputStream out = new DataOutputStream(outstream);
            out.writeLong(fileModified);
            out.close();
        } catch (IOException e) {
            Log.e(THIS_FILE, "Cannot manage final local backup state", e);
        } 
    } 
 
    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);
    } 
 
    /** 
     * Read data from the input stream 
     *  
     * @param data the input stream 
     * @return the data 
     * @throws IOException I/O error 
     */ 
    private String readData(BackupDataInputStream data) throws IOException {
        String dataS;
        byte[] buf = new byte[data.size()];
        data.read(buf, 0, buf.length);
        ByteArrayInputStream bais = new ByteArrayInputStream(buf);
        DataInputStream dis = new DataInputStream(bais);
        dataS = dis.readUTF();
        dis.close();
        bais.close();
Experience pair programming with AI  Get Codota for Java