|
45 | 45 | import org.apache.maven.plugin.logging.Log; |
46 | 46 | import org.apache.maven.project.MavenProject; |
47 | 47 | import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 48 | +import org.eclipse.aether.util.ChecksumUtils; |
48 | 49 |
|
49 | 50 | public class Bundler { |
50 | 51 |
|
@@ -200,43 +201,24 @@ private void addToZip(File file, String prefix, ZipOutputStream zipOut) throws I |
200 | 201 |
|
201 | 202 | private void generateChecksumsAndAddToZip(File sourceFile, String prefix, ZipOutputStream zipOut) |
202 | 203 | throws NoSuchAlgorithmException, IOException { |
203 | | - for (String algo : CHECKSUM_ALGOS) { |
204 | | - File checksumFile = generateChecksum(sourceFile, algo); |
| 204 | + for (File checksumFile : generateChecksums(sourceFile, CHECKSUM_ALGOS)) { |
205 | 205 | addToZip(checksumFile, prefix, zipOut); |
206 | 206 | } |
207 | 207 | } |
208 | 208 |
|
209 | | - public File generateChecksum(File file, String algo) throws NoSuchAlgorithmException, IOException { |
210 | | - String extension = algo.toLowerCase().replace("-", ""); |
211 | | - File checksumFile = new File(file.getAbsolutePath() + "." + extension); |
212 | | - // It might have been generated externally. In that case, use that. |
213 | | - if (checksumFile.exists()) { |
214 | | - return checksumFile; |
215 | | - } |
216 | | - |
217 | | - // Create the checksum file |
218 | | - MessageDigest digest = MessageDigest.getInstance(algo); |
219 | | - try (InputStream is = Files.newInputStream(file.toPath()); |
220 | | - OutputStream nullOut = new OutputStream() { |
221 | | - @Override |
222 | | - public void write(int b) {} |
223 | | - }; |
224 | | - DigestOutputStream dos = new DigestOutputStream(nullOut, digest)) { |
225 | | - |
226 | | - byte[] buffer = new byte[8192]; |
227 | | - int bytesRead; |
228 | | - while ((bytesRead = is.read(buffer)) != -1) { |
229 | | - dos.write(buffer, 0, bytesRead); |
| 209 | + public List<File> generateChecksums(File file, List<String> algos) throws IOException { |
| 210 | + Map<String, Object> results = ChecksumUtils.calc(file, algos); |
| 211 | + List<File> checkSumFiles = new ArrayList<>(); |
| 212 | + for (Map.Entry<String, Object> entry : results.entrySet()) { |
| 213 | + String extension = entry.getKey().toLowerCase().replace("-", ""); |
| 214 | + File checksumFile = new File(file.getAbsolutePath() + "." + extension); |
| 215 | + log.info("generateChecksums: " + extension); |
| 216 | + if (!checksumFile.exists()) { |
| 217 | + Files.write(checksumFile.toPath(), entry.getValue().toString().getBytes(StandardCharsets.UTF_8)); |
230 | 218 | } |
| 219 | + checkSumFiles.add(checksumFile); |
231 | 220 | } |
232 | | - |
233 | | - StringBuilder sb = new StringBuilder(); |
234 | | - for (byte b : digest.digest()) { |
235 | | - sb.append(String.format("%02x", b)); |
236 | | - } |
237 | | - |
238 | | - Files.write(checksumFile.toPath(), sb.toString().getBytes(StandardCharsets.UTF_8)); |
239 | | - return checksumFile; |
| 221 | + return checkSumFiles; |
240 | 222 | } |
241 | 223 |
|
242 | 224 | Model readPomFile(File pomFile) throws MojoExecutionException { |
|
0 commit comments