From bc26bded3c8fd5b5993af6f3428da1d5b31eab96 Mon Sep 17 00:00:00 2001 From: drivenflywheel Date: Tue, 10 Oct 2023 18:46:22 +0000 Subject: [PATCH] KffFile: replaced knownFile.read(..byte[]) with knownFile.readFully(byte[]) to prevent bailing on incomplete reads. --- src/main/java/emissary/kff/KffFile.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/emissary/kff/KffFile.java b/src/main/java/emissary/kff/KffFile.java index 0faff04a7b..6b75ea652e 100755 --- a/src/main/java/emissary/kff/KffFile.java +++ b/src/main/java/emissary/kff/KffFile.java @@ -3,6 +3,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; import java.io.RandomAccessFile; @@ -139,8 +140,8 @@ public String getPreferredAlgorithm() { private boolean binaryFileSearch(@Nonnull byte[] hash, long crc) { // Initialize indexes for binary search - int low = 0; - int high = bSearchInitHigh; + long low = 0; + long high = bSearchInitHigh; /* Buffer to hold a record */ byte[] rec = new byte[recordLength]; @@ -150,15 +151,11 @@ private boolean binaryFileSearch(@Nonnull byte[] hash, long crc) { // Search until the indexes cross while (low <= high) { // Calculate the midpoint - int mid = (low + high) >> 1; + long mid = (low + high) >> 1; // Multiply the index by the record length to get the buffer position and read the record knownFile.seek(recordLength * mid); - int count = knownFile.read(rec); - if (count != recordLength) { - logger.warn("Short read on KffFile at {} read {} expected {}", (recordLength * mid), count, recordLength); - return false; - } + knownFile.readFully(rec); // Compare the record with the target. Adjust the indexes accordingly. int c = compare(rec, hash, crc); @@ -170,6 +167,9 @@ private boolean binaryFileSearch(@Nonnull byte[] hash, long crc) { return true; } } + } catch (EOFException e) { + // this shouldn't happen if we're synchronizing calls correctly + logger.warn("EOFException reading KffFile: {}", e.getLocalizedMessage()); } catch (IOException e) { logger.warn("Exception reading KffFile", e); } finally {