Jarrod Roberson Jarrod Roberson - 2 months ago 10
Groovy Question

How do you cast an int to a byte and test against an entry in a byte[]?

Given the following code how do I get rid of the


Method threw
'org.codehaus.groovy.runtime.typehandling.GroovyCastException'
exception. Cannot cast object 'true' with class 'java.lang.Boolean' to
class 'byte'


exception.

byte[] pattern = [0x78,0x9C]
int b = 0
while ((b = stream.read()) != -1) {
while ((byte)b != pattern.getAt(j)) {
/* omitted for brevity */
}
}


For some reason it is insisting that
b
is a
Boolean
when it is specifically declared as an
int
. This compiles and runs as Java code, but not as Groovy and I need this in a Groovy script.

Answer

The following code solves the problem:

b.byteValue() != pattern.getAt(j)