FEAT StreamUtils.digestStream + FileUtils.digestFileContent
NClazz/java-commons/pipeline/head This commit looks good
Details
NClazz/java-commons/pipeline/head This commit looks good
Details
parent
5d0f99dc0b
commit
7cc05720fc
|
@ -1,14 +1,13 @@
|
|||
package de.nclazz.commons.io;
|
||||
|
||||
import de.nclazz.commons.StringUtils;
|
||||
import lombok.NonNull;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.Writer;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public abstract class FileUtils {
|
||||
|
@ -95,7 +94,7 @@ public abstract class FileUtils {
|
|||
|
||||
if(!file.exists()) file.createNewFile();
|
||||
|
||||
try(FileWriter writer = new FileWriter(file);) {
|
||||
try(FileWriter writer = new FileWriter(file)) {
|
||||
writerConsumer.accept(writer);
|
||||
}catch(Exception e) {
|
||||
throw e;
|
||||
|
@ -104,4 +103,29 @@ public abstract class FileUtils {
|
|||
return file;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a hash from the file content of given file with given{@link MessageDigest}
|
||||
* @param file to hash
|
||||
* @param md hash algorithm
|
||||
* @return hex {@code String} hash
|
||||
*/
|
||||
public static String digestFileContent(@NonNull File file, @NonNull MessageDigest md) {
|
||||
try(FileInputStream stream = new FileInputStream(file)) {
|
||||
return StreamUtils.digestStream(stream, md);
|
||||
}catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static String generateSHA1FromFile(File file) {
|
||||
return digestFileContent(file, MessageDigest.getInstance("SHA-1"));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static String generateMD5FromFile(File file) {
|
||||
return digestFileContent(file, MessageDigest.getInstance("MD5"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package de.nclazz.commons.io;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import de.nclazz.commons.StringUtils;
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public abstract class StreamUtils {
|
||||
|
||||
|
@ -15,7 +16,7 @@ public abstract class StreamUtils {
|
|||
*
|
||||
* @param streamIn InputStream
|
||||
* @param streamOut OutputStream
|
||||
* @throws IOException
|
||||
* @throws IOException on Error
|
||||
*/
|
||||
public static void pipeStream(InputStream streamIn, OutputStream streamOut) throws IOException {
|
||||
byte[] bytes = new byte[4096]; //TODO make configurable? maybe as a sys property?
|
||||
|
@ -41,6 +42,26 @@ public abstract class StreamUtils {
|
|||
return bos.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Digest an InputStream with given MessageDigest.
|
||||
* The stream will not be closed after processing!
|
||||
* @param stream to hash
|
||||
* @param md hash algorithm
|
||||
* @return new String hash
|
||||
*/
|
||||
public static String digestStream(@NonNull InputStream stream, @NonNull MessageDigest md) {
|
||||
try {
|
||||
byte[] buffer = new byte[2048];
|
||||
int len;
|
||||
while((len = stream.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, len);
|
||||
}
|
||||
byte[] digest = md.digest();
|
||||
return StringUtils.toHex(digest);
|
||||
}catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
@ -22,15 +23,19 @@ public class FileUtilsTests {
|
|||
assertThrows(IllegalArgumentException.class, () -> FileUtils.deleteDirectoryRecursively(readmeFile));
|
||||
assertDoesNotThrow(() -> FileUtils.deleteDirectoryRecursively(dir));
|
||||
|
||||
assertTrue(!dir.exists());
|
||||
assertFalse(dir.exists());
|
||||
}
|
||||
|
||||
File createDirectory() throws IOException {
|
||||
File dir = new File("target/tmp/subdirs/secondlevel");
|
||||
dir.mkdirs();
|
||||
if(!dir.mkdirs())
|
||||
throw new RuntimeException("Could not create directories!");
|
||||
|
||||
for(int i = 0; i < 64; i++) {
|
||||
new File(dir, "test_"+i+".txt").createNewFile();
|
||||
String filename = "test_" + i + ".txt";
|
||||
if(!new File(dir, filename).createNewFile()) {
|
||||
throw new RuntimeException("Could not create File +'" + filename + "'!");
|
||||
}
|
||||
}
|
||||
|
||||
return dir;
|
||||
|
@ -66,4 +71,13 @@ public class FileUtilsTests {
|
|||
assertFalse(tempFile.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDigestFileContent() {
|
||||
File file = new File("src/test/resources/test-files/alphabet-utf8.txt");
|
||||
String sha1 = "db16441c4b330570a9ac83b0e0b006fcd74cc32b";
|
||||
String md5 = "2ad372c377013baa4ee32ab6649d2449";
|
||||
|
||||
assertEquals(sha1, FileUtils.generateSHA1FromFile(file));
|
||||
assertEquals(md5, FileUtils.generateMD5FromFile(file));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue