Вопрос

I need to convert an integer (representing x.509 KeyUsage mask) to boolean array, in Java I have following function available which is working fine:

public boolean[] getKeyUsage()
{
    int mask =  144; // Integer.parseInt(testRunner.testCase.getPropertyValue("keyUsage")); 
    byte[] bytes = new byte[] { (byte)(mask & 0xff), (byte)((mask & 0xff00) >> 8) };
    boolean[] keyUsage = new boolean[9];

    for (int i = 0; i != 9; i++)
    {
        keyUsage[i] = (bytes[i / 8] & (0x80 >>> (i % 8))) != 0;
    }
    return keyUsage;
}

In this case (144), it should return

keyUsage = {boolean[9]@976} [0] = true [1] = false [2] = false [3] = true [4] = false [5] = false [6] = false [7] = false [8] = false

When running this in Groovy, I get

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script6.groovy: 22: expecting '}', found ',' @ line 22, column 56. w byte[] { (byte)(mask & 0xff), (byte)(( ^ org.codehaus.groovy.syntax.SyntaxException: expecting '}', found ',' @ line 22, column 56. at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:140) at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108) at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236) at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:162) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:912) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:574) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:550) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:527) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:613) at groovy.lang.GroovyShell.parse(GroovyShell.java:625) at groovy.lang.GroovyShell.parse(GroovyShell.java:652) at groovy.lang.GroovyShell.parse(GroovyShell.java:643) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:148) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:93) at

I am a total n00b in Groovy, so sorry if I am missing something obvious

Это было полезно?

Решение

I think this gets the result you need:

boolean[] getKeyUsage( int mask ) {
    (7..0).collect { int it ->
        ( ( mask >> it ) & 1 )
    }
}

getKeyUsage( 144 ) == [ true, false, false, true, false, false, false, false ]

The error you were getting is because you construct arrays in Groovy with:

byte[] bytes = [ 1, 2 ]

Not the new byte[] { 1, 2 } Java way. But I don't believe you need the byte array...

Другие советы

In Groovy the following is not valid:

int mask =  144;
byte[] bytes = new byte[] { (byte)(mask & 0xff), (byte)((mask & 0xff00) >> 8) };

The following is valid:

int mask =  144;
byte[] bytes = [(byte)(mask & 0xff), (byte)((mask & 0xff00) >> 8)];

You can eliminate the casts if you like:

int mask =  144;
byte[] bytes = [(mask & 0xff), ((mask & 0xff00) >> 8)];

You can also eliminate the semicolons if you like.

I hope that helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top