1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app.backup;
18
19import android.annotation.SystemApi;
20import android.os.ParcelFileDescriptor;
21import java.io.FileDescriptor;
22import java.io.IOException;
23
24/**
25 * Provides the structured interface through which a {@link BackupAgent} commits
26 * information to the backup data set, via its {@link
27 * BackupAgent#onBackup(ParcelFileDescriptor,BackupDataOutput,ParcelFileDescriptor)
28 * onBackup()} method.  Data written for backup is presented
29 * as a set of "entities," key/value pairs in which each binary data record "value" is
30 * named with a string "key."
31 * <p>
32 * To commit a data record to the backup transport, the agent's
33 * {@link BackupAgent#onBackup(ParcelFileDescriptor,BackupDataOutput,ParcelFileDescriptor)
34 * onBackup()} method first writes an "entity header" that supplies the key string for the record
35 * and the total size of the binary value for the record.  After the header has been
36 * written, the agent then writes the binary entity value itself.  The entity value can
37 * be written in multiple chunks if desired, as long as the total count of bytes written
38 * matches what was supplied to {@link #writeEntityHeader(String, int) writeEntityHeader()}.
39 * <p>
40 * Entity key strings are considered to be unique within a given application's backup
41 * data set. If a backup agent writes a new entity under an existing key string, its value will
42 * replace any previous value in the transport's remote data store.  You can remove a record
43 * entirely from the remote data set by writing a new entity header using the
44 * existing record's key, but supplying a negative <code>dataSize</code> parameter.
45 * When you do so, the agent does not need to call {@link #writeEntityData(byte[], int)}.
46 * <h3>Example</h3>
47 * <p>
48 * Here is an example illustrating a way to back up the value of a String variable
49 * called <code>mStringToBackUp</code>:
50 * <pre>
51 * static final String MY_STRING_KEY = "storedstring";
52 *
53 * public void {@link BackupAgent#onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor) onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)}
54 *         throws IOException {
55 *     ...
56 *     byte[] stringBytes = mStringToBackUp.getBytes();
57 *     data.writeEntityHeader(MY_STRING_KEY, stringBytes.length);
58 *     data.writeEntityData(stringBytes, stringBytes.length);
59 *     ...
60 * }</pre>
61 *
62 * @see BackupAgent
63 */
64public class BackupDataOutput {
65    long mBackupWriter;
66
67    /** @hide */
68    @SystemApi
69    public BackupDataOutput(FileDescriptor fd) {
70        if (fd == null) throw new NullPointerException();
71        mBackupWriter = ctor(fd);
72        if (mBackupWriter == 0) {
73            throw new RuntimeException("Native initialization failed with fd=" + fd);
74        }
75    }
76
77    /**
78     * Mark the beginning of one record in the backup data stream. This must be called before
79     * {@link #writeEntityData}.
80     * @param key A string key that uniquely identifies the data record within the application.
81     *    Keys whose first character is \uFF00 or higher are not valid.
82     * @param dataSize The size in bytes of this record's data.  Passing a dataSize
83     *    of -1 indicates that the record under this key should be deleted.
84     * @return The number of bytes written to the backup stream
85     * @throws IOException if the write failed
86     */
87    public int writeEntityHeader(String key, int dataSize) throws IOException {
88        int result = writeEntityHeader_native(mBackupWriter, key, dataSize);
89        if (result >= 0) {
90            return result;
91        } else {
92            throw new IOException("result=0x" + Integer.toHexString(result));
93        }
94    }
95
96    /**
97     * Write a chunk of data under the current entity to the backup transport.
98     * @param data A raw data buffer to send
99     * @param size The number of bytes to be sent in this chunk
100     * @return the number of bytes written
101     * @throws IOException if the write failed
102     */
103    public int writeEntityData(byte[] data, int size) throws IOException {
104        int result = writeEntityData_native(mBackupWriter, data, size);
105        if (result >= 0) {
106            return result;
107        } else {
108            throw new IOException("result=0x" + Integer.toHexString(result));
109        }
110    }
111
112    /** @hide */
113    public void setKeyPrefix(String keyPrefix) {
114        setKeyPrefix_native(mBackupWriter, keyPrefix);
115    }
116
117    /** @hide */
118    @Override
119    protected void finalize() throws Throwable {
120        try {
121            dtor(mBackupWriter);
122        } finally {
123            super.finalize();
124        }
125    }
126
127    private native static long ctor(FileDescriptor fd);
128    private native static void dtor(long mBackupWriter);
129
130    private native static int writeEntityHeader_native(long mBackupWriter, String key, int dataSize);
131    private native static int writeEntityData_native(long mBackupWriter, byte[] data, int size);
132    private native static void setKeyPrefix_native(long mBackupWriter, String keyPrefix);
133}
134
135