Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: remove blobId(MessageDigest, InputStream)
  • Loading branch information
ppkarwasz committed Apr 11, 2026
commit 9064aa23dce73af3c0eba956e5480e85192116de
47 changes: 0 additions & 47 deletions src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,26 +223,6 @@ public TreeIdBuilder addDirectory(final String name) {
return current;
}

/**
* Adds a file entry at the given path within this tree.
*
* <p>If {@code name} contains {@code '/'}, intermediate subdirectories are created automatically.</p>
*
* <p>The stream is eagerly drained.</p>
*
* <p>If the size of the stream is known in advance, consider using {@link #addFile(GitIdentifiers.FileMode, String, long, InputStream)}
* instead.</p>
*
* @param mode The file mode (e.g. {@link FileMode#REGULAR}).
* @param name The relative path of the entry in normalized form(may contain {@code '/'}).
* @param data The file content.
* @throws IOException If the stream cannot be read.
* @throws IllegalArgumentException If any path component is {@code "."} or {@code ".."}.
*/
public void addFile(final FileMode mode, final String name, final InputStream data) throws IOException {
addFile(mode, name, () -> blobId(messageDigest, readAllBytes(data)));
}

/**
* Adds a file entry at the given path within this tree, streaming content without buffering.
*
Expand Down Expand Up @@ -330,23 +310,6 @@ public static byte[] blobId(final MessageDigest messageDigest, final byte[] data
return DigestUtils.digest(messageDigest, data);
}

/**
* Reads through a stream and returns a generalized Git blob identifier.
*
* <p>The stream is drained and its contents are buffered to determine the size before hashing. To avoid
* buffering, use {@link #blobId(MessageDigest, long, InputStream)} when the size is known in advance.</p>
*
* <p>When the hash algorithm is SHA-1, the identifier is identical to Git blob identifier and SWHID contents identifier.</p>
*
* @param messageDigest The MessageDigest to use (for example SHA-1).
* @param data Stream to digest.
* @return A generalized Git blob identifier.
* @throws IOException On error reading the stream.
*/
public static byte[] blobId(final MessageDigest messageDigest, final InputStream data) throws IOException {
return blobId(messageDigest, readAllBytes(data));
}

/**
* Reads through a stream of known size and returns a generalized Git blob identifier, without buffering.
*
Expand Down Expand Up @@ -441,16 +404,6 @@ private static void populateFromPath(final TreeIdBuilder builder, final Path dir
}
}

private static byte[] readAllBytes(final InputStream in) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final byte[] buf = new byte[DigestUtils.BUFFER_SIZE];
int n;
while ((n = in.read(buf)) != -1) {
out.write(buf, 0, n);
}
return out.toByteArray();
}

/**
* Reads through a directory and returns a generalized Git tree identifier.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ void testBlobIdByteArray(final String resourceName, final String expectedSha1Hex
assertArrayEquals(Hex.decodeHex(expectedSha1Hex), GitIdentifiers.blobId(DigestUtils.getSha1Digest(), data));
}

@ParameterizedTest
@MethodSource("blobIdProvider")
void testBlobIdInputStream(final String resourceName, final String expectedSha1Hex) throws Exception {
final byte[] data = Files.readAllBytes(resourcePath(resourceName));
assertArrayEquals(Hex.decodeHex(expectedSha1Hex),
GitIdentifiers.blobId(DigestUtils.getSha1Digest(), new ByteArrayInputStream(data)));
}

@ParameterizedTest
@MethodSource("blobIdProvider")
void testBlobIdInputStreamWithSize(final String resourceName, final String expectedSha1Hex) throws Exception {
Expand Down Expand Up @@ -206,10 +198,6 @@ void testTreeIdBuilderAddFileInputStream() throws Exception {
byteArrayBuilder.addFile(GitIdentifiers.FileMode.REGULAR, "file.txt", content);
final byte[] expected = byteArrayBuilder.build();

final GitIdentifiers.TreeIdBuilder streamBuilder = GitIdentifiers.treeIdBuilder(md);
streamBuilder.addFile(GitIdentifiers.FileMode.REGULAR, "file.txt", new ByteArrayInputStream(content));
assertArrayEquals(expected, streamBuilder.build());

final GitIdentifiers.TreeIdBuilder sizedStreamBuilder = GitIdentifiers.treeIdBuilder(md);
sizedStreamBuilder.addFile(GitIdentifiers.FileMode.REGULAR, "file.txt", content.length, new ByteArrayInputStream(content));
assertArrayEquals(expected, sizedStreamBuilder.build());
Expand Down