Ross, Bill wrote:
>
> I am new to Java but have doing TCL for many years.
>
> I have one application that requires java.
>
> I have gotten it all working but am now trying to add a new feature.
>
> I get an object “cstat” back using tclbelnd.
>
> According to the Javadoc I need to read it back into a byte array.
>
> I then want to write it to a file using “$fos {write byte[]} $dataBytes;”
>
> I can’t figure out the right syntax in tclblend to do this.
>
> I can use a different Java command to get the size of the file which
> is 3188.
>
> I tried creating a byte array of this size .
>
> Any help would be appreciated.
>
> Here is the line from the javadoc that shows the Java code that does
> the data
>
> byte[] bytes = (byte[])cstat.getData();
>
> Here is the relevant tclblend code I tried
>
> set fos [java::new java.io.FileOutputStream
> "/home/server/output/filename"];
>
> set dataBytes [java::new {byte[]} 3188];
>
> set dataBytes [$cstat getData];
>
> $fos {write byte[]} $dataBytes;
>
> $fos close;
>
> When I run my code I get
>
> expected object of type byte[] but got "java0xc" (java.lang.Object)
>
> while executing
>
> "$fos {write byte[]} $dataBytes"
>
Your code does not really make sense:
set dataBytes [java::new {byte[]} 3188]
set dataBytes [$cstat getData]
Why allocate a byte[] and then assign it to dataBytes in step 1? Did you
mean to pass that to the getData method or something?
The error indicates that the type of dataBytes after the call to getData
is java.lang.Object. Likely that is what your method returns as the
object type. You could change your method so that the return type is
byte[] in the Java code. Otherwise, you could use the java::cast command
to cast the java.lang.Object result to byte[] (that step is missing in
your Tcl code, but it appears in the Java snip you provide).
cheers
Mo DeJong
|