forked from Qortal/qortal
Moved DataFileChunk(byte[] fileContent) constructor to the superclass so it can be used by regular data files too.
This commit is contained in:
parent
1e8dbfe4b7
commit
fa11f4f45b
@ -72,6 +72,31 @@ public class DataFile {
|
|||||||
this(file.getPath());
|
this(file.getPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DataFile(byte[] fileContent) {
|
||||||
|
if (fileContent == null) {
|
||||||
|
LOGGER.error("fileContent is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String base58Digest = Base58.encode(Crypto.digest(fileContent));
|
||||||
|
LOGGER.debug(String.format("File digest: %s, size: %d bytes", base58Digest, fileContent.length));
|
||||||
|
|
||||||
|
String outputFilePath = this.getOutputFilePath(base58Digest);
|
||||||
|
File outputFile = new File(outputFilePath);
|
||||||
|
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
|
||||||
|
outputStream.write(fileContent);
|
||||||
|
this.filePath = outputFilePath;
|
||||||
|
// Verify hash
|
||||||
|
if (!base58Digest.equals(this.base58Digest())) {
|
||||||
|
LOGGER.error("Digest {} does not match file digest {}", base58Digest, this.base58Digest());
|
||||||
|
this.delete();
|
||||||
|
throw new IllegalStateException("Data file digest validation failed");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Unable to write data to file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static DataFile fromBase58Digest(String base58Digest) {
|
public static DataFile fromBase58Digest(String base58Digest) {
|
||||||
String filePath = DataFile.getOutputFilePath(base58Digest);
|
String filePath = DataFile.getOutputFilePath(base58Digest);
|
||||||
return new DataFile(filePath);
|
return new DataFile(filePath);
|
||||||
|
@ -21,28 +21,7 @@ public class DataFileChunk extends DataFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DataFileChunk(byte[] fileContent) {
|
public DataFileChunk(byte[] fileContent) {
|
||||||
if (fileContent == null) {
|
super(fileContent);
|
||||||
LOGGER.error("Chunk fileContent is null");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String base58Digest = Base58.encode(Crypto.digest(fileContent));
|
|
||||||
LOGGER.debug(String.format("Chunk digest: %s, size: %d bytes", base58Digest, fileContent.length));
|
|
||||||
|
|
||||||
String outputFilePath = this.getOutputFilePath(base58Digest);
|
|
||||||
File outputFile = new File(outputFilePath);
|
|
||||||
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
|
|
||||||
outputStream.write(fileContent);
|
|
||||||
this.filePath = outputFilePath;
|
|
||||||
// Verify hash
|
|
||||||
if (!base58Digest.equals(this.base58Digest())) {
|
|
||||||
LOGGER.error("Digest {} does not match file digest {}", base58Digest, this.base58Digest());
|
|
||||||
this.delete();
|
|
||||||
throw new IllegalStateException("DataFileChunk digest validation failed");
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalStateException("Unable to write chunk data to file");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user