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) : 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() {
61     if (utf_chars_) {
62       env_->ReleaseStringUTFChars(string_, utf_chars_);
63     }
64   }
65 
66   ScopedUtfChars& operator=(ScopedUtfChars&& rhs) noexcept {
67     if (this != &rhs) {
68       // Delete the currently owned UTF chars.
69       this->~ScopedUtfChars();
70 
71       // Move the rhs ScopedUtfChars and zero it out.
72       env_ = rhs.env_;
73       string_ = rhs.string_;
74       utf_chars_ = rhs.utf_chars_;
75       rhs.env_ = nullptr;
76       rhs.string_ = nullptr;
77       rhs.utf_chars_ = nullptr;
78     }
79     return *this;
80   }
81 
c_str()82   const char* c_str() const {
83     return utf_chars_;
84   }
85 
size()86   size_t size() const {
87     return strlen(utf_chars_);
88   }
89 
90   const char& operator[](size_t n) const {
91     return utf_chars_[n];
92   }
93 
94 #if __has_include(<string_view>)
string_view()95   operator std::string_view() const { return utf_chars_; }
96 #endif
97 
98  private:
99   JNIEnv* env_;
100   jstring string_;
101   const char* utf_chars_;
102 
103   DISALLOW_COPY_AND_ASSIGN(ScopedUtfChars);
104 };
105