如何使用从C ++返回Java的java.nio.ByteBuffer [英] How to use a java.nio.ByteBuffer return from C++ to Java

查看:59
本文介绍了如何使用从C ++返回Java的java.nio.ByteBuffer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几乎是

This is almost a repeat of question in How can I make Swig correctly wrap a char* buffer that is modified in C as a Java Something-or-other? ...
Instead of Stringbuffer if I were to use a bytebuffer, what would be the change in the typemap ?

推荐答案

我整理了一个示例,说明如何使用以下头文件/函数作为测试方法:

I've put together an example of how you might do this using the following header file/function as a test:

#include <stdio.h>

static void foo(char *buf, int len) {
  while(len--)
    putchar(*buf++);
}

我的解决方案是修改

My solution was to modify this answer such that the proxy takes a ByteBuffer and converts it into a byte[] for us to pass to the JNI code, which then converts it into a pointer + length combination for us.

%module test

%{
#include "test.h"
%}

%typemap(jtype) (char *buf, int len) "byte[]"
%typemap(jstype) (char *buf, int len) "java.nio.ByteBuffer"
%typemap(jni) (char *buf, int len) "jbyteArray"
%typemap(javain,pre="    byte[] temp$javainput = new byte[$javainput.capacity()];"
                    "    $javainput.get(temp$javainput);")
        (char *buf, int len) "temp$javainput"

%typemap(in,numinputs=1) (char *buf, int len) {
  $1 = JCALL2(GetByteArrayElements, jenv, $input, NULL);
  $2 = JCALL1(GetArrayLength, jenv, $input);
}

%typemap(freearg) (const signed char *arr, size_t sz) {
  // Or use  0 instead of ABORT to keep changes if it was a copy
  JCALL3(ReleaseByteArrayElements, jenv, $input, $1, JNI_ABORT); 
}

%include "test.h"

这里的新位在javain类型图中,分配了一个临时 byte [] ,然后使用 get 进行填充.实际上有一个 array()函数,如果您使用的 ByteBuffer 支持,则应该改用,即类型映射应为:

The new bit here is in the javain typemap, allocating a temporary byte[] and then using get to fill it. There is actually an array() function that if the ByteBuffer you're using supports you should use instead, i.e. the typemap should be just:

%typemap(javain) (char *buf, int len) "$javainput.array()"

如果实现支持(该方法是可选的,并且可能抛出 UnsuportedOperationException ).

if your implementation supports it (the method is optional and may throw UnsuportedOperationException).

实际上,这可以使用SWIG 2.0进一步简化,从前面提到的问题开始,因为我们希望始终的类型为 byte ,所以我们可以使用内置的intSWIG 2.0中的typemap来简化我们的界面,现在变成:

In actual fact this can be simplified further with SWIG 2.0, from the previous referenced question since we are expecting the type to always be byte we can use a built int typemap from SWIG 2.0 to simplify our interface which now becomes:

%module test

%{
#include "test.h"
%}

%apply (char *STRING, size_t LENGTH) { (char *buf, int len) }
%typemap(javain) (char *buf, int len) "$javainput.array()"
%typemap(jstype) (char *buf, int len) "java.nio.ByteBuffer"

%include "test.h"

我使用以下Java测试了这三个版本:

I tested all three versions of this with the following Java:

public class run {
  public static void main(String[] argv) {
    System.loadLibrary("test");
    byte value[] = "hello world\n".getBytes();
    java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(value);
    test.foo(buf);
  }
}

为了避免可能不受支持的 array()的安全,您可能想做的是在带有编译指示的函数中添加try/catch:

To be safe with the possibly unsupported array() what you probably want to do is add a try/catch in a function with a pragma:

%pragma(java) modulecode = %{
  private static byte[] buf2bytearr(java.nio.ByteBuffer buf) {
    try {
      return buf.array();
    }
    catch (UnsupportedOperationException e) {
      byte arr[] = new byte[buf.capacity()];
      buf.get(arr);
      return arr;
    }
  }
%}

,然后修改类型图以使用它:

and then modify the typemap to use that:

%typemap(javain) (char *buf, int len) "buf2bytearr($javainput)"

这篇关于如何使用从C ++返回Java的java.nio.ByteBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆