You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2017/08/23 15:20:01 UTC

[3/3] karaf git commit: Use java.util.file.Files.newInputStream()

Use java.util.file.Files.newInputStream()

Instead of wiring to FileInputStream, which is hard to GC due to
the presence of finalize(), use Files.newInputStream.

Brings down feature generation time in OpenDaylight, before:
real    2m5.828s
user    2m14.886s
sys     0m20.849s

after:
real    1m46.523s
user    1m59.258s
sys     0m17.048s

Signed-off-by: Robert Varga <ni...@hq.sk>


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/e2415b8a
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/e2415b8a
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/e2415b8a

Branch: refs/heads/karaf-4.1.x
Commit: e2415b8af82db7b223054e7814eaefb733a64aeb
Parents: 74bbcc4
Author: Robert Varga <ni...@hq.sk>
Authored: Sun Aug 13 18:56:29 2017 +0200
Committer: Christian Schneider <ch...@die-schneider.net>
Committed: Wed Aug 23 17:19:52 2017 +0200

----------------------------------------------------------------------
 .../features/GenerateDescriptorMojo.java        | 36 +++++++++-----------
 .../apache/karaf/tooling/utils/MojoSupport.java | 12 +++----
 2 files changed, 22 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/e2415b8a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
index c6dc9fc..222c51f 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
@@ -23,7 +23,6 @@ import static org.apache.karaf.deployer.kar.KarArtifactInstaller.FEATURE_CLASSIF
 import java.io.BufferedInputStream;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -31,6 +30,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.StringWriter;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -605,7 +605,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
         }
     }
 
-    private Dependency findMatchingDependency(List<Dependency> dependencies, Dependency reference) {
+    private static Dependency findMatchingDependency(List<Dependency> dependencies, Dependency reference) {
         String referenceName = reference.getName();
         for (Dependency dependency : dependencies) {
             if (referenceName.equals(dependency.getName())) {
@@ -615,7 +615,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
         return null;
     }
 
-    private void mergeDependencies(Dependency target, Dependency source) {
+    private static void mergeDependencies(Dependency target, Dependency source) {
         if (target.getVersion() == null || Feature.DEFAULT_VERSION.equals(target.getVersion())) {
             target.setVersion(source.getVersion());
         }
@@ -646,30 +646,28 @@ public class GenerateDescriptorMojo extends MojoSupport {
      */
 
     private Manifest getManifest(File file) throws IOException {
-        InputStream is;
+        final InputStream is;
         try {
-            is = new BufferedInputStream(new FileInputStream(file));
+            is = Files.newInputStream(file.toPath());
         } catch (Exception e) {
             getLog().warn("Error while opening artifact", e);
             return null;
         }
 
-        try {
-            is.mark(256 * 1024);
-            JarInputStream jar = new JarInputStream(is);
-            Manifest m = jar.getManifest();
-            if (m == null) {
-                getLog().warn("Manifest not present in the first entry of the zip - " + file.getName());
+        try (BufferedInputStream bis = new BufferedInputStream(is)) {
+            bis.mark(256 * 1024);
+
+            try (JarInputStream jar = new JarInputStream(bis)) {
+                Manifest m = jar.getManifest();
+                if (m == null) {
+                    getLog().warn("Manifest not present in the first entry of the zip - " + file.getName());
+                }
+                return m;
             }
-            jar.close();
-            return m;
-        } finally {
-            // just in case when we did not open bundle
-            is.close();
         }
     }
 
-    private Features readFeaturesFile(File featuresFile) throws XMLStreamException, JAXBException, IOException {
+    private static Features readFeaturesFile(File featuresFile) throws XMLStreamException, JAXBException, IOException {
         return JaxbUtil.unmarshal(featuresFile.toURI().toASCIIString(), false);
     }
 
@@ -879,7 +877,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
         }
     }
 
-    private Features toFeatures(Collection<Bundle> addedBundles, Collection<Dependency> addedDependencys, ObjectFactory objectFactory) {
+    private static Features toFeatures(Collection<Bundle> addedBundles, Collection<Dependency> addedDependencys, ObjectFactory objectFactory) {
         Features features = objectFactory.createFeaturesRoot();
         Feature feature = objectFactory.createFeature();
         feature.getBundle().addAll(addedBundles);
@@ -889,7 +887,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
     }
 
 
-    private void writeDependencies(Features features, File file) throws JAXBException, IOException {
+    private static void writeDependencies(Features features, File file) throws JAXBException, IOException {
         file.getParentFile().mkdirs();
         if (!file.getParentFile().exists() || !file.getParentFile().isDirectory()) {
             throw new IOException("Cannot create directory at " + file.getParent());

http://git-wip-us.apache.org/repos/asf/karaf/blob/e2415b8a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
index 71134bb..168e7cb 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
@@ -18,11 +18,12 @@
 package org.apache.karaf.tooling.utils;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.nio.file.Files;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -354,11 +355,8 @@ public abstract class MojoSupport extends AbstractMojo {
         File targetDir = destFile.getParentFile();
         ensureDirExists(targetDir);
 
-        try {
-            try (
-                FileInputStream is = new FileInputStream(sourceFile);
-                FileOutputStream bos = new FileOutputStream(destFile)
-            ) {
+        try (InputStream is = Files.newInputStream(sourceFile.toPath())) {
+            try (OutputStream bos = Files.newOutputStream(destFile.toPath())) {
                 StreamUtils.copy(is, bos);
             }
         } catch (IOException e) {