1 /* 2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2.0, 6 as published by the Free Software Foundation. 7 8 This program is also distributed with certain software (including 9 but not limited to OpenSSL) that is licensed under separate terms, 10 as designated in a particular file or component or in included license 11 documentation. The authors of MySQL hereby grant you an additional 12 permission to link the program and your derivative works with the 13 separately licensed software that they have included with MySQL. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License, version 2.0, for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 /* 25 * jtie_tconv_string_impl.hpp 26 */ 27 28 #ifndef jtie_tconv_string_impl_hpp 29 #define jtie_tconv_string_impl_hpp 30 31 #include <assert.h> // not using namespaces yet 32 #include <jni.h> 33 34 #include "jtie_tconv_string.hpp" 35 #include "jtie_tconv_impl.hpp" 36 #include "helpers.hpp" 37 38 // --------------------------------------------------------------------------- 39 // Java String <-> const char * type conversion 40 // --------------------------------------------------------------------------- 41 42 // comments: to support UCS2 and also locale encodings... 43 // - class _jstring can be subclassed (analog to bytebuffer mappings) 44 // - see JNU_NewStringNative (§8.2.1) and JNU_GetStringNativeChars (§8.2.2) 45 // in JNI Programming Guide & Tutorial...) 46 // - beware that GetStringChars() etc does not deliver not null-terminated 47 // character strings; some OS (e.g., Windows) expect two trailing zero byte 48 // values to terminate Unicode strings. 49 50 // Implements the mapping of Java String parameters. 51 // declared as template to support other specializations (e.g. char *) 52 template< typename J, typename C > 53 struct ParamStringT; 54 55 // Implements the mapping of Java String results. 56 // declared as template to support other specializations (e.g. char *) 57 template< typename J, typename C > 58 struct ResultStringT; 59 60 template<> 61 struct ParamStringT< jstring, const char * > { 62 static const char * convertParamStringT63 convert(cstatus & s, jstring j, JNIEnv * env) { 64 TRACE("const char * ParamStringT.convert(cstatus &, jstring, JNIEnv *)"); 65 66 // init return value and status to error 67 s = -1; 68 const char * c = NULL; 69 70 // return a C string from a Java String 71 if (j == NULL) { 72 // ok 73 s = 0; 74 } else { 75 // get a UTF-8 string, to be released by ReleaseStringUTFChars() 76 // ignore whether C string is pinned or a copy of Java string 77 c = env->GetStringUTFChars(j, NULL); 78 if (c == NULL) { 79 // exception pending 80 } else { 81 // ok 82 s = 0; 83 } 84 } 85 return c; 86 } 87 88 static void releaseParamStringT89 release(const char * c, jstring j, JNIEnv * env) { 90 TRACE("void ParamStringT.release(const char *, jstring, JNIEnv *)"); 91 if (c == NULL) { 92 assert(j == NULL); 93 } else { 94 assert(j); 95 // release the UTF-8 string allocated by GetStringUTFChars() 96 env->ReleaseStringUTFChars(j, c); 97 } 98 } 99 }; 100 101 template<> 102 struct ResultStringT< jstring, const char * > { 103 static jstring convertResultStringT104 convert(const char * c, JNIEnv * env) { 105 TRACE("jstring ResultStringT.convert(const char *, JNIEnv *)"); 106 if (c == NULL) 107 return NULL; 108 109 // construct a String object from a UTF-8 C string 110 return env->NewStringUTF(c); 111 } 112 }; 113 114 // --------------------------------------------------------------------------- 115 // Specializations for Java String <-> [const] char * type conversion 116 // --------------------------------------------------------------------------- 117 118 // extend String specializations to const pointers 119 template< typename C > 120 struct Param< jstring, C * const > 121 : Param< jstring, C * > {}; 122 template< typename C > 123 struct Result< jstring, C * const > 124 : Result< jstring, C * > {}; 125 126 // specialize Java Strings mapped to 'const char *' 127 template<> 128 struct Param< jstring, const char * > 129 : ParamStringT< jstring, const char * > {}; 130 template<> 131 struct Result< jstring, const char * > 132 : ResultStringT< jstring, const char * > {}; 133 134 // specialize Java Strings mapped to 'char *' (only result mapping!) 135 // no parameter mapping desirable 136 // template<> 137 // struct Param< jstring, char * > 138 // : ParamStringT< jstring, const char * > {}; 139 // result mapping of compatible with 'const char*' 140 template<> 141 struct Result< jstring, char * > 142 : ResultStringT< jstring, const char * > {}; 143 144 // --------------------------------------------------------------------------- 145 146 #endif // jtie_tconv_string_impl_hpp 147