Feat: cache md5 file value
This commit is contained in:
62
src/main/java/com/sheepit/client/Md5.java
Normal file
62
src/main/java/com/sheepit/client/Md5.java
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Laurent CLOUET
|
||||
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; version 2
|
||||
* of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package com.sheepit.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Md5 {
|
||||
private static Map<String, String> cache = new HashMap<>();
|
||||
|
||||
// TODO: check if the file has changed (cache the time of modification)
|
||||
// TODO: to avoid memory increase, check if files are deleted
|
||||
|
||||
public String get(String path) {
|
||||
if (cache.containsKey(path) == false) {
|
||||
generate(path);
|
||||
}
|
||||
return cache.get(path);
|
||||
}
|
||||
|
||||
public void generate(String path) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
InputStream is = Files.newInputStream(Paths.get(path));
|
||||
DigestInputStream dis = new DigestInputStream(is, md);
|
||||
byte[] buffer = new byte[8192];
|
||||
while (dis.read(buffer) > 0)
|
||||
; // process the entire file
|
||||
String data = Utils.convertBinaryToHex(md.digest());
|
||||
dis.close();
|
||||
is.close();
|
||||
cache.put(path, data);
|
||||
}
|
||||
catch (NoSuchAlgorithmException | IOException e) {
|
||||
cache.put(path, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,21 +68,8 @@ public class Utils {
|
||||
}
|
||||
|
||||
public static String md5(String path_of_file_) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
InputStream is = Files.newInputStream(Paths.get(path_of_file_));
|
||||
DigestInputStream dis = new DigestInputStream(is, md);
|
||||
byte[] buffer = new byte[8192];
|
||||
while (dis.read(buffer) > 0)
|
||||
; // process the entire file
|
||||
String data = convertBinaryToHex(md.digest());
|
||||
dis.close();
|
||||
is.close();
|
||||
return data;
|
||||
}
|
||||
catch (NoSuchAlgorithmException | IOException e) {
|
||||
return "";
|
||||
}
|
||||
Md5 md5 = new Md5();
|
||||
return md5.get(path_of_file_);
|
||||
}
|
||||
|
||||
public static String convertBinaryToHex(byte[] bytes) {
|
||||
|
||||
Reference in New Issue
Block a user