简体   繁体   English

我有2个非常相似的C函数,分别从python和Java调用。 如何将2种库合并为1种可以从两种语言调用的库?

[英]I have 2 very similar C functions being called from python and Java. How can I combine the 2 libraries into 1 that can be called from both languages?

Basically I have 2 implementations of a C function "encrypt" which I call from python using ctypes and java using JNI. 基本上,我有2个C函数“ encrypt”的实现,我使用ctypes从python调用它,而使用JNI从java调用。 I've been told to take the two dynamic libraries that were used for java and python, and combine them into a static library that can be called from both. 我被告知要使用用于Java和python的两个动态库,并将它们组合成一个可以从两者中调用的静态库。 To clarify, here's the C function implementation for java using JNI: 为了澄清,这是使用JNI的Java C函数实现:

#include "jniTest.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_jniTest_passBytes
  (JNIEnv *env, jclass cls, jbyteArray array) {
    unsigned char *buffer = (*env)->GetByteArrayElements(env, array, NULL);
    jsize size = (*env)->GetArrayLength(env,array);

    for(int i=0; i<size; i++){
        buffer[i] += 1;
        printf("%c",buffer[i]);
    }
    (*env)->ReleaseByteArrayElements(env, array, buffer, 0);
}

So the function takes in a byte array and increments each byte by 1 then returns each element of the new byte array. 因此,该函数接收一个字节数组,并将每个字节加1,然后返回新字节数组的每个元素。 Here's the java side: 这是Java方面:

class jniTest{
    public static native void passBytes(byte[] bytes);
    static{
        System.loadLibrary("encrypter");
    {
    public static void main(String[] args){
        Tester tester = new Tester();
        byte[] b = "hello";
        tester.passBytes(b);
    }
}

The python implementation of the C function is the exact same, just not using all that JNI syntax: C函数的python实现完全相同,只是不使用所有JNI语法:

#include <stdio.h>

void encrypt(int size, unsigned char *buffer);

void encrypt(int size, unsigned char *buffer){
    for(int i=0; i < size; i++){
        buffer[i]+=1; 
        printf("%c", buffer[i]);
    }
}

And the python side: 和python方面:

import ctypes

encryptPy = ctypes.CDLL('/home/aradhak/Documents/libencrypt.so')
hello = "hello"
byteArr = bytearray(hello)
rawBytes = (ctyes_c.ubyte*len(byteArr)(*(byteArr))
encryptPy.encrypt(len(byteArr), rawBytes)

As you can see, the C function for python and java do the exact same thing (few minor differences). 如您所见,用于python和java的C函数做的事情完全相同(差异不大)。 They take in a byte array, increment each byte by 1, and print each incremented byte. 它们采用字节数组,将每个字节加1,然后打印每个递增的字节。 I just need to combine these two C libraries and combine them into a single static library which can be accessed from both python AND java. 我只需要组合这两个C库并将它们组合成一个可以从python和java进行访问的静态库。 Is this possible? 这可能吗? Thanks. 谢谢。

You can call the python version from the java one. 您可以从Java版本中调用python版本。 This avoids the duplicate code. 这样可以避免重复的代码。 You'll still have to do the to/from JNI marshalling though: 不过,您仍然必须执行往返JNI编组的操作:

void encrypt(int size, unsigned char *buffer){
    for(int i=0; i < size; i++){
        buffer[i]+=1; 
        printf("%c", buffer[i]);
    }
}

JNIEXPORT void JNICALL Java_jniTest_passBytes
  (JNIEnv *env, jclass cls, jbyteArray array) {
    unsigned char *buffer = (*env)->GetByteArrayElements(env, array, NULL);
    jsize size = (*env)->GetArrayLength(env,array);

    encrypt(size, buffer);

    (*env)->ReleaseByteArrayElements(env, array, buffer, 0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在从普通java类调用时,如何获取活动,运行和返回Location对象 - How can I get activity, running and returning Location object, while being called from normal java class 如何判断一个方法是否被调用? - How can I tell if a method is being called? 如何在 java 中组合两种非常相似的方法,从而产生两个单独的 ListArray? - How can I combine two very similar methods in java which result in two seperate ListArrays? 从Java和Python调用本机库时如何释放内存? - How is memory freed when native libraries are called from Java and Python? 从java上的url解析pdf。 我可以用jsoup吗? - parse pdf from url on java. can i use jsoup? Java。 我可以从此类的方法中删除该类的实例吗? - Java. Can i delete instance of class from method of this class? 如何从Spring HandlerInterceptor中查找Handler上调用的方法? - How can I lookup the method being called on a Handler from a Spring HandlerInterceptor? 如何防止我的findViewById方法被过早调用? - How can I prevent my findViewById method from being called too early? Java - 我可以反思地发现我是否从catch块中的函数调用了partifuclar异常? - Java - can I reflectively find out if I'm being called from a function within a catch block for a partifuclar exception? Java的。 如何提高性能? - Java. How can I improve performance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM