This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Scheme vector to Java byte[]?


Hoehle, Joerg-Cyril wrote:
> while I have little problems exchanging strings and ints with Java
> code, I find less support than expected for byte[] arrays.

True, Kawa does not provide really convenient access to Java arrays.
Instead, it does provide full support for SRFI-4.  It uses wrapper
classes rather than directly using Java arrays, which would be
difficult, partly because Java does not have unsigned numeric types
(except char).

I found nothing similar for the u8vector type, which presumably is close to <byte[]>.

The Java 'byte' type is signed, so the Scheme version of byte[] is s8vector.


or a built-in transparent calling mechanism.
E.g. how to (invoke-static <org.apache.xerces.impl.dv.util.Base64>
> 'encode a-byte[]-thing-out-of-Kawa-Scheme)?

The issue isn't "transparent calling mechanism" but "transparent array
mechanism".  That might be nice, but there are some complications.

1. I believe the (make <u8vector> data-byte[]-object)
should find its way into the documentation,

I wrote up something for the manual. See below.


> 2. Similarly, I believe (invoke "abc" 'toCharArray) should find
> its way into the documentation (interfacing Java to Scheme).

That might below in a FAQ or Wiki, but I'm not sure it belongs
in a reference manual.  It it does, where would you put it, and
what would you write?

3. I believe there should be an easy way to get the bytes[] out of
> a u8vector so that I can pass that to Java code, not self-written
code on top of primitive-array-new and a consecutive iteration.

Perhaps, but should it copy or not? What if the u8vector's length is different from the underlying array's length?

Or is my assumtion wrong and u8vector is a complex gap-buffer
> without a byte[] underneath?

Partly.  It's a single byte[] but there is also a length field,
to support re-sizing arrays, as well as potentially support Common
Lisp's fill pointer.

New text for the manual:

Relationship with Java arrays
-----------------------------

   Each uniform array type is implemented as an "underlying Java array",
and a length field.  The underlying type is `byte[]' for `<u8vector>'
or `<s8vector>'; `short[]' for `<u16vector>' or `<u16vector>'; `int[]'
for `<u32vector>' or `<s32vector>'; `long[]' for `<u64vector>' or
`<s64vector>'; `<float[]' for `<f32vector>'; and `<double[]' for
`<f32vector>'.  The length field allows a uniform array to only use the
initial part of the underlying array.  (This can be used to support
Common Lisp's fill pointer feature.)  This also allows resizing a
uniform vector.  There is no Scheme function for this, but you can use
the `setSize' method:
     (invoke some-vector 'setSize 200)

   If you have a Java array, you can create a uniform vector sharing
with the Java array:
     (define arr :: <byte[]> ((primitive-array-new <byte>) 10))
     (define vec :: <u8vector> (make <u8vector> arr))
   At this point `vec' uses `arr' for its underlying storage, so
changes to one affect the other.  It `vec' is re-sized so it needs a
larger underlying array, then it will no longer use `arr'.
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]