The most generic answer is to expose the result as an unbouned C array in Java:
%module test
%include "carrays.i"
%array_class(unsigned char, ucArray);
unsigned char *getFoo();
This generates a class, called ucArray
, which has gets and sets for a specific index. It's unbounded, with the same semantics as C arrays, but lightweight and usable. Since it's not bounded you can invoke Undefined Behaviour from Java, and it has no bounds which makes it impossible to use as an Iterator
or Collection
.
If your array is NULL/0 terminated it would be reasonably easy to use %extend
to implement the Java Iterable
interface too, rough, untested example:
%typemap(javainterfaces) ucArray "java.lang.Iterable<Short>"
%typemap(javacode) ucArray %{
public java.util.Iterator<Short> iterator() {
return new java.util.Iterator<Short>() {
private int pos = 0;
public Short next() {
// Mmmm autoboxing!
return getitem(pos++);
}
public void remove() {
throw new UnsupportedOperationException();
}
public boolean hasNext() {
// Check if we're at the end. A NULL/0 element represents the end
return getitem(pos) != 0;
}
};
}
%}
This needs to go somewhere before the %array_class
macro gets called. It says that our ucArray
class will implement Iterable
and then implements that interface using an anonymous inner class in the iterator()
(the only method of the Iterable
interface) method to implement the Iterator
interface.