summaryrefslogtreecommitdiff
path: root/src/java/String.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2019-04-24 13:55:15 +0200
committerMax Kellermann <max@musicpd.org>2019-04-24 14:07:52 +0200
commit61b2ae0f7cdab867af7a41e279ac14c58092b338 (patch)
tree33f0f8de952b373edc6ee85fd467e123df982d25 /src/java/String.cxx
parent0d2ec5ead22fdbadb6c602b2cb4c2ce71096aefb (diff)
java/String: add method ToString()
Diffstat (limited to 'src/java/String.cxx')
-rw-r--r--src/java/String.cxx20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/java/String.cxx b/src/java/String.cxx
index 899f670c4..d072b818f 100644
--- a/src/java/String.cxx
+++ b/src/java/String.cxx
@@ -1,5 +1,5 @@
/*
- * Copyright 2010-2018 Max Kellermann <max.kellermann@gmail.com>
+ * Copyright 2010-2019 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -29,6 +29,7 @@
#include "String.hxx"
#include "util/TruncateString.hxx"
+#include "util/ScopeExit.hxx"
char *
Java::String::CopyTo(JNIEnv *env, jstring value,
@@ -42,3 +43,20 @@ Java::String::CopyTo(JNIEnv *env, jstring value,
env->ReleaseStringUTFChars(value, p);
return result;
}
+
+std::string
+Java::String::ToString(JNIEnv *env, jstring s) noexcept
+{
+ assert(env != nullptr);
+ assert(s != nullptr);
+
+ const char *p = env->GetStringUTFChars(s, nullptr);
+ if (p == nullptr)
+ return std::string();
+
+ AtScopeExit(env, s, p) {
+ env->ReleaseStringUTFChars(s, p);
+ };
+
+ return std::string(p);
+}