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

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a35da3ddd89a5dd77fdd56a4c9049b3f09d9dd59
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Feb 5 11:05:39 2020 +0100

    [CAMEL-14444] Build speed: Avoid overwriting the manifest if unchanged
---
 .../apache/felix/bundleplugin/ManifestPlugin.java  | 48 +++++++++++++++++++---
 1 file changed, 42 insertions(+), 6 deletions(-)

diff --git 
a/init/camel-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
 
b/init/camel-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
index 0e64afa..0fdb252 100644
--- 
a/init/camel-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
+++ 
b/init/camel-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
@@ -16,6 +16,7 @@
  */
 package org.apache.felix.bundleplugin;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -28,6 +29,8 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -417,19 +420,52 @@ public class ManifestPlugin extends BundlePlugin {
     }
 
     public static void writeManifest(Manifest manifest, File outputFile, 
boolean niceManifest, BuildContext buildContext, Log log) throws IOException {
-        log.debug("Write manifest to " + outputFile.getPath());
-        outputFile.getParentFile().mkdirs();
-
-        OutputStream os = buildContext.newFileOutputStream(outputFile);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
         try {
-            ManifestWriter.outputManifest(manifest, os, niceManifest);
+            ManifestWriter.outputManifest(manifest, baos, niceManifest);
         } finally {
             try {
-                os.close();
+                baos.close();
             } catch (IOException e) {
                 // nothing we can do here
             }
         }
+
+        log.debug("Write manifest to " + outputFile.getPath());
+        if (updateFile(outputFile.toPath(), baos.toByteArray())) {
+            buildContext.refresh(outputFile);
+        }
+    }
+
+    /**
+     * Update a file with the given binary content if neeed.
+     * The file won't be modified if the content is already the same.
+     *
+     * @param path the path of the file to update
+     * @param newdata the new binary data, <code>null</code> to delete the file
+     * @return <code>true</code> if the file was modified, <code>false</code> 
otherwise
+     * @throws IOException if an exception occurs
+     */
+    public static boolean updateFile(Path path, byte[] newdata) throws 
IOException {
+        if (newdata == null) {
+            if (!Files.exists(path)) {
+                return false;
+            }
+            Files.delete(path);
+            return true;
+        } else {
+            byte[] olddata = new byte[0];
+            if (Files.exists(path) && Files.isReadable(path)) {
+                olddata = Files.readAllBytes(path);
+            }
+            if (Arrays.equals(olddata, newdata)) {
+                return false;
+            }
+            Files.createDirectories(path.getParent());
+            Files.write(path, newdata, StandardOpenOption.WRITE,
+                    StandardOpenOption.CREATE, 
StandardOpenOption.TRUNCATE_EXISTING);
+            return true;
+        }
     }
 
     /*

Reply via email to