piks
piks

Reputation: 1673

skip method always returns 0 of Inputstream

I have to generate checksum by reading the file and for that, first , I have to read the 8 bytes header and then skip some of bytes(that would be variable length and it is available for each file). But after reading first 8 bytes and storing to the byte array, if I run skip on the same inputstream, it always returns 0. I am not able to find out what could be the reason for that. Please find below the part of code:

    MessageDigest md = MessageDigest.getInstance("SHA1");
DigestInputStream din = new DigestInputStream(inputstream, md);
while(din.read(headerArr,0,8) != -1){
}
byte[]headerData = md.digest();
//skip some bytes
long skippedBytes = inputstream.skip(bytesTobeskipped);

But, If I don't read the header then skip works fine, it tries to skip the provided bytes. So, please help me out to resolve this issue or point me out what am I doing wrong.

Upvotes: 0

Views: 307

Answers (1)

user207421
user207421

Reputation: 310998

But after reading first 8 bytes

while(din.read(headerArr,0,8) != -1){
    }

You're not reading the first 8 bytes. You're reading the last 8 bytes, or rather the last bytes after reading some unknown number of prior bytes.

Change the while to an if, and don't ignore the count returned by the read() method.

Upvotes: 1

Related Questions