Skip to content
Snippets Groups Projects
Commit a63bd67f authored by Adrien Béraud's avatar Adrien Béraud
Browse files

bin/jni: use direct data copy instead of byte-per-byte

Change-Id: Ia42e80d20b2ede043dcf8c085b814b84361dca53
parent 852c9ebe
No related branches found
No related tags found
No related merge requests found
%header %{
namespace jami {
struct DataView {
const uint8_t* data;
size_t size;
};
struct Data {
std::vector<uint8_t> data;
};
}
%}
namespace jami {
struct DataView;
struct Data;
%typemap(jni) DataView "jbyteArray"
%typemap(jtype) DataView "byte[]"
%typemap(jstype) DataView "byte[]"
%typemap(jni) Data "jbyteArray"
%typemap(jtype) Data "byte[]"
%typemap(jstype) Data "byte[]"
%typemap(javadirectorin) Data "$jniinput"
%typemap(javadirectorout) DataView "$javacall"
%typemap(in) Data
%{ if(!$input) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
return $null;
}
jsize len = jenv->GetArrayLength($input);
$1.data.resize(len);
jenv->GetByteArrayRegion($input, 0, len, (jbyte*)$1.data.data()); %}
%typemap(out) DataView {
jbyteArray r = jenv->NewByteArray($1.size);
jenv->SetByteArrayRegion(r, 0, $1.size, (const jbyte *)$1.data);
$result = r;
}
%typemap(javain) Data "$javainput"
%typemap(javaout) DataView {
return $jnicall;
}
}
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
%include "std_map.i"; %include "std_map.i";
%include "std_vector.i"; %include "std_vector.i";
%include "stdint.i"; %include "stdint.i";
%include "data_view.i";
%header %{ %header %{
#include <android/log.h> #include <android/log.h>
...@@ -117,9 +118,11 @@ namespace std { ...@@ -117,9 +118,11 @@ namespace std {
public java.util.HashMap<String,String> toNativeFromUtf8() { public java.util.HashMap<String,String> toNativeFromUtf8() {
java.util.HashMap<String,String> out = new java.util.HashMap<>((int)size()); java.util.HashMap<String,String> out = new java.util.HashMap<>((int)size());
StringVect keys = keys(); for (String s : keys()) {
for (String s : keys) { try {
out.put(s, getRaw(s).toJavaString()); out.put(s, new String(getRaw(s), "utf-8"));
} catch (java.io.UnsupportedEncodingException e) {
}
} }
return out; return out;
} }
...@@ -137,9 +140,9 @@ namespace std { ...@@ -137,9 +140,9 @@ namespace std {
void setRaw(const std::string& key, const vector<uint8_t>& value) { void setRaw(const std::string& key, const vector<uint8_t>& value) {
(*$self)[key] = std::string(value.data(), value.data()+value.size()); (*$self)[key] = std::string(value.data(), value.data()+value.size());
} }
std::vector<uint8_t> getRaw(const std::string& key) { jami::DataView getRaw(const std::string& key) {
auto& v = $self->at(key); auto& v = $self->at(key);
return {v.begin(), v.end()}; return {(const uint8_t*)v.data(), v.size()};
} }
} }
...@@ -169,24 +172,19 @@ namespace std { ...@@ -169,24 +172,19 @@ namespace std {
dat = in.getBytes(); dat = in.getBytes();
} }
Blob n = new Blob(); Blob n = new Blob();
n.reserve(dat.length); n.setBytes(dat);
for (int i=0; i<dat.length; i++) {
n.add(dat[i]);
}
return n; return n;
} }
public String toJavaString() { %}
byte[] dat = new byte[(int)size()];
for (int i=0; i<dat.length; i++) { %extend vector<uint8_t> {
dat[i] = (byte)get(i); jami::DataView getBytes() {
return {self->data(), self->size()};
} }
try { void setBytes(jami::Data data) {
return new String(dat, "utf-8"); *self = std::move(data.data);
} catch (java.io.UnsupportedEncodingException e) {
return "";
} }
} }
%}
%template(Blob) vector<uint8_t>; %template(Blob) vector<uint8_t>;
%template(FloatVect) vector<float>; %template(FloatVect) vector<float>;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment