Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 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 | |
| 17 | package android.backup; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | |
| 21 | import java.io.FileDescriptor; |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 22 | import java.io.IOException; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 23 | |
Joe Onorato | 290bb01 | 2009-05-13 18:57:29 -0400 | [diff] [blame] | 24 | /** @hide */ |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 25 | public class BackupDataOutput { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 26 | int mBackupWriter; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 27 | |
| 28 | public static final int OP_UPDATE = 1; |
| 29 | public static final int OP_DELETE = 2; |
| 30 | |
Joe Onorato | 83248c4 | 2009-06-17 17:55:20 -0700 | [diff] [blame] | 31 | public BackupDataOutput(FileDescriptor fd) { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 32 | 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 Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 39 | // A dataSize of -1 indicates that the record under this key should be deleted |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 40 | 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 Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 58 | public void setKeyPrefix(String keyPrefix) { |
| 59 | setKeyPrefix_native(mBackupWriter, keyPrefix); |
| 60 | } |
| 61 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 62 | protected void finalize() throws Throwable { |
| 63 | try { |
| 64 | dtor(mBackupWriter); |
| 65 | } finally { |
| 66 | super.finalize(); |
| 67 | } |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 68 | } |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 69 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 70 | private native static int ctor(FileDescriptor fd); |
| 71 | private native static void dtor(int mBackupWriter); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 72 | |
| 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 Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 75 | private native static void setKeyPrefix_native(int mBackupWriter, String keyPrefix); |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 76 | } |
| 77 | |