3 #include "org_argeo_api_uuid_NativeUuidFactory.h"
9 static inline jobject
fromBytes(JNIEnv
*env
, uuid_t out
) {
13 for (int i
= 0; i
< 8; i
++)
14 msb
= (msb
<< 8) | (out
[i
] & 0xff);
15 for (int i
= 8; i
< 16; i
++)
16 lsb
= (lsb
<< 8) | (out
[i
] & 0xff);
18 jclass uuidClass
= (*env
)->FindClass(env
, "java/util/UUID");
19 jmethodID uuidConstructor
= (*env
)->GetMethodID(env
, uuidClass
, "<init>",
22 jobject jUUID
= (*env
)->AllocObject(env
, uuidClass
);
23 (*env
)->CallVoidMethod(env
, jUUID
, uuidConstructor
, msb
, lsb
);
28 static inline void toBytes(JNIEnv
*env
, jobject jUUID
, uuid_t result
) {
30 jclass uuidClass
= (*env
)->FindClass(env
, "java/util/UUID");
31 jmethodID getMostSignificantBits
= (*env
)->GetMethodID(env
, uuidClass
,
32 "getMostSignificantBits", "()J");
33 jmethodID getLeastSignificantBits
= (*env
)->GetMethodID(env
, uuidClass
,
34 "getLeastSignificantBits", "()J");
36 jlong msb
= (*env
)->CallLongMethod(env
, jUUID
, getMostSignificantBits
);
37 jlong lsb
= (*env
)->CallLongMethod(env
, jUUID
, getLeastSignificantBits
);
39 for (int i
= 0; i
< 8; i
++)
40 result
[i
] = (unsigned char) ((msb
>> ((7 - i
) * 8)) & 0xff);
41 for (int i
= 8; i
< 16; i
++)
42 result
[i
] = (unsigned char) ((lsb
>> ((15 - i
) * 8)) & 0xff);
50 * Class: org_argeo_api_uuid_NativeUuidFactory
52 * Signature: ()Ljava/util/UUID;
54 JNIEXPORT jobject JNICALL
Java_org_argeo_api_uuid_NativeUuidFactory_timeUUID(
55 JNIEnv
*env
, jobject
) {
58 uuid_generate_time(out
);
59 return fromBytes(env
, out
);
63 * Class: org_argeo_api_uuid_NativeUuidFactory
65 * Signature: (Ljava/util/UUID;[B)Ljava/util/UUID;
67 JNIEXPORT jobject JNICALL
Java_org_argeo_api_uuid_NativeUuidFactory_nameUUIDv5(
68 JNIEnv
*env
, jobject
, jobject namespaceUuid
, jbyteArray name
) {
72 toBytes(env
, namespaceUuid
, ns
);
73 jsize length
= (*env
)->GetArrayLength(env
, name
);
74 jbyte
*bytes
= (*env
)->GetByteArrayElements(env
, name
, 0);
76 uuid_generate_sha1(out
, ns
, bytes
, length
);
77 return fromBytes(env
, out
);
81 * Class: org_argeo_api_uuid_NativeUuidFactory
83 * Signature: (Ljava/util/UUID;[B)Ljava/util/UUID;
85 JNIEXPORT jobject JNICALL
Java_org_argeo_api_uuid_NativeUuidFactory_nameUUIDv3(
86 JNIEnv
*env
, jobject
, jobject namespaceUuid
, jbyteArray name
) {
90 toBytes(env
, namespaceUuid
, ns
);
91 jsize length
= (*env
)->GetArrayLength(env
, name
);
92 jbyte
*bytes
= (*env
)->GetByteArrayElements(env
, name
, 0);
94 uuid_generate_md5(out
, ns
, bytes
, length
);
95 return fromBytes(env
, out
);
99 * Class: org_argeo_api_uuid_NativeUuidFactory
100 * Method: randomUUIDStrong
101 * Signature: ()Ljava/util/UUID;
103 JNIEXPORT jobject JNICALL
Java_org_argeo_api_uuid_NativeUuidFactory_randomUUIDStrong(
104 JNIEnv
*env
, jobject
) {
107 uuid_generate_random(out
);
108 return fromBytes(env
, out
);