question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HashingInputStream does not override skip()

See original GitHub issue

HashingInputStream does not override FilterInputStream.skip(long). Therefore any skipped data is not considered by the hash calculation. I doubt that this is intended, though if it is, can this please be clearly documented in the description of the method?

A usecase where skipping data is important is when working with ZipInputStreams:

HashingInputStream hashIn = ...;
ZipInputStream zipIn = new ZipInputStream(hashIn);

for (ZipEntry zipEntry; (zipEntry = zipIn.getNextEntry()) != null;) {
    // Read entry
    ...
}

// Skip all remaining data which was not part of the last ZipEntry
while (hashIn.read() != -1) {
    hashIn.skip(Long.MAX_VALUE);
}
HashCode hashCode = hashIn.hash();

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Marcono1234commented, Jul 13, 2020

Thank you very much! The case I described in the description of this issue shows one situation where it is desired that skip() hashes the data. Though maybe there are indeed cases where it is not.

Regarding DigestInputStream, there is JDK-6587699 though there hasn’t been much activity.

0reactions
cpovirkcommented, Jul 13, 2020

Our notes (from 2013!) give these points against hashing the skipped data:

This requires skip() to actually perform reads, potentially slowing it down.

DigestInputStream doesn’t hash skipped characters. But I could totally believe this is nothing but an oversight, especially since it doesn’t do anything sensible with mark()/reset(), either.

fundamental problem here is that filter-streams that “also do X” are intrinsically weird.

Weighing against that was:

If you pass a HashingInputStream for a file to some arbitrary “read this file” method, you don’t care which methods it’s calling. If its implementation switches from read() to skip(1), your hashing should still work.

Also:

None of our existing users care either way.

It’s entirely possible that even one user who did care could have shifted the decision.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HashingInputStream (Guava: Google Core Libraries for Java ...
Reads the specified bytes of data from the underlying input stream and updates the hasher with the bytes read. void, reset(). reset() is...
Read more >
guava/HashingInputStream.java at master - GitHub
mark() is not supported for HashingInputStream. *. * @return {@code false} always. */. @Override. public boolean markSupported() {. return false;. }.
Read more >
Error converting InputStream to a BinaryValue| JBoss.org Content ...
In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large...
Read more >
Robust skipping of data in a java.io.InputStream and its subtypes
I don't think we can get a really robust implementation because the skip() method contract is rather bizarre. For one thing, the behaviour...
Read more >
Java example - HashingInputStreamTest.java ... - Alvin Alexander
This example Java source code file (HashingInputStreamTest.java) is included in ... hashinginputstream, hashinginputstreamtest, nullpointertester, override, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found