1ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes/* 2ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 3ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * 4ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * you may not use this file except in compliance with the License. 6ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * You may obtain a copy of the License at 7ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * 8ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * 10ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * See the License for the specific language governing permissions and 14ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes * limitations under the License. 15ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes */ 16ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 17ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes#define LOG_TAG "ExecStrings" 18ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 19ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes#include "ExecStrings.h" 20ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 21ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes#include <stdlib.h> 22ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 23ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes#include "cutils/log.h" 24ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes#include "ScopedLocalRef.h" 25ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 26ad9208affa02f92a6b85354a33123d51d80febe1Elliott HughesExecStrings::ExecStrings(JNIEnv* env, jobjectArray java_string_array) 27620bd0d151abcc1bd45d176878ca3e2a12de0ae0Ian Rogers : env_(env), java_array_(java_string_array), array_(NULL) { 28ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes if (java_array_ == NULL) { 29ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes return; 30ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes } 31ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 32ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes jsize length = env_->GetArrayLength(java_array_); 33ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes array_ = new char*[length + 1]; 34ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes array_[length] = NULL; 35ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes for (jsize i = 0; i < length; ++i) { 36ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes ScopedLocalRef<jstring> java_string(env_, reinterpret_cast<jstring>(env_->GetObjectArrayElement(java_array_, i))); 37ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes // We need to pass these strings to const-unfriendly code. 38ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes char* string = const_cast<char*>(env_->GetStringUTFChars(java_string.get(), NULL)); 39ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes array_[i] = string; 40ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes } 41ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes} 42ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 43ad9208affa02f92a6b85354a33123d51d80febe1Elliott HughesExecStrings::~ExecStrings() { 44ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes if (array_ == NULL) { 45ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes return; 46ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes } 47ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 48ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes // Temporarily clear any pending exception so we can clean up. 49ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes jthrowable pending_exception = env_->ExceptionOccurred(); 50ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes if (pending_exception != NULL) { 51ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes env_->ExceptionClear(); 52ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes } 53ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 54ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes jsize length = env_->GetArrayLength(java_array_); 55ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes for (jsize i = 0; i < length; ++i) { 56ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes ScopedLocalRef<jstring> java_string(env_, reinterpret_cast<jstring>(env_->GetObjectArrayElement(java_array_, i))); 57ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes env_->ReleaseStringUTFChars(java_string.get(), array_[i]); 58ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes } 59ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes delete[] array_; 60ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 61ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes // Re-throw any pending exception. 62ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes if (pending_exception != NULL) { 63ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes if (env_->Throw(pending_exception) < 0) { 64ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes ALOGE("Error rethrowing exception!"); 65ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes } 66ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes } 67ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes} 68ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes 69ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hugheschar** ExecStrings::get() { 70ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes return array_; 71ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes} 72