From abfe0a925a1733748abb5e7dabafd12fbeef8c24 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Thu, 17 Jun 2021 18:20:42 +0100 Subject: [PATCH] More DataFile methods and improvements --- .../java/org/qortal/storage/DataFile.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/qortal/storage/DataFile.java b/src/main/java/org/qortal/storage/DataFile.java index e4d727d1..0a54555b 100644 --- a/src/main/java/org/qortal/storage/DataFile.java +++ b/src/main/java/org/qortal/storage/DataFile.java @@ -102,6 +102,10 @@ public class DataFile { return new DataFile(filePath); } + public static DataFile fromDigest(byte[] digest) { + return DataFile.fromBase58Digest(Base58.encode(digest)); + } + private boolean createDataDirectory() { // Create the data directory if it doesn't exist String dataPath = Settings.getInstance().getDataPath(); @@ -153,7 +157,7 @@ public class DataFile { // Validate the file size long fileSize = Files.size(path); if (fileSize > MAX_FILE_SIZE) { - LOGGER.error(String.format("DataFile is too large: %d bytes (max chunk size: %d bytes)", fileSize, MAX_FILE_SIZE)); + LOGGER.error(String.format("DataFile is too large: %d bytes (max size: %d bytes)", fileSize, MAX_FILE_SIZE)); return DataFile.ValidationResult.FILE_TOO_LARGE; } @@ -250,6 +254,18 @@ public class DataFile { } } + public byte[] getBytes() { + Path path = Paths.get(this.filePath); + try { + byte[] bytes = Files.readAllBytes(path); + LOGGER.info("getBytes: %d", bytes); + return bytes; + } catch (IOException e) { + LOGGER.error("Unable to read bytes for file"); + return null; + } + } + /* Helper methods */ @@ -263,6 +279,20 @@ public class DataFile { return false; } + public boolean exists() { + File file = new File(this.filePath); + return file.exists(); + } + + public long size() { + Path path = Paths.get(this.filePath); + try { + return Files.size(path); + } catch (IOException e) { + return 0; + } + } + private File getFile() { File file = new File(this.filePath); if (file.exists()) { @@ -298,6 +328,8 @@ public class DataFile { } return this.base58Digest().substring(0, Math.min(this.base58Digest().length(), SHORT_DIGEST_LENGTH)); } + + @Override public String toString() { return this.shortDigest(); }