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