Package nl.knaw.dans.bagit.hash
Class Hasher
java.lang.Object
nl.knaw.dans.bagit.hash.Hasher
Convenience class for generating a HEX formatted string of the checksum hash.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classRepresents the result of a chunk operation when processing data for hashing.private static enumprivate static interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate static final Stringprivate static final longprivate static final intprivate static final intprivate static final org.slf4j.Loggerprivate static final Stringprivate static final ResourceBundleprivate static final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidaddMessageDigestHashToManifest(Path path, Map<Manifest, MessageDigest> manifestToMessageDigestMap) static Map<Manifest,MessageDigest> createManifestToMessageDigestMap(Collection<SupportedAlgorithm> algorithms) private static Hasher.ChunkResultexecuteWithRetry(Hasher.RetryableOperation<Hasher.ChunkResult> operation, String description, int maxRetries, int retrySleepMs) private static StringformatMessageDigest(MessageDigest messageDigest) private static Hasher.ChunkResulthandlePartialContent(HttpURLConnection conn, MessageDigest messageDigest, long currentTotalSize) static Stringhash(URL url, MessageDigest messageDigest) Create a HEX formatted string checksum hash of the data from the URLstatic StringCreate a HEX formatted string checksum hash of the data from the URLstatic Stringhash(Path path, MessageDigest messageDigest) Create a HEX formatted string checksum hash of the filestatic voidhash(Path path, Map<Manifest, MessageDigest> manifestToMessageDigestMap) Update the Manifests with the file's hashstatic StringCreate a HEX formatted string checksum hash of the data from theFetchItemprivate static StringhashFullStream(URL url, MessageDigest messageDigest, Map<String, String> extraHeaders) private static HttpURLConnectionOpens a HttpURLConnection for the given range.private static intupdateDigestFromStream(InputStream is, MessageDigest messageDigest) Reads from the InputStream and updates the MessageDigest.(package private) static voidupdateMessageDigests(Path path, Collection<MessageDigest> messageDigests)
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
_64_KB
private static final int _64_KB- See Also:
-
CHUNK_SIZE
private static final int CHUNK_SIZE- See Also:
-
messages
-
CHUNK_SIZE_PROP
- See Also:
-
MAX_RETRIES_PROP
- See Also:
-
RETRY_SLEEP_MS_PROP
- See Also:
-
DEFAULT_CHUNK_SIZE
private static final long DEFAULT_CHUNK_SIZE- See Also:
-
DEFAULT_MAX_RETRIES
private static final int DEFAULT_MAX_RETRIES- See Also:
-
DEFAULT_RETRY_SLEEP_MS
private static final int DEFAULT_RETRY_SLEEP_MS- See Also:
-
-
Constructor Details
-
Hasher
private Hasher()
-
-
Method Details
-
hash
Create a HEX formatted string checksum hash of the file- Parameters:
path- thePath(file) to hashmessageDigest- theMessageDigestobject representing the hashing algorithm- Returns:
- the hash as a hex formated string
- Throws:
IOException- if there is a problem reading the file
-
hash
Create a HEX formatted string checksum hash of the data from the URL- Parameters:
url- theURLto hashmessageDigest- theMessageDigestobject representing the hashing algorithm- Returns:
- the hash as a hex-formatted string
- Throws:
IOException- if there is a problem reading from the URL
-
hash
public static String hash(FetchItem item, MessageDigest messageDigest, Map<String, String> extraHeaders) throws IOExceptionCreate a HEX formatted string checksum hash of the data from theFetchItem- Parameters:
item- theFetchItemto hashmessageDigest- theMessageDigestobject representing the hashing algorithmextraHeaders- optional extra headers to send with the request- Returns:
- the hash as a hex formatted string
- Throws:
IOException- if there is a problem reading from the URL
-
hashFullStream
private static String hashFullStream(URL url, MessageDigest messageDigest, Map<String, String> extraHeaders) throws IOException- Throws:
IOException
-
hash
public static String hash(URL url, MessageDigest messageDigest, Map<String, String> extraHeaders) throws IOExceptionCreate a HEX formatted string checksum hash of the data from the URL- Parameters:
url- theURLto hashmessageDigest- theMessageDigestobject representing the hashing algorithmextraHeaders- optional extra headers to send with the request- Returns:
- the hash as a hex formatted string
- Throws:
IOException- if there is a problem reading from the URL
-
handlePartialContent
private static Hasher.ChunkResult handlePartialContent(HttpURLConnection conn, MessageDigest messageDigest, long currentTotalSize) throws IOException - Throws:
IOException
-
executeWithRetry
private static Hasher.ChunkResult executeWithRetry(Hasher.RetryableOperation<Hasher.ChunkResult> operation, String description, int maxRetries, int retrySleepMs) throws IOException - Throws:
IOException
-
openRangedConnection
private static HttpURLConnection openRangedConnection(URL url, String range, Map<String, String> extraHeaders) throws IOExceptionOpens a HttpURLConnection for the given range.- Throws:
IOException
-
updateDigestFromStream
private static int updateDigestFromStream(InputStream is, MessageDigest messageDigest) throws IOException Reads from the InputStream and updates the MessageDigest. Returns the number of bytes read.- Throws:
IOException
-
hash
public static void hash(Path path, Map<Manifest, MessageDigest> manifestToMessageDigestMap) throws IOExceptionUpdate the Manifests with the file's hash- Parameters:
path- thePath(file) to hashmanifestToMessageDigestMap- the map betweenManifestandMessageDigest- Throws:
IOException- if there is a problem reading the file
-
updateMessageDigests
static void updateMessageDigests(Path path, Collection<MessageDigest> messageDigests) throws IOException - Throws:
IOException
-
addMessageDigestHashToManifest
private static void addMessageDigestHashToManifest(Path path, Map<Manifest, MessageDigest> manifestToMessageDigestMap) -
formatMessageDigest
-
createManifestToMessageDigestMap
public static Map<Manifest,MessageDigest> createManifestToMessageDigestMap(Collection<SupportedAlgorithm> algorithms) throws NoSuchAlgorithmException - Parameters:
algorithms- theSupportedAlgorithmthat you which to map toMessageDigest- Returns:
- mapping between
ManifestandMessageDigest - Throws:
NoSuchAlgorithmException- ifMessageDigestdoesn't support the algorithm
-