blob: d29c5ba02f1403cfdf8cfa533751cb9f5321e3ff [file] [log] [blame]
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001/*
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.backup;
18
19import android.content.Context;
20
21import java.io.FileDescriptor;
Joe Onorato1cf58742009-06-12 11:06:24 -070022import java.io.IOException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070023
Joe Onorato290bb012009-05-13 18:57:29 -040024/** @hide */
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070025public class BackupDataOutput {
Joe Onoratod2110db2009-05-19 13:41:21 -070026 int mBackupWriter;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070027
28 public static final int OP_UPDATE = 1;
29 public static final int OP_DELETE = 2;
30
Joe Onorato83248c42009-06-17 17:55:20 -070031 public BackupDataOutput(FileDescriptor fd) {
Joe Onoratod2110db2009-05-19 13:41:21 -070032 if (fd == null) throw new NullPointerException();
33 mBackupWriter = ctor(fd);
34 if (mBackupWriter == 0) {
35 throw new RuntimeException("Native initialization failed with fd=" + fd);
36 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070037 }
38
Christopher Tate6785dd82009-06-18 15:58:25 -070039 // A dataSize of -1 indicates that the record under this key should be deleted
Joe Onorato1cf58742009-06-12 11:06:24 -070040 public int writeEntityHeader(String key, int dataSize) throws IOException {
41 int result = writeEntityHeader_native(mBackupWriter, key, dataSize);
42 if (result >= 0) {
43 return result;
44 } else {
45 throw new IOException("result=0x" + Integer.toHexString(result));
46 }
47 }
48
49 public int writeEntityData(byte[] data, int size) throws IOException {
50 int result = writeEntityData_native(mBackupWriter, data, size);
51 if (result >= 0) {
52 return result;
53 } else {
54 throw new IOException("result=0x" + Integer.toHexString(result));
55 }
56 }
57
Joe Onorato06290a42009-06-18 20:10:37 -070058 public void setKeyPrefix(String keyPrefix) {
59 setKeyPrefix_native(mBackupWriter, keyPrefix);
60 }
61
Joe Onoratod2110db2009-05-19 13:41:21 -070062 protected void finalize() throws Throwable {
63 try {
64 dtor(mBackupWriter);
65 } finally {
66 super.finalize();
67 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070068 }
Joe Onorato06290a42009-06-18 20:10:37 -070069
Joe Onoratod2110db2009-05-19 13:41:21 -070070 private native static int ctor(FileDescriptor fd);
71 private native static void dtor(int mBackupWriter);
Joe Onorato1cf58742009-06-12 11:06:24 -070072
73 private native static int writeEntityHeader_native(int mBackupWriter, String key, int dataSize);
74 private native static int writeEntityData_native(int mBackupWriter, byte[] data, int size);
Joe Onorato06290a42009-06-18 20:10:37 -070075 private native static void setKeyPrefix_native(int mBackupWriter, String keyPrefix);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070076}
77