-4

I would like to ask a question for computing a hashvalue for a empty file. I need to compute a hash_value when the file f is first created and is empty. Then, at the end I will update the hash_value again. My code is not working for windows os. Can you tell me how to handle this? Thanks.

            objectFile = File(fullFilePath);
            fileInputStream = FileInputStream(objectFile);
            data = IOUtils.toString(fileInputStream, 'UTF-8');

            persistent digest;            

            if isempty(digest)
                digest = MessageDigest.getInstance('SHA-256');
            end

            hash = digest.digest(java.lang.String(data).getBytes('UTF-8'));

Error Message

    digest.digest(java.lang.String(data).getBytes('UTF-8'))
    Java exception occurred:
    java.lang.NullPointerException

        at java.security.MessageDigest.update(Unknown Source)

        at java.security.MessageDigest.digest(Unknown Source)
susanna
  • 1,395
  • 3
  • 20
  • 32

1 Answers1

-1

It looks like data is null, so instead of the following statement:

hash = digest.digest(java.lang.String(data).getBytes('UTF-8'));

Can you write this:

if isEmpty(data)
    hash = 0;
else
    hash = digest.digest(java.lang.String(data).getBytes('UTF-8'));
end
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102