From ad31b66671de910e34c07624238c3f1f6d724dfe Mon Sep 17 00:00:00 2001 From: io42630 Date: Sun, 14 Aug 2022 09:45:50 +0200 Subject: [PATCH] + move hash to util --- .../ensync/artifacts/SyncDirectory.java | 22 ++------------ .../java/com/olexyn/ensync/util/HashUtil.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/olexyn/ensync/util/HashUtil.java diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java index 1d72cd1..ae9680f 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -108,7 +108,6 @@ public class SyncDirectory { * List is cleared and created each time. */ public Map makeListOfLocallyModifiedFiles(Map listFileSystem, Record record) { - return listFileSystem.entrySet().stream().filter( fileEntry -> { String fileKey = fileEntry.getKey(); @@ -121,23 +120,6 @@ public class SyncDirectory { ).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); } - private String getHash(Path path) { - var thisFc = LockKeeper.getFc(path); - try (var is = Channels.newInputStream(thisFc)) { - var m = MessageDigest.getInstance("SHA256"); - byte[] buffer = new byte[262144]; - int bytesRead; - while ((bytesRead = is.read(buffer)) != -1) { - m.update(buffer, 0, bytesRead); - } - var i = new BigInteger(1, m.digest()); - return String.format("%1$032X", i); - } catch (Exception e) { - LogU.warnPlain("Failed to create Hash.\n%s", e.getMessage()); - return null; - } - } - /** * QUERY state of the filesystem at realPath. * WRITE the state of the filesystem to file. @@ -241,8 +223,8 @@ public class SyncDirectory { LogU.infoPlain(" to: " + otherFile.toPath()); if (!thisFile.isFile()) { return; } if (otherFile.exists()) { - var thisHash = getHash(thisFile.toPath()); - var otherHash = getHash(otherFile.toPath()); + var thisHash = HashUtil.getHash(thisFile.toPath()); + var otherHash = HashUtil.getHash(otherFile.toPath()); if (thisHash == null || otherHash == null) { return; } if (thisHash.equals(otherHash)) { dropAge(thisFile, otherFile); diff --git a/src/main/java/com/olexyn/ensync/util/HashUtil.java b/src/main/java/com/olexyn/ensync/util/HashUtil.java new file mode 100644 index 0000000..b6f15c4 --- /dev/null +++ b/src/main/java/com/olexyn/ensync/util/HashUtil.java @@ -0,0 +1,30 @@ +package com.olexyn.ensync.util; + +import com.olexyn.ensync.lock.LockKeeper; +import com.olexyn.min.log.LogU; + +import java.math.BigInteger; +import java.nio.channels.Channels; +import java.nio.file.Path; +import java.security.MessageDigest; + +public class HashUtil { + + public static String getHash(Path path) { + var thisFc = LockKeeper.getFc(path); + try (var is = Channels.newInputStream(thisFc)) { + var m = MessageDigest.getInstance("SHA256"); + byte[] buffer = new byte[262144]; + int bytesRead; + while ((bytesRead = is.read(buffer)) != -1) { + m.update(buffer, 0, bytesRead); + } + var i = new BigInteger(1, m.digest()); + return String.format("%1$032X", i); + } catch (Exception e) { + LogU.warnPlain("Failed to create Hash.\n%s", e.getMessage()); + return null; + } + } + +}