filechampion4j 0.9.8.3
File validation library for Java
Loading...
Searching...
No Matches
dev.filechampion.filechampion4j.CalculateChecksum Class Reference
Collaboration diagram for dev.filechampion.filechampion4j.CalculateChecksum:
Collaboration graph

Classes

class  ChecksumTask
 

Public Member Functions

 CalculateChecksum (byte[] inputData) throws IllegalArgumentException
 
byte[] getChecksum (String hashAlgorithm) throws NoSuchAlgorithmException, InterruptedException, ExecutionException, IOException
 

Private Member Functions

byte[] calculateSmallChecksum ()
 
byte[] calculateChecksum () throws InterruptedException, ExecutionException, IOException
 

Private Attributes

MessageDigest md
 
final byte[] inputData
 
int byteSize
 

Static Private Attributes

static final int MIN_CHUNK_SIZE = 1024 * 1024
 
static final int MAX_CHUNK_SIZE = 3 * 1024 * 1024
 

Detailed Description

This class is used to calculate the checksums of a file. It is optimized for large files and uses multiple threads to calculate the checksum in parallel.

Definition at line 20 of file CalculateChecksum.java.

Constructor & Destructor Documentation

◆ CalculateChecksum()

dev.filechampion.filechampion4j.CalculateChecksum.CalculateChecksum ( byte[]  inputData) throws IllegalArgumentException

Creates a new instance of the this class.

Parameters
inputData(bytep[]) The input data to calculate the checksum for.
Exceptions
IllegalArgumentExceptionThrown if the input data is null or empty.

Definition at line 32 of file CalculateChecksum.java.

32 {
33 if (inputData == null || inputData.length == 0) {
34 throw new IllegalArgumentException("Input data must contain at least one byte.");
35 }
36 this.inputData = inputData;
37 this.byteSize = inputData.length;
38 }

References dev.filechampion.filechampion4j.CalculateChecksum.inputData.

Member Function Documentation

◆ calculateChecksum()

byte[] dev.filechampion.filechampion4j.CalculateChecksum.calculateChecksum ( ) throws InterruptedException, ExecutionException, IOException
private

Calculates the checksum for large input data.

Returns
(byte[]) The calculated checksum.
Exceptions
InterruptedExceptionThrown if the thread is interrupted.
ExecutionExceptionThrown if the execution fails.
IOExceptionThrown if an I/O error occurs.

Definition at line 72 of file CalculateChecksum.java.

72 {
73 ByteArrayInputStream bais = new ByteArrayInputStream(inputData);
74 int numProcessors = Runtime.getRuntime().availableProcessors();
75 ThreadPoolExecutor executor = new ThreadPoolExecutor(numProcessors, numProcessors,
76 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
77 long fileSize = inputData.length;
78 long chunkSize = Math.max(MIN_CHUNK_SIZE, Math.min(fileSize / numProcessors, MAX_CHUNK_SIZE));
79 int numChunks = (int) Math.ceil((double) fileSize / chunkSize);
80 byte[] buffer = new byte[(int) chunkSize];
81 List<Future<?>> futures = new ArrayList<>(numChunks);
82 for (int i = 0; i < numChunks; i++) {
83 int bytesRead = bais.read(buffer, 0, (int) Math.min(chunkSize, fileSize - i * chunkSize));
84 Future<?> future = executor.submit(new ChecksumTask(md, buffer, bytesRead));
85 futures.add(future);
86 }
87 // wait for all futures to complete
88 for (Future<?> future : futures) {
89 future.get();
90 }
91 executor.shutdown();
92 bais.close();
93 return md.digest();
94 }

References dev.filechampion.filechampion4j.CalculateChecksum.inputData, dev.filechampion.filechampion4j.CalculateChecksum.MAX_CHUNK_SIZE, dev.filechampion.filechampion4j.CalculateChecksum.md, and dev.filechampion.filechampion4j.CalculateChecksum.MIN_CHUNK_SIZE.

Referenced by dev.filechampion.filechampion4j.CalculateChecksum.getChecksum().

Here is the caller graph for this function:

◆ calculateSmallChecksum()

byte[] dev.filechampion.filechampion4j.CalculateChecksum.calculateSmallChecksum ( )
private

Calculates the checksum for small input data.

Returns
(byte[]) The calculated checksum.

Definition at line 61 of file CalculateChecksum.java.

61 {
62 return md.digest(inputData);
63 }

References dev.filechampion.filechampion4j.CalculateChecksum.inputData, and dev.filechampion.filechampion4j.CalculateChecksum.md.

Referenced by dev.filechampion.filechampion4j.CalculateChecksum.getChecksum().

Here is the caller graph for this function:

◆ getChecksum()

byte[] dev.filechampion.filechampion4j.CalculateChecksum.getChecksum ( String  hashAlgorithm) throws NoSuchAlgorithmException, InterruptedException, ExecutionException, IOException

Calculates the checksum for the input data.

Parameters
hashAlgorithm(String) The hash algorithm to use. Must be one of: MD5, SHA-1, SHA-256, SHA-512.
Returns
(byte[]) The calculated checksum.
Exceptions
NoSuchAlgorithmExceptionThrown if the algorithm is not available.
InterruptedExceptionThrown if the thread is interrupted.
ExecutionExceptionThrown if the execution fails.
IOExceptionThrown if an I/O error occurs.

Definition at line 49 of file CalculateChecksum.java.

References dev.filechampion.filechampion4j.CalculateChecksum.byteSize, dev.filechampion.filechampion4j.CalculateChecksum.calculateChecksum(), dev.filechampion.filechampion4j.CalculateChecksum.calculateSmallChecksum(), dev.filechampion.filechampion4j.CalculateChecksum.md, and dev.filechampion.filechampion4j.CalculateChecksum.MIN_CHUNK_SIZE.

Referenced by dev.filechampion.filechampion4j.FileValidator.calculateChecksum(), and dev.filechampion.filechampion4j.CliPluginHelper.calculateChecksum().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ byteSize

int dev.filechampion.filechampion4j.CalculateChecksum.byteSize
private

◆ inputData

◆ MAX_CHUNK_SIZE

final int dev.filechampion.filechampion4j.CalculateChecksum.MAX_CHUNK_SIZE = 3 * 1024 * 1024
staticprivate

◆ md

◆ MIN_CHUNK_SIZE

final int dev.filechampion.filechampion4j.CalculateChecksum.MIN_CHUNK_SIZE = 1024 * 1024
staticprivate

The documentation for this class was generated from the following file: