JNI之String类型
JNI使用的是改良的UTF-8格式的Strings。
以下文档来自官方:
Modified UTF-8 Strings
The JNI uses modified UTF-8 strings to represent various string types. Modified UTF-8 strings are the same as those used by the Java VM. Modified UTF-8 strings are encoded so that character sequences that contain only non-null ASCII characters can be represented using only one byte per character, but all Unicode characters can be represented.
翻译:
JNI使用的是改良的UTF-8格式的Strings来表示各种字符串类型。改良的UTF-8的strings和Java VM使用的是一样的。改良的UTF-8的strings编码,使得仅包含非空ASCII字符的字符序列能够按每字符占一个字节来表示,但所有的Unicode字符均可以被表示出来。
String Operations(String 操作)
1.NewString -- 创建String
jstring NewString(JNIEnv *env, const jchar *unicodeChars,jsize len);
Constructs a new java.lang.String object from an array of Unicode characters.
通过Unicode字符的数组来创建一个新的String对象。备注:Unicode和UTF-8还是有区别的。
参数:
env:JNI 接口指针。
unicodeChars:指向 Unicode 字符串的指针。
len:Unicode 字符串的长度。
返回值:
Java 字符串对象。如果无法构造该字符串,则为 NULL。
抛出:
OutOfMemoryError:如果系统内存不足。
2.GetStringLength -- 获取String的长度
jsize GetStringLength(JNIEnv *env, jstring string);
Returns the length (the count of Unicode characters) of a Java string.
返回 Java 字符串的长度(Unicode 字符数)。
参数:
env:JNI 接口指针。
string:Java 字符串对象。
返回值:
Java 字符串的长度。
3.GetStringChars -- 获取StringChars的指针
const jchar * GetStringChars(JNIEnv *env, jstring string,jboolean *isCopy);
Returns a pointer to the array of Unicode characters of the string. This pointer is valid until ReleaseStringchars() is called.
If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made; or it is set to JNI_FALSE if no copy is made.
返回指向字符串的 Unicode 字符数组的指针。该指针在调用 ReleaseStringchars() 前一直有效。
如果 isCopy 非空,则在复制完成后将 *isCopy 设为 JNI_TRUE。如果没有复制,则设为JNI_FALSE。
参数:
env:JNI 接口指针。
string:Java 字符串对象。
isCopy:指向布尔值的指针。
返回值:
指向 Unicode 字符串的指针,如果操作失败,则返回NULL。
4.ReleaseStringChars -- 释放StringChars
void ReleaseStringChars(JNIEnv *env, jstring string,const jchar *chars);
Informs the VM that the native code no longer needs access to chars. The chars argument is a pointer obtained from string using GetStringChars().
通知虚拟机平台相关代码无需再访问 chars。参数 chars 是一个指针,可通过 GetStringChars() 从 string 获得。
参数:
env:JNI 接口指针。
string:Java 字符串对象。
chars:指向 Unicode 字符串的指针。
UTF-8
5.NewStringUTF -- 创建UTF的String
jstring NewStringUTF(JNIEnv *env, const char *bytes);
Constructs a new java.lang.String object from an array of characters in modified UTF-8 encoding.
通过改良的 UTF-8 字符数组构造新 java.lang.String 对象。
参数:
env:JNI 接口指针。如果无法构造该字符串,则为 NULL。
bytes:指向 UTF-8 字符串的指针。
返回值:
Java 字符串对象。如果无法构造该字符串,则为 NULL。
抛出:
OutOfMemoryError:如果系统内存不足。
6.GetStringUTFLength -- 获取UTF的String的长度
jsize GetStringUTFLength(JNIEnv *env, jstring string);
Returns the length in bytes of the modified UTF-8 representation of a string.
以字节为单位返回字符串的 UTF-8 长度。
参数:
env:JNI 接口指针。
string:Java 字符串对象。
返回值:
返回字符串的 UTF-8 长度。
7.GetStringUTFChars -- 获取StringUTFChars的指针
const char * GetStringUTFChars(JNIEnv *env, jstring string,jboolean *isCopy);
Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding. This array is valid until it is released by ReleaseStringUTFChars().
If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made; or it is set to JNI_FALSE if no copy is made.
返回指向字符串的 UTF-8 字符数组的指针。该数组在被ReleaseStringUTFChars() 释放前将一直有效。
如果 isCopy 不是 NULL,*isCopy 在复制完成后即被设为 JNI_TRUE。如果未复制,则设为 JNI_FALSE。
参数:
env:JNI 接口指针。
string:Java 字符串对象。
isCopy:指向布尔值的指针。
返回值:
指向 UTF-8 字符串的指针。如果操作失败,则为 NULL。
8.ReleaseStringUTFChars -- 释放StringUTFChars
void ReleaseStringUTFChars(JNIEnv *env, jstring string,const char *utf);
Informs the VM that the native code no longer needs access to utf. The utf argument is a pointer derived from string using GetStringUTFChars().
通知虚拟机平台相关代码无需再访问 utf。utf 参数是一个指针,可利用 GetStringUTFChars() 从 string 获得。
参数:
env:JNI 接口指针。
string:Java 字符串对象。
utf:指向 UTF-8 字符串的指针。
Note
In JDK/JRE 1.1, programmers can get primitive array elements in a user-supplied buffer. As of JDK/JRE 1.2 additional set of functions are provided allowing native code to obtain characters in Unicode (UTF-16) or modified UTF-8 encoding in a user-supplied buffer. See the functions below.
注意:
在JDK/JRE 1.1,程序员可以在用户提供的缓冲区获取基本类型数组元素。从JDK/JRE1.2之后,提供了额外的方法,这些方法允许在用户提供的缓冲区获取Unicode字符(UTF-16编码)或者是UTF-8的字符。这些方法详见如下:
9.GetStringUTFRegion
void GetStringRegion(JNIEnv *env, jstring str, jsize start, jsize len, jchar *buf);
Copies len number of Unicode characters beginning at offset start to the given buffer buf.
Throws StringIndexOutOfBoundsException on index overflow.
在str(Unicode字符)从start位置开始截取len长度放置到buf中。
抛出StringIndexOutOfBoundsException异常。
10.GetStringUTFRegion
void GetStringUTFRegion(JNIEnv *env, jstring str, jsize start, jsize len, char *buf);
Translates len number of Unicode characters beginning at offset start into modified UTF-8 encoding and place the result in the given buffer buf.
Throws StringIndexOutOfBoundsException on index overflow.
将str(Unicode字符)从start位置开始截取len长度转换为改良的UTF-8编码并将结果放置到buf中。
抛出StringIndexOutOfBoundsException异常。
11.GetStringCritical / ReleaseStringCritical
const jchar * GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy);
void ReleaseStringCritical(JNIEnv *env, jstring string, const jchar *carray);
The semantics of these two functions are similar to the existing Get/ReleaseStringChars functions. If possible, the VM returns a pointer to string elements; otherwise, a copy is made. However, there are significant restrictions on how these functions can be used. In a code segment enclosed by Get/ReleaseStringCritical calls, the native code must not issue arbitrary JNI calls, or cause the current thread to block.
The restrictions on Get/ReleaseStringCritical are similar to those on Get/ReleasePrimitiveArrayCritical.
这两个函数的语义是类似于现有的 Get/ReleaseStringChars 功能。如果可能的话,虚拟机返回一个指向字符串元素的指针;否则,将返回一个复制的副本。然而使用这些方法是有值得注意的限制的。In a code segment enclosed by Get/ReleaseStringCritical calls, the native code must not issue arbitrary JNI calls, or cause the current thread to block.
备注:
为了提高JVM返回字符串直接指针的可能性,JDK1.2中引入了一对新函数,Get/ReleaseStringCritical。表面上,它们和Get/ReleaseStringChars函数差不多,但实际上这两个函数在使用有很大的限制。
使用这两个函数时,你必须两个函数中间的代码是运行在"critical region"(临界区)的,即,这两个函数中间的本地代码不能调用任何会让线程阻塞或等待JVM中的其它线程的本地函数或JNI函数。
有了这些限制, JVM就可以在本地方法持有一个从GetStringCritical得到的字符串的直接指针时禁止GC。当GC被禁止时,任何线程如果触发GC的话,都会被阻塞。而Get/ReleaseStringCritical这两个函数中间的任何本地代码都不可以执行会导致阻塞的调用或者为新对象在JVM中分配内存。否则,JVM有可能死锁,想象一下这样的场景中:
1、 只有当前线程触发的GC完成阻塞并释放GC时,由其它线程触发的GC才可能由阻塞中释放出来继续运行。
2、 在这个过程中,当前线程会一直阻塞。因为任何阻塞性调用都需要获取一个正被其它线程持有的锁,而其它线程正等待GC。
Get/ReleaseStringCritical的交迭调用是安全的,这种情况下,它们的使用必须有严格的顺序限制。而且,我们一定要记住检查是否因为内存溢出而导致它的返回值是NULL。因为JVM在执行GetStringCritical这个函数时,仍有发生数据复制的可能性,尤其是当JVM内部存储的数组不连续时,为了返回一个指向连续内存空间的指针,JVM必须复制所有数据。
总之,为了避免死锁,在Get/ReleaseStringCritical之间不要调用任何JNI函数。Get/ReleaseStringCritical和 Get/ReleasePrimitiveArrayCritical这两个函数是可以的。
参考资料:
http://blog.csdn.net/mu0206mu/article/details/7182010
http://blog.csdn.net/a345017062/article/details/8068917
JNI之String类型的更多相关文章
- JNI中java类型的简写
在JNI中,当我们使用GetFieldID/GetStaticFieldID或GetMethodID/GetStaticMethodID及定义JNINativeMethod等时,我们需要表示成员变 ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- 每日一记-mybatis碰到的疑惑:String类型可以传入多个参数吗
碰到一个觉得很疑惑的问题,Mybatis的parameterType为String类型的时候,能够接收多个参数的吗? 背景 初学Mybatis的时候,看的教程和书籍上都是在说基本的数据类型如:int. ...
- C#string类型总结
字符串的特性:不可变性,每对字符串做拼接或者重新赋值之类的操作,都会在内存中产生一个新的实例. 所以说,在.Net平台下,如果你对一个字符串进行大量的拼接赋值等操作,会产生大量的垃圾. --- ...
- 把《c++ primer》读薄(3-1 标准库string类型初探)
督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...
- 【原创】Java和C#下String类型中的==和equals的原理与区别
一.Java下 1.几个例子 public static void main(String[] arge) { String str1 = new String("1234"); ...
- String类型的属性和方法
× 目录 [1]属性 [2]对象通用方法 [3]访问字符方法[4]字符串拼接[5]创建子串方法[6]大小写转换[7]查找子串位置[8]正则匹配方法[9]去除首尾空格[10]字符串比较 前面的话 前面已 ...
- String类型传值以及对象传值
package Virtual; class Stan{ String mm = "hello"; } class Virtual { public static void mai ...
- java动手动脑和课后实验型问题String类型
1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? true true false 总结: 使用new关键字创建字符串对象时, 每次申请 ...
随机推荐
- css 基础1
css 层叠样式表 css手册 样式写在head 中间 style标签 css 样式规则: 选择器 {属性:属性值:属性:属性值} 字体样式属性:font-size 字号大小 color 字体颜色 f ...
- [整理]win7下VS2010遇到内存不足解决方发
电脑重装Win7 64bit不久后,一天内VS2010使用久了,就会出现内存不足,实际内存使用情况却不是,显示内存已使用70%.以前没有遇到过,经网上查找,貌似是VS2010对内存计算会在某些情况下计 ...
- css框架,一把锋利的剑
CSS 框架是一系列 CSS 文件的集合体,包含了基本的元素重置,页面排版.网格布局.表单样式.通用规则等代码块,用于简化web前端开发的工作,提高工作效率. 产生原因 互联网行业已经发展了多年,浏览 ...
- Sortable.js参数
所有的事件回调函数都有两个参数:event和ui,浏览器自有event对象,和经过封装的ui对象ui.helper - 表示sortable元素的JQuery对象,通常是当前元素的克隆对象ui.pos ...
- [转]mitmproxy套件使用攻略及定制化开发
mitmproxy是一款支持HTTP(S)的中间人代理工具.不同于Fiddler2,burpsuite等类似功能工具,mitmproxy可在终端下运行.mitmproxy使用Python开发,是辅助w ...
- Python实现 -- 冒泡排序、选择排序、插入排序
冒泡排序 冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 冒泡排序的原理: 比较两个相邻的元素,如果第一个比第二个大,就交换他们 对每一对相邻的元素做同样的工作,从开始第 ...
- css部分复习整理
CSS代码语法 css 样式由选择符和声明组成,而声明又由属性和值组成,如下图所示: 选择符:又称选择器,指明网页中要应用样式规则的元素,如本例中是网页中所有的段(p)的文字将变成蓝色,而其他的元素( ...
- 【转】手把手教你 Mockito 的使用
原文链接:https://segmentfault.com/a/1190000006746409 什么是 Mockito Mockito 是一个强大的用于 Java 开发的模拟测试框架, 通过 Moc ...
- 在阿里云申请Symantec免费SSL证书操作流程
2016年阿里云与国内证书颁发机构天威诚信推出了基于Symantec(赛门铁克)的免费SSL证书,有需要免费SSL证书产品的可以前往阿里云进行申请. 申请地址:阿里云云盾证书服务—Symantec免费 ...
- PHP时间戳和日期转换
获取当前时间 <?php var_dump(time()); //获取当前时间戳 int(1502245603) 时间戳转换为时间,可以用date(‘Y-m-s h:i:s’, 具体时间戳来实现 ...