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]

Scheme vector to Java byte[]?


Hi,

while I have little problems exchanging strings and ints with Java code, I find less support than expected for byte[] arrays.

Per wrote on char[] in a response to me in May 2002:
>> I figured out that it would suffice to create a Java char[] 
>You can use the toCharArray method to get a char[]:
>(invoke "some Scheme string" 'toCharArray)

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

>Or you can create a char[] using primitive-array-new (see the manual).
Sheesh!

Scanning the online classes documentation, I found half the solution:

1. byte[] -> Scheme object
Use the U8Vector(byte[] data) constructor. Examples:

; Using base64 from the Xerces implementation:
(define bytes
  (invoke-static <org.apache.xerces.impl.dv.util.Base64> 'decode "abcd"))
bytes -> [B@4c47db
(make <u8vector> bytes) -> #u8(105 183 29)

Furthermore, it appears that the u8vector object shares the byte[] array with the argument, which I find convenient, e.g.

(define rand8 ((primitive-array-new <byte>) 8))
(define randu8 (make <u8vector> rand8))
(define rand (make <java.util.Random>)) ;optional long seed
(invoke rand 'nextBytes rand8)
randu8 ;; Assumes randu8 shares byte[] array with rand8 -- works
randu8 is destructively updated for each invocation of nextBytes
You may like that or not.


2. HOWTO Scheme vector/list -> <byte[]>
?? -- nothing like toByteArray
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)?

Summary:
1. I believe the (make <u8vector> data-byte[]-object) should find its way into the documentation, not remain deeply buried in javadoc class documentation.
2. Similarly, I believe (invoke "abc" 'toCharArray) should find its way into the documentation (interfacing Java to Scheme).
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.
Or is my assumtion wrong and u8vector is a complex gap-buffer without a byte[] underneath?

Thanks for your help,
	Jorg Hohle.


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