Skip to content

Commit

Permalink
Fix recently broken ModuleBase.skipBytes method
Browse files Browse the repository at this point in the history
Broken during incorrect merge of PR openpreserve#117. Each loop of the skip method
was skipping the same amount of bytes, not subtracting the amount already
skipped. Reverted to original pull request code then refactored.

Fixes openpreserve#71, fixes openpreserve#193.
  • Loading branch information
david-russo committed Mar 17, 2017
1 parent 93f9f5a commit e26d5fe
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions jhove-core/src/main/java/edu/harvard/hul/ois/jhove/ModuleBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ public static float readFloat (DataInputStream stream, boolean endian,
}

public static double readDouble (RandomAccessFile file, boolean endian)
throws IOException
throws IOException
{
double f = 0.0F;
if (endian) {
Expand Down Expand Up @@ -1354,32 +1354,31 @@ public static double readDouble (DataInputStream stream, boolean endian,
return f;
}

/* Skip over some bytes. */
public long skipBytes(DataInputStream stream, long bytesToSkip)
throws IOException
/** Skip over some bytes. Return number of bytes skipped. */
public long skipBytes(DataInputStream stream, long bytesToSkip)
throws IOException
{
return skipBytes (stream, bytesToSkip, null);
return skipBytes(stream, bytesToSkip, null);
}

/* Skip over some bytes. */
public long skipBytes(DataInputStream stream, long bytesToSkip,
ModuleBase counted)
throws IOException
/** Skip over some bytes. Return number of bytes skipped. */
public long skipBytes(DataInputStream stream, long bytesToSkip,
ModuleBase counted)
throws IOException
{
long retVal = 0;
long bytesLeft = bytesToSkip;
while (bytesLeft > 0) {
long n = stream.skip(bytesToSkip);
bytesLeft -= n;
retVal += n;
if (counted != null) {
counted._nByte += n;
}
if (stream.available() == 0) {
break;
}
long totalBytesSkipped = 0;
while (bytesToSkip > 0) {
long bytesSkipped = stream.skip(bytesToSkip);
totalBytesSkipped += bytesSkipped;
bytesToSkip -= bytesSkipped;
// Cease skipping if end of stream reached before
// requested number of bytes have been skipped.
if (stream.available() == 0) break;
}
if (counted != null) {
counted._nByte += totalBytesSkipped;
}
return retVal;
return totalBytesSkipped;
}

/**
Expand Down

0 comments on commit e26d5fe

Please sign in to comment.