19
19
20
20
package io .github .jwharm .flatpakgradlegenerator ;
21
21
22
- import org .gradle .api .artifacts .Configuration ;
23
22
import org .gradle .api .artifacts .ModuleVersionIdentifier ;
24
23
import org .gradle .api .artifacts .ResolvedArtifact ;
25
24
26
25
import java .io .BufferedInputStream ;
26
+ import java .io .ByteArrayInputStream ;
27
27
import java .io .ByteArrayOutputStream ;
28
+ import java .io .InputStream ;
28
29
import java .io .File ;
29
30
import java .io .IOException ;
30
31
import java .net .HttpURLConnection ;
@@ -71,17 +72,16 @@ static ArtifactResolver getInstance(String dest) {
71
72
*
72
73
* @throws NoSuchAlgorithmException no provider for the SHA-512 algorithm
73
74
*/
74
- Optional <byte []> tryResolve (DependencyDetails dep ,
75
- String repository ,
76
- String filename )
77
- throws NoSuchAlgorithmException {
75
+ Optional <byte []> tryResolve (DependencyDetails dep ,
76
+ String repository ,
77
+ String filename ) throws IOException , NoSuchAlgorithmException {
78
78
79
79
// Build the url and try to download the file
80
80
String url = repository + dep .path () + "/" + filename ;
81
81
var contents = getFileContentsFrom (url );
82
82
83
83
if (contents .isPresent ()) {
84
- String sha512 = calculateSHA512 (contents .get ());
84
+ String sha512 = calculateSHA512 (new ByteArrayInputStream ( contents .get () ));
85
85
String dest = dep .path ();
86
86
87
87
// If the filename contains a path, cut it out and append it to the dest field
@@ -111,7 +111,7 @@ Optional<byte[]> tryResolve(DependencyDetails dep,
111
111
* find the artifact in the local Gradle file, and use that file to calculate the SHA-512 hash
112
112
* and add it to the JSON output.
113
113
*
114
- * @param configuration the Gradle configuration that contains the artifact
114
+ * @param artifacts resolved artifacts for the gradle configuration containing the dependency
115
115
* @param id the id of the dependency
116
116
* @param dep a DependencyDetail instance with the Maven coordinates of the artifact
117
117
* @param repository the repository to try to download from
@@ -120,7 +120,7 @@ Optional<byte[]> tryResolve(DependencyDetails dep,
120
120
* @throws IOException error while reading the jar file
121
121
* @throws NoSuchAlgorithmException no provider for the SHA-512 algorithm
122
122
*/
123
- void tryResolveCached (Configuration configuration ,
123
+ void tryResolveCached (Set < ResolvedArtifact > artifacts ,
124
124
ModuleVersionIdentifier id ,
125
125
DependencyDetails dep ,
126
126
String repository ,
@@ -129,18 +129,13 @@ void tryResolveCached(Configuration configuration,
129
129
String altName )
130
130
throws IOException , NoSuchAlgorithmException {
131
131
132
- // Find the jar in the local Gradle cache
133
- Set <ResolvedArtifact > artifacts = configuration
134
- .getResolvedConfiguration ()
135
- .getResolvedArtifacts ();
136
-
137
132
for (var artifact : artifacts ) {
138
133
if (artifact .getModuleVersion ().getId ().equals (id )) {
139
134
File file = artifact .getFile ();
140
135
141
136
// Check against filenames in the .module file
142
137
if (checkName )
143
- if (! (file .getName ().equals (filename ) ||
138
+ if (!(file .getName ().equals (filename ) ||
144
139
file .getName ().equals (altName )))
145
140
continue ;
146
141
@@ -155,7 +150,7 @@ void tryResolveCached(Configuration configuration,
155
150
+ file .getName ();
156
151
var isValid = isValid (url );
157
152
158
- if ((! isValid ) && filename .contains ("SNAPSHOT" )) {
153
+ if ((!isValid ) && filename .contains ("SNAPSHOT" )) {
159
154
// Try again, but this time, replace SNAPSHOT with snapshot details
160
155
url = repository
161
156
+ dep .path ()
@@ -165,9 +160,12 @@ void tryResolveCached(Configuration configuration,
165
160
}
166
161
167
162
if (isValid ) {
163
+ String sha512 ;
164
+
168
165
// Read the file from the local Gradle cache and calculate the SHA-512 hash
169
- byte [] bytes = Files .readAllBytes (file .toPath ());
170
- String sha512 = calculateSHA512 (bytes );
166
+ try (var fileInputStream = Files .newInputStream (file .toPath ())) {
167
+ sha512 = calculateSHA512 (fileInputStream );
168
+ }
171
169
172
170
// Generate and append the json
173
171
generateJsonBlock (
@@ -280,16 +278,25 @@ private Optional<byte[]> getFileContents(String url) {
280
278
/**
281
279
* Generate an SHA-512 hash for a byte array using MessageDigest.
282
280
*
283
- * @param contents the input byte array
281
+ * @param contentsInputStream the input stream for contents to compute hash of
284
282
* @return the SHA-512 hash
285
283
* @throws NoSuchAlgorithmException no provider for the SHA-512 algorithm
284
+ * @throws IOException error when reading input
286
285
*/
287
- private String calculateSHA512 (byte [] contents ) throws NoSuchAlgorithmException {
286
+ private String calculateSHA512 (InputStream contentsInputStream ) throws NoSuchAlgorithmException , IOException {
287
+ byte [] buffer = new byte [4096 ];
288
+
288
289
// Create a MessageDigest object for SHA-512
289
290
MessageDigest md = MessageDigest .getInstance ("SHA-512" );
290
291
291
- // Update the MessageDigest with the bytes from the input String
292
- md .update (contents );
292
+ int numRead ;
293
+ do {
294
+ numRead = contentsInputStream .read (buffer );
295
+ if (numRead > 0 ) {
296
+ md .update (buffer , 0 , numRead );
297
+ }
298
+ } while (numRead != -1 );
299
+
293
300
byte [] hashBytes = md .digest ();
294
301
295
302
// Convert the byte array to a hexadecimal string
0 commit comments