8000 Use FileChannel.map for efficient digest computation by jorsol · Pull Request #217 · oras-project/oras-java · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use FileChannel.map for efficient digest computation #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/main/java/land/oras/utils/DigestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
package land.oras.utils;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.Security;
import java.util.HexFormat;
import land.oras.exception.OrasException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jspecify.annotations.NullMarked;
Expand All @@ -36,6 +38,8 @@
@NullMarked
final class DigestUtils {

private static final HexFormat HEX_FORMAT = HexFormat.of();

static {
Security.addProvider(new BouncyCastleProvider());
}
Expand All @@ -55,19 +59,19 @@ private DigestUtils() {}
static String digest(String algorithm, String prefix, Path path) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
try (InputStream is = Files.newInputStream(path, StandardOpenOption.READ)) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
digest.update(buffer, 0, bytesRead);
try (var channel = FileChannel.open(path, StandardOpenOption.READ)) {
long fileSize = channel.size();
long position = 0;
while (position < fileSize) {
long remaining = fileSize - position;
int chunkSize = (int) Math.min(Integer.MAX_VALUE, remaining);
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, position, chunkSize);
digest.update(buffer);
position += chunkSize;
}
}
byte[] hashBytes = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
return "%s:%s".formatted(prefix, sb.toString());
return formatHex(prefix, hashBytes);
} catch (Exception e) {
throw new OrasException("Failed to calculate digest", e);
}
Expand All @@ -86,12 +90,7 @@ static String digest(String algorithm, String prefix, byte[] bytes) {
byte[] hashBytes = digest.digest(bytes);

// Convert the byte array to hex
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}

return "%s:%s".formatted(prefix, sb.toString());
return formatHex(prefix, hashBytes);
} catch (Exception e) {
throw new OrasException("Failed to calculate digest", e);
}
Expand All @@ -113,13 +112,14 @@ static String digest(String algorithm, String prefix, InputStream input) {
digest.update(buffer, 0, bytesRead);
}
byte[] hashBytes = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
return "%s:%s".formatted(prefix, sb.toString());
return formatHex(prefix, hashBytes);
} catch (Exception e) {
throw new OrasException("Failed to calculate digest", e);
}
}

private static String formatHex(String prefix, final byte[] hashBytes) {
String formatHex = HEX_FORMAT.formatHex(hashBytes);
return prefix + ":" + formatHex;
}
}
0