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; 22 import java.io.IOException; 23 24 /** @hide */ 25 public class BackupDataOutput { 26 int mBackupWriter; 27 28 public static final int OP_UPDATE = 1; 29 public static final int OP_DELETE = 2; 30 BackupDataOutput(FileDescriptor fd)31 public BackupDataOutput(FileDescriptor fd) { 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 } 37 } 38 39 // A dataSize of -1 indicates that the record under this key should be deleted writeEntityHeader(String key, int dataSize)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 writeEntityData(byte[] data, int size)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 setKeyPrefix(String keyPrefix)58 public void setKeyPrefix(String keyPrefix) { 59 setKeyPrefix_native(mBackupWriter, keyPrefix); 60 } 61 finalize()62 protected void finalize() throws Throwable { 63 try { 64 dtor(mBackupWriter); 65 } finally { 66 super.finalize(); 67 } 68 } 69 ctor(FileDescriptor fd)70 private native static int ctor(FileDescriptor fd); dtor(int mBackupWriter)71 private native static void dtor(int mBackupWriter); 72 writeEntityHeader_native(int mBackupWriter, String key, int dataSize)73 private native static int writeEntityHeader_native(int mBackupWriter, String key, int dataSize); writeEntityData_native(int mBackupWriter, byte[] data, int size)74 private native static int writeEntityData_native(int mBackupWriter, byte[] data, int size); setKeyPrefix_native(int mBackupWriter, String keyPrefix)75 private native static void setKeyPrefix_native(int mBackupWriter, String keyPrefix); 76 } 77 78