1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#![cfg(any(feature = "all", feature = "java-lang-String"))]
use super::java;
use jni_glue::*;
use std::fmt::{self, Debug, Formatter};
use std::char::DecodeUtf16Error;
impl java::lang::String {
pub fn from_env_str<'env, S: AsRef<str>>(env: &'env Env, string: S) -> Local<'env, Self> {
let chars = string.as_ref().encode_utf16().collect::<Vec<_>>();
let string = unsafe { env.new_string(
chars.as_ptr() as *const jchar,
chars.len() as jni_sys::jsize,
) };
unsafe { Local::from_env_object(env.as_jni_env() as *const _, string) }
}
fn string_chars(&self) -> StringChars {
unsafe {
let env = Env::from_ptr(self.0.env);
StringChars::from_env_jstring(env, self.0.object)
}
}
pub fn to_string(&self) -> Result<String, DecodeUtf16Error> { self.string_chars().to_string() }
pub fn to_string_lossy(&self) -> String { self.string_chars().to_string_lossy() }
}
impl Debug for java::lang::String {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.to_string_lossy(), f)
}
}