BackupDataOutput.java revision 4528186e0d65fc68ef0dd1941aa2ac8aefcd55a3
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 java.io.FileDescriptor;
20import java.io.IOException;
21
22/**
23 * STOPSHIP: document
24 */
25public class BackupDataOutput {
26    int mBackupWriter;
27
28    public static final int OP_UPDATE = 1;
29    public static final int OP_DELETE = 2;
30
31    /** @hide */
32    public BackupDataOutput(FileDescriptor fd) {
33        if (fd == null) throw new NullPointerException();
34        mBackupWriter = ctor(fd);
35        if (mBackupWriter == 0) {
36            throw new RuntimeException("Native initialization failed with fd=" + fd);
37        }
38    }
39
40    /**
41     * Mark the beginning of one record in the backup data stream.
42     *
43     * @param key
44     * @param dataSize The size in bytes of this record's data.  Passing a dataSize
45     *    of -1 indicates that the record under this key should be deleted.
46     * @return The number of bytes written to the backup stream
47     * @throws IOException if the write failed
48     */
49    public int writeEntityHeader(String key, int dataSize) throws IOException {
50        int result = writeEntityHeader_native(mBackupWriter, key, dataSize);
51        if (result >= 0) {
52            return result;
53        } else {
54            throw new IOException("result=0x" + Integer.toHexString(result));
55        }
56    }
57
58    /**
59     * Write a chunk of data under the current entity to the backup transport.
60     * @param data A raw data buffer to send
61     * @param size The number of bytes to be sent in this chunk
62     * @return the number of bytes written
63     * @throws IOException if the write failed
64     */
65    public int writeEntityData(byte[] data, int size) throws IOException {
66        int result = writeEntityData_native(mBackupWriter, data, size);
67        if (result >= 0) {
68            return result;
69        } else {
70            throw new IOException("result=0x" + Integer.toHexString(result));
71        }
72    }
73
74    public void setKeyPrefix(String keyPrefix) {
75        setKeyPrefix_native(mBackupWriter, keyPrefix);
76    }
77
78    /** @hide */
79    protected void finalize() throws Throwable {
80        try {
81            dtor(mBackupWriter);
82        } finally {
83            super.finalize();
84        }
85    }
86
87    private native static int ctor(FileDescriptor fd);
88    private native static void dtor(int mBackupWriter);
89
90    private native static int writeEntityHeader_native(int mBackupWriter, String key, int dataSize);
91    private native static int writeEntityData_native(int mBackupWriter, byte[] data, int size);
92    private native static void setKeyPrefix_native(int mBackupWriter, String keyPrefix);
93}
94
95