Commit 61b2ae0f authored by Max Kellermann's avatar Max Kellermann

java/String: add method ToString()

parent 0d2ec5ea
/*
* 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);
}
/*
* 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
......@@ -34,6 +34,8 @@
#include <jni.h>
#include <string>
#include <stddef.h>
namespace Java {
......@@ -69,6 +71,12 @@ namespace Java {
char *buffer, size_t max_size) noexcept {
return CopyTo(env, Get(), buffer, max_size);
}
static std::string ToString(JNIEnv *env, jstring s) noexcept;
std::string ToString() const noexcept {
return ToString(GetEnv(), Get());
}
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment