Hi again ... ABCL
I want to fill a Java byte[] array. But I don't know how to correctly use JARRAY-SET (I should use it, right?):
CL-USER(1): (defvar $*byte (jclass "byte")) $*BYTE CL-USER(2): (setf array (jnew-array $*byte 10)) #<jarray [B@7a2ee7e5 {1D7AAA0E}> CL-USER(3): (jarray-set array 1 0) #<THREAD "interpreter" {6E61A414}>: Debugger invoked on condition of type JAVA-EXCEPTION Java exception 'java.lang.IllegalArgumentException: argument type mismatch'. Restarts: 0: TOP-LEVEL Return to top level. [1] CL-USER(4): 0 CL-USER(5): (jarray-ref array 0) 0
So, a plain number "1" cannot work, but what else I can do? a object of java.lang.Byte?
And, is there any quick way I can fill a byte array using contents from a Lisp array?
Regards,
Chun Tian (binghe)
On Tue, Sep 14, 2010 at 8:45 AM, Chun Tian (binghe) binghe.lisp@gmail.com wrote:
Hi again ... ABCL
I want to fill a Java byte[] array. But I don't know how to correctly use JARRAY-SET (I should use it, right?):
CL-USER(1): (defvar $*byte (jclass "byte")) $*BYTE CL-USER(2): (setf array (jnew-array $*byte 10)) #<jarray [B@7a2ee7e5 {1D7AAA0E}> CL-USER(3): (jarray-set array 1 0) #<THREAD "interpreter" {6E61A414}>: Debugger invoked on condition of type JAVA-EXCEPTION Java exception 'java.lang.IllegalArgumentException: argument type mismatch'. Restarts: 0: TOP-LEVEL Return to top level. [1] CL-USER(4): 0 CL-USER(5): (jarray-ref array 0) 0
So, a plain number "1" cannot work, but what else I can do? a object of java.lang.Byte?
You need to convert 1 to a Java byte... honestly I don't know if and how it can be done. Our fixnums are mapped to integers... we should probably extend jcoerce to handle numeric conversions as well. And conversion of numeric arrays, while we're at it.
And, is there any quick way I can fill a byte array using contents from a Lisp array?
The unexported system::%make-byte-array-output-stream and system::%get-output-stream-bytes come to my mind... but still you'll have to loop over the Lisp array. If there are no objections from other devs, I'd rather extend jcoerce to support your use case.
Cheers, Alessio
PS you can use (setf (jarray-ref array index) value) instead of jarray-set.
Hi, Alessio
Thanks for your information.
I think, if you can make JARRAY-SET or (SETF JARRAY-REF) directly use Lisp (signed-byte 8) numbers, that will be better.
Beside, Java booleans has the same problem, I have to use (make-immediate-object t :boolean) and (make-immediate-object nil :boolean) to fill method arguments when they need "boolean". Why not let us just use T and NIL?
--binghe
在 2010-9-14,15:08, Alessio Stalla 写道:
On Tue, Sep 14, 2010 at 8:45 AM, Chun Tian (binghe) binghe.lisp@gmail.com wrote:
Hi again ... ABCL
I want to fill a Java byte[] array. But I don't know how to correctly use JARRAY-SET (I should use it, right?):
CL-USER(1): (defvar $*byte (jclass "byte")) $*BYTE CL-USER(2): (setf array (jnew-array $*byte 10)) #<jarray [B@7a2ee7e5 {1D7AAA0E}> CL-USER(3): (jarray-set array 1 0) #<THREAD "interpreter" {6E61A414}>: Debugger invoked on condition of type JAVA-EXCEPTION Java exception 'java.lang.IllegalArgumentException: argument type mismatch'. Restarts: 0: TOP-LEVEL Return to top level. [1] CL-USER(4): 0 CL-USER(5): (jarray-ref array 0) 0
So, a plain number "1" cannot work, but what else I can do? a object of java.lang.Byte?
You need to convert 1 to a Java byte... honestly I don't know if and how it can be done. Our fixnums are mapped to integers... we should probably extend jcoerce to handle numeric conversions as well. And conversion of numeric arrays, while we're at it.
And, is there any quick way I can fill a byte array using contents from a Lisp array?
The unexported system::%make-byte-array-output-stream and system::%get-output-stream-bytes come to my mind... but still you'll have to loop over the Lisp array. If there are no objections from other devs, I'd rather extend jcoerce to support your use case.
Cheers, Alessio
PS you can use (setf (jarray-ref array index) value) instead of jarray-set.
2010/9/14 Chun Tian (binghe) binghe.lisp@gmail.com:
Hi, Alessio
Thanks for your information.
I think, if you can make JARRAY-SET or (SETF JARRAY-REF) directly use Lisp (signed-byte 8) numbers, that will be better.
Right. We already handle bytes right in direct method calls, so there's no need to enhance jcoerce, just fixing jarray-set will do.
Beside, Java booleans has the same problem, I have to use (make-immediate-object t :boolean) and (make-immediate-object nil :boolean) to fill method arguments when they need "boolean". Why not let us just use T and NIL?
T works as a boolean, IIRC. With NIL, there's the problem that it's normally used to mean null rather than false.
2010/9/14 Chun Tian (binghe) binghe.lisp@gmail.com:
Hi, Alessio
Thanks for your information.
I think, if you can make JARRAY-SET or (SETF JARRAY-REF) directly use Lisp (signed-byte 8) numbers, that will be better.
Right. We already handle bytes right in direct method calls, so there's no need to enhance jcoerce, just fixing jarray-set will do.
Great.
Beside, Java booleans has the same problem, I have to use (make-immediate-object t :boolean) and (make-immediate-object nil :boolean) to fill method arguments when they need "boolean". Why not let us just use T and NIL?
T works as a boolean, IIRC. With NIL, there's the problem that it's normally used to mean null rather than false.
OK, I can live with current state. I defined two constants for this and re-use them every time when needed:
(defconstant +java-true+ (make-immediate-object t :boolean)) (defconstant +java-false+ (make-immediate-object nil :boolean))
--binghe
On Sep 14, 2010, at 09:08 , Alessio Stalla wrote:
On Tue, Sep 14, 2010 at 8:45 AM, Chun Tian (binghe) binghe.lisp@gmail.com wrote:
Hi again ... ABCL
I want to fill a Java byte[] array. But I don't know how to correctly use JARRAY-SET (I should use it, right?):
CL-USER(1): (defvar $*byte (jclass "byte")) $*BYTE CL-USER(2): (setf array (jnew-array $*byte 10)) #<jarray [B@7a2ee7e5 {1D7AAA0E}> CL-USER(3): (jarray-set array 1 0) #<THREAD "interpreter" {6E61A414}>: Debugger invoked on condition of type JAVA-EXCEPTION Java exception 'java.lang.IllegalArgumentException: argument type mismatch'. Restarts: 0: TOP-LEVEL Return to top level. [1] CL-USER(4): 0 CL-USER(5): (jarray-ref array 0) 0
So, a plain number "1" cannot work, but what else I can do? a object of java.lang.Byte?
You need to convert 1 to a Java byte... honestly I don't know if and how it can be done. Our fixnums are mapped to integers... we should probably extend jcoerce to handle numeric conversions as well. And conversion of numeric arrays, while we're at it.
As Alessio probably well knows, you can produce a java byte for say "23" briefly as follows
(jcall (jmethod "java.lang.Byte" "byteValue") (jnew "java.lang.Byte" (format nil "~A" 23)))
but it returns as a Lisp integer before you can grab that reference.
Extending JCOERCE is probably the way to go here.
-- "A screaming comes across the sky. It has happened before, but there is nothing to compare to it now."
armedbear-devel@common-lisp.net