android_backup_BackupDataOutput.cpp revision d2110dbce071a236b6176de344ca797b737542eb
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#define LOG_TAG "FileBackupHelper_native" 18#include <utils/Log.h> 19 20#include "JNIHelp.h" 21#include <android_runtime/AndroidRuntime.h> 22 23#include <utils/backup_helpers.h> 24 25namespace android 26{ 27 28static jfieldID s_descriptorField = 0; 29 30static int 31ctor_native(JNIEnv* env, jobject This, jobject fileDescriptor) 32{ 33 int err; 34 35 int fd = env->GetIntField(fileDescriptor, s_descriptorField); 36 if (fd == -1) { 37 return NULL; 38 } 39 40 return (int)new BackupDataWriter(fd); 41} 42 43static void 44dtor_native(JNIEnv* env, jobject This, int fd) 45{ 46 delete (BackupDataWriter*)fd; 47} 48 49static const JNINativeMethod g_methods[] = { 50 { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native }, 51 { "dtor", "(I)V", (void*)dtor_native }, 52}; 53 54int register_android_backup_BackupDataOutput(JNIEnv* env) 55{ 56 LOGD("register_android_backup_BackupDataOutput"); 57 58 jclass clazz; 59 60 clazz = env->FindClass("java/io/FileDescriptor"); 61 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor"); 62 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I"); 63 LOG_FATAL_IF(s_descriptorField == NULL, 64 "Unable to find descriptor field in java.io.FileDescriptor"); 65 66 return AndroidRuntime::registerNativeMethods(env, "android/backup/BackupDataOutput", 67 g_methods, NELEM(g_methods)); 68} 69 70} 71