use of org.graalvm.jniutils.JNI.JByteArray in project graal by oracle.
the class TruffleToLibGraalEntryPoints method getVersionProperties.
@TruffleToLibGraal(GetVersionProperties)
@CEntryPoint(name = "Java_org_graalvm_compiler_truffle_runtime_hotspot_libgraal_TruffleToLibGraalCalls_getVersionProperties")
@SuppressWarnings({ "unused", "try" })
public static JByteArray getVersionProperties(JNIEnv env, JClass hsClazz, @CEntryPoint.IsolateThreadContext long isolateThreadId) {
JNIMethodScope scope = LibGraalUtil.openScope(TruffleToLibGraalEntryPoints.class, GetVersionProperties, env);
try (JNIMethodScope s = scope) {
Map<String, Object> versionProperties = new HashMap<>();
for (Map.Entry<Object, Object> e : DebugContext.addVersionProperties(null).entrySet()) {
Object key = e.getKey();
Object value = e.getValue();
assert key instanceof String;
assert value instanceof String;
versionProperties.put((String) key, value);
}
byte[] serializedProperties = OptionsEncoder.encode(versionProperties);
JByteArray hsSerializedOptions = NewByteArray(env, serializedProperties.length);
CCharPointer cdata = GetByteArrayElements(env, hsSerializedOptions, WordFactory.nullPointer());
CTypeConversion.asByteBuffer(cdata, serializedProperties.length).put(serializedProperties);
ReleaseByteArrayElements(env, hsSerializedOptions, cdata, JArray.MODE_WRITE_RELEASE);
scope.setObjectResult(hsSerializedOptions);
} catch (Throwable t) {
JNIExceptionWrapper.throwInHotSpot(env, t);
scope.setObjectResult(WordFactory.nullPointer());
}
return scope.getObjectResult();
}
use of org.graalvm.jniutils.JNI.JByteArray in project graal by oracle.
the class JNINativeMarshallerAdapter method unmarshall.
@Override
public T unmarshall(JNIEnv env, JObject jObject) {
if (jObject.isNull()) {
return null;
}
int bufSize = BUFFER_SIZE;
CCharPointer staticBuffer = StackValue.get(bufSize);
int len = JNIUtil.GetArrayLength(env, (JByteArray) jObject);
CCharPointer useBuffer;
if (len < bufSize) {
useBuffer = staticBuffer;
} else {
useBuffer = UnmanagedMemory.malloc(len);
}
try {
JNIUtil.GetByteArrayRegion(env, ((JByteArray) jObject), 0, len, useBuffer);
try (BinaryInput in = BinaryInput.create(useBuffer, len)) {
return binaryMarshaller.read(in);
} catch (IOException e) {
throw new AssertionError(e.getMessage(), e);
}
} finally {
if (useBuffer != staticBuffer) {
UnmanagedMemory.free(useBuffer);
}
}
}
use of org.graalvm.jniutils.JNI.JByteArray in project graal by oracle.
the class JNIUtil method createHSArray.
public static JByteArray createHSArray(JNIEnv jniEnv, byte[] a) {
if (a == null) {
return WordFactory.nullPointer();
}
JByteArray array = NewByteArray(jniEnv, a.length);
arrayCopy(jniEnv, a, 0, array, 0, a.length);
return array;
}
use of org.graalvm.jniutils.JNI.JByteArray in project graal by oracle.
the class JNINativeMarshallerAdapter method marshall.
@Override
public JObject marshall(JNIEnv env, T object) {
if (object == null) {
return WordFactory.nullPointer();
}
int bufSize = BUFFER_SIZE;
CCharPointer buffer = StackValue.get(bufSize);
try (CCharPointerBinaryOutput out = BinaryOutput.create(buffer, bufSize, false)) {
binaryMarshaller.write(out, object);
int len = out.getPosition();
JByteArray binaryData = JNIUtil.NewByteArray(env, len);
JNIUtil.SetByteArrayRegion(env, binaryData, 0, len, out.getAddress());
return binaryData;
} catch (IOException e) {
throw new AssertionError(e.getMessage(), e);
}
}
Aggregations