Forum Discussion

Terkon's avatar
Terkon
Contributor
4 years ago
Solved

Groovy: get byte array from file returns different values each run

Hi!

 

I want to get an byte array of a file via groovy. I use this:

def contents = new File(filePathName).bytes

 

"filePathName" is defined as d:\test.txt

test.txt contains "Hello!"

 

If I execute the script in SoapUI it returns

[B@711f9010]

 

The next time, same file, same everything, just a new execution, it returns for example:

[B@129dc10c]

 

and so on.

 

Why is that?

I get a similar response when I use a larger zip file for this.

The path is correct, If I change it to something wrong, or change the file name to something invalid, I get an error message.

  • Hi,

     

    I think what you're seeing is the object id, which changes every time you call the script.

     

    As bytes is an array, you need to access it like an array,  E.g.

     

    byte[] bytes = new File(filePathName).bytes;
    log.info(bytes[0]);
    log.info(bytes[1]);

     

    Or, the whole array....

    byte[] bytes = new File(filePathName).bytes;
    log.info(bytes.toString());

    Note how the values are the same each time you call it.

     

2 Replies

  • Hi,

     

    I think what you're seeing is the object id, which changes every time you call the script.

     

    As bytes is an array, you need to access it like an array,  E.g.

     

    byte[] bytes = new File(filePathName).bytes;
    log.info(bytes[0]);
    log.info(bytes[1]);

     

    Or, the whole array....

    byte[] bytes = new File(filePathName).bytes;
    log.info(bytes.toString());

    Note how the values are the same each time you call it.