Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/src/main/java/com/dynatrace/file/util/PollBasedFilePoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
Expand All @@ -32,7 +33,7 @@ class PollBasedFilePoller extends FilePoller {
private final ScheduledExecutorService executorService;

private final CRC32 crc32 = new CRC32();
private long prevChecksumValue = 0;
private Long prevChecksumValue = null;

protected PollBasedFilePoller(Path filePath, Duration pollInterval) {
super(filePath);
Expand Down Expand Up @@ -64,27 +65,30 @@ public boolean fileContentsUpdated() {
}

private synchronized void poll() {
long latestChecksumValue = getChecksumValue();
if (latestChecksumValue != prevChecksumValue) {
prevChecksumValue = latestChecksumValue;
Long latestChecksumValue = getChecksumValue();

// If the latest checksum value is a value other than zero, it means that the file is not
// empty.
// The if check above ensures that it is also different from the previous checksum, so the
// file contents must have been updated.
if (latestChecksumValue != 0) {
// | latest | previous | outcome |
// | ------ | -------- | ----------------------- |
// | null | null | do nothing |
// | null | !null | do nothing |
// | !null | null | update (no file before) |
// | !null | !null | update if changed |
if (latestChecksumValue != null) {
if (!prevChecksumValue.equals(latestChecksumValue)) {
changedSinceLastInquiry.set(true);
}
prevChecksumValue = latestChecksumValue;
}
}

private synchronized long getChecksumValue() {
private synchronized Long getChecksumValue() {
crc32.reset();
try {
byte[] fileBytes = Files.readAllBytes(absoluteFilePath);
crc32.update(fileBytes, 0, fileBytes.length);
} catch (IOException e) {
LOGGER.warning(() -> String.format(LOG_MESSAGE_FAILED_FILE_READ, absoluteFilePath, e));
return null;
}
return crc32.getValue();
}
Expand Down
Loading