1 /* 2 * Copyright (C) 2010 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 #pragma once 18 19 #include <stddef.h> 20 #include <string.h> 21 22 #include <jni.h> 23 24 #include "nativehelper_utils.h" 25 26 // Protect this with __has_include to cope with `stl: "none"` users. 27 #if __has_include(<string_view>) 28 #include <string_view> 29 #endif 30 31 // A smart pointer that provides read-only access to a Java string's UTF chars. 32 // Unlike GetStringUTFChars, we throw NullPointerException rather than abort if 33 // passed a null jstring, and c_str will return nullptr. 34 // This makes the correct idiom very simple: 35 // 36 // ScopedUtfChars name(env, java_name); 37 // if (name.c_str() == nullptr) { 38 // return nullptr; 39 // } 40 // 41 // Also consider using `GET_UTF_OR_RETURN`, a shorthand for the 4 lines above. 42 class ScopedUtfChars { 43 public: ScopedUtfChars(JNIEnv * env,jstring s)44 ScopedUtfChars(JNIEnv* env, jstring s) noexcept : env_(env), string_(s) { 45 if (s == nullptr) { 46 utf_chars_ = nullptr; 47 jniThrowNullPointerException(env); 48 } else { 49 utf_chars_ = env->GetStringUTFChars(s, nullptr); 50 } 51 } 52 ScopedUtfChars(ScopedUtfChars && rhs)53 ScopedUtfChars(ScopedUtfChars&& rhs) noexcept : 54 env_(rhs.env_), string_(rhs.string_), utf_chars_(rhs.utf_chars_) { 55 rhs.env_ = nullptr; 56 rhs.string_ = nullptr; 57 rhs.utf_chars_ = nullptr; 58 } 59 ~ScopedUtfChars()60 ~ScopedUtfChars() noexcept { 61 release_string(); 62 } 63 64 ScopedUtfChars& operator=(ScopedUtfChars&& rhs) noexcept { 65 if (this != &rhs) { 66 // Delete the currently owned UTF chars. 67 release_string(); 68 69 // Move the rhs ScopedUtfChars and zero it out. 70 env_ = rhs.env_; 71 string_ = rhs.string_; 72 utf_chars_ = rhs.utf_chars_; 73 rhs.env_ = nullptr; 74 rhs.string_ = nullptr; 75 rhs.utf_chars_ = nullptr; 76 } 77 return *this; 78 } 79 c_str()80 const char* c_str() const noexcept { 81 return utf_chars_; 82 } 83 size()84 size_t size() const noexcept { 85 return strlen(utf_chars_); 86 } 87 88 const char& operator[](size_t n) const noexcept { 89 return utf_chars_[n]; 90 } 91 92 #if __has_include(<string_view>) string_view()93 operator std::string_view() const noexcept { return utf_chars_; } 94 #endif 95 96 private: release_string()97 void release_string() noexcept { 98 if (utf_chars_) { 99 env_->ReleaseStringUTFChars(string_, utf_chars_); 100 } 101 } 102 103 JNIEnv* env_; 104 jstring string_; 105 const char* utf_chars_; 106 107 DISALLOW_COPY_AND_ASSIGN(ScopedUtfChars); 108 }; 109