Skip to content

Commit cec54aa

Browse files
committed
simpler hashing + use hex-string
1 parent 0f7e1f0 commit cec54aa

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/DeduplicatingResourceTransformer.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.github.jengelman.gradle.plugins.shadow.transformers
22

33
import java.io.File
4-
import java.nio.ByteBuffer
54
import java.security.MessageDigest
65
import javax.inject.Inject
6+
import org.apache.commons.io.input.MessageDigestInputStream
77
import org.apache.tools.zip.ZipOutputStream
88
import org.gradle.api.GradleException
99
import org.gradle.api.file.FileTreeElement
1010
import org.gradle.api.model.ObjectFactory
1111
import org.gradle.api.tasks.Internal
1212
import org.gradle.api.tasks.util.PatternSet
13+
import org.gradle.internal.impldep.org.apache.commons.codec.binary.Hex
1314

1415
/**
1516
* Transformer to include files with identical content only once in the shadow JAR.
@@ -61,11 +62,11 @@ public open class DeduplicatingResourceTransformer(
6162
public constructor(objectFactory: ObjectFactory) : this(objectFactory, PatternSet())
6263

6364
internal data class PathInfos(val failOnDuplicateContent: Boolean) {
64-
val filesPerHash: MutableMap<Long, MutableList<File>> = mutableMapOf()
65+
val filesPerHash: MutableMap<String, MutableList<File>> = mutableMapOf()
6566

6667
fun uniqueContentCount() = filesPerHash.size
6768

68-
fun addFile(hash: Long, file: File): Boolean {
69+
fun addFile(hash: String, file: File): Boolean {
6970
var filesForHash: MutableList<File>? = filesPerHash[hash]
7071
val new = filesForHash == null
7172
if (new) {
@@ -113,23 +114,23 @@ public open class DeduplicatingResourceTransformer(
113114
@Transient
114115
private var digest: MessageDigest? = null
115116

116-
internal fun hashForFile(file: File): Long {
117+
internal fun hashForFile(file: File): String {
117118
if (digest == null) {
118119
digest = MessageDigest.getInstance("SHA-256")
119120
}
120121
val d = digest!!
121122
try {
123+
// We could replace this block with `o.a.c.codec.digest.DigestUtils.digest(MessageDigest, File)`,
124+
// but adding a whole new dependency seemed a bit overkill.
125+
// Using org.apache.commons.io.input.MessageDigestInputStream doesn't give simpler code either.
122126
file.inputStream().use {
123127
val buffer = ByteArray(8192)
124-
while (true) {
125-
val rd = it.read(buffer)
126-
if (rd == -1) {
127-
break
128-
}
129-
d.update(buffer, 0, rd)
128+
var readBytes: Int
129+
while (it.read(buffer).also { r -> readBytes = r } != -1) {
130+
d.update(buffer, 0, readBytes)
130131
}
131132
}
132-
return ByteBuffer.wrap(d.digest()).getLong(0)
133+
return Hex.encodeHexString(d.digest(), true)
133134
} catch (e: Exception) {
134135
throw RuntimeException("Failed to read data or calculate hash for $file", e)
135136
}

src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/DeduplicatingResourceTransformerTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class DeduplicatingResourceTransformerTest : BaseTransformerTest<DeduplicatingRe
2525
lateinit var file2: File
2626
lateinit var file3: File
2727

28-
var hash1: Long = 0L
29-
var hash2: Long = 0L
30-
var hash3: Long = 0L
28+
var hash1: String = ""
29+
var hash2: String = ""
30+
var hash3: String = ""
3131

3232
@BeforeEach
3333
fun setupFiles() {

0 commit comments

Comments
 (0)