You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2023/02/03 22:51:40 UTC

[maven-artifact-plugin] 04/04: [MARTIFACT-43] Replace DigestHelper with DigestUtils

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch version-next
in repository https://gitbox.apache.org/repos/asf/maven-artifact-plugin.git

commit 1cd0df6275f1f1a0cd5a7ef11cc6b12b9728731d
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Fri Feb 3 23:38:27 2023 +0100

    [MARTIFACT-43] Replace DigestHelper with DigestUtils
---
 .../artifact/buildinfo/BuildInfoWriter.java        | 12 ++++-
 .../plugins/artifact/buildinfo/DigestHelper.java   | 52 ----------------------
 2 files changed, 11 insertions(+), 53 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java
index 3be6008..5fe3348 100644
--- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java
+++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java
@@ -19,12 +19,16 @@
 package org.apache.maven.plugins.artifact.buildinfo;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintWriter;
+import java.nio.file.Files;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
+import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.DefaultArtifact;
 import org.apache.maven.artifact.handler.ArtifactHandler;
@@ -245,7 +249,13 @@ class BuildInfoWriter {
         p.println();
         p.println(prefix + ".filename=" + filename);
         p.println(prefix + ".length=" + file.length());
-        p.println(prefix + ".checksums.sha512=" + DigestHelper.calculateSha512(file));
+        try (InputStream is = Files.newInputStream(file.toPath())) {
+            p.println(prefix + ".checksums.sha512=" + DigestUtils.sha512Hex(is));
+        } catch (IOException ioe) {
+            throw new MojoExecutionException("Error processing file " + file, ioe);
+        } catch (IllegalArgumentException iae) {
+            throw new MojoExecutionException("Could not get hash algorithm", iae.getCause());
+        }
     }
 
     Map<Artifact, String> getArtifacts() {
diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/DigestHelper.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/DigestHelper.java
deleted file mode 100644
index 5f27ff8..0000000
--- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/DigestHelper.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.maven.plugins.artifact.buildinfo;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-
-import org.apache.commons.codec.binary.Hex;
-import org.apache.maven.plugin.MojoExecutionException;
-
-/**
- * Helper to calculate sha512.
- */
-class DigestHelper {
-    static String calculateSha512(File file) throws MojoExecutionException {
-        try (FileInputStream fis = new FileInputStream(file)) {
-            MessageDigest messageDigest = MessageDigest.getInstance("sha-512");
-
-            byte[] buffer = new byte[16 * 1024];
-            int size = fis.read(buffer, 0, buffer.length);
-            while (size >= 0) {
-                messageDigest.update(buffer, 0, size);
-                size = fis.read(buffer, 0, buffer.length);
-            }
-
-            return Hex.encodeHexString(messageDigest.digest());
-        } catch (IOException ioe) {
-            throw new MojoExecutionException("Error opening file " + file, ioe);
-        } catch (NoSuchAlgorithmException nsae) {
-            throw new MojoExecutionException("Could not get hash algorithm", nsae);
-        }
-    }
-}