Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ink/jni/internal/jni_proto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/message_lite.h"

namespace ink {
namespace jni {
namespace ink::jni {

absl::Status ParseProtoFromByteArray(JNIEnv* env, jbyteArray serialized_proto,
google::protobuf::MessageLite& dest) {
Expand All @@ -41,7 +40,8 @@ absl::Status ParseProtoFromByteArray(JNIEnv* env, jbyteArray serialized_proto,
jbyte* bytes = env->GetByteArrayElements(serialized_proto, nullptr);
ABSL_CHECK(bytes);
bool success = dest.ParseFromArray(bytes + offset, size);
env->ReleaseByteArrayElements(serialized_proto, bytes, 0);
// No need to copy back the array, which is not modified.
env->ReleaseByteArrayElements(serialized_proto, bytes, JNI_ABORT);
if (!success) {
return absl::InvalidArgumentError(absl::StrCat(
"Failed to parse ", dest.GetTypeName(), " proto from byte[]."));
Expand Down Expand Up @@ -88,10 +88,10 @@ jbyteArray SerializeProto(JNIEnv* env, const google::protobuf::MessageLite& src)
jbyte* bytes = env->GetByteArrayElements(serialized_proto, nullptr);
ABSL_CHECK(bytes);
bool success = src.SerializeToArray(bytes, size);
// Here the array elements must be copied back to the JVM.
env->ReleaseByteArrayElements(serialized_proto, bytes, 0);
ABSL_CHECK(success);
return serialized_proto;
}

} // namespace jni
} // namespace ink
} // namespace ink::jni
6 changes: 2 additions & 4 deletions ink/jni/internal/jni_proto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
#include "absl/status/status.h"
#include "google/protobuf/message_lite.h"

namespace ink {
namespace jni {
namespace ink::jni {

// Attempts to parse a serialized proto from either a direct java.nio.ByteBuffer
// or a jbyteArray, one of which must be non-null. If the proto doesn't parse,
Expand Down Expand Up @@ -62,7 +61,6 @@ absl::Status ParseProtoFromBuffer(JNIEnv* env,
[[nodiscard]] jbyteArray SerializeProto(JNIEnv* env,
const google::protobuf::MessageLite& src);

} // namespace jni
} // namespace ink
} // namespace ink::jni

#endif // INK_JNI_INTERNAL_JNI_PROTO_UTIL_H_
Loading