in memory to stream

This commit is contained in:
PhilReact 2025-06-01 20:31:36 +03:00
parent fa8b9f2cee
commit 517f7b92d5

View File

@ -4,6 +4,7 @@ import org.qortal.repository.DataException;
import org.qortal.utils.Base58; import org.qortal.utils.Base58;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.Files; import java.nio.file.Files;
@ -25,7 +26,11 @@ public class ArbitraryDataDigest {
} }
public void compute() throws IOException, DataException { public void compute() throws IOException, DataException {
List<Path> allPaths = Files.walk(path).filter(Files::isRegularFile).sorted().collect(Collectors.toList()); List<Path> allPaths = Files.walk(path)
.filter(Files::isRegularFile)
.sorted()
.collect(Collectors.toList());
Path basePathAbsolute = this.path.toAbsolutePath(); Path basePathAbsolute = this.path.toAbsolutePath();
MessageDigest sha256; MessageDigest sha256;
@ -48,25 +53,27 @@ public class ArbitraryDataDigest {
// Account for \ VS / : Linux VS Windows // Account for \ VS / : Linux VS Windows
String pathString = relativePath.toString(); String pathString = relativePath.toString();
if (relativePath.getFileSystem().toString().contains("Windows")) {
if(relativePath.getFileSystem().toString().contains("Windows")) { pathString = pathString.replace("\\", "/");
pathString = pathString.replace("\\","/");
} }
// Hash path // Hash path
byte[] filePathBytes = pathString.getBytes(StandardCharsets.UTF_8); byte[] filePathBytes = pathString.getBytes(StandardCharsets.UTF_8);
System.out.printf("Path: %s \n", pathString);
System.out.printf("Path Byte Array: %s \n", Arrays.toString(filePathBytes));
sha256.update(filePathBytes); sha256.update(filePathBytes);
// Hash contents try (InputStream in = Files.newInputStream(path)) {
byte[] fileContent = Files.readAllBytes(path); byte[] buffer = new byte[65536]; // 64 KB
System.out.printf("File Content: %s \n", Arrays.toString(fileContent)); int bytesRead;
sha256.update(fileContent); while ((bytesRead = in.read(buffer)) != -1) {
sha256.update(buffer, 0, bytesRead);
}
}
} }
this.hash = sha256.digest(); this.hash = sha256.digest();
} }
public boolean isHashValid(byte[] hash) { public boolean isHashValid(byte[] hash) {
return Arrays.equals(hash, this.hash); return Arrays.equals(hash, this.hash);
} }