karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject [1/3] karaf git commit: Use java.util.file.Files.newInputStream()
Date Wed, 23 Aug 2017 14:49:49 GMT
Repository: karaf
Updated Branches:
  refs/heads/master 49e484437 -> edcda0b0e


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 <nite@hq.sk>


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

Branch: refs/heads/master
Commit: ed797960d2a43eee8c04a9572478320af2420cfb
Parents: 49e4844
Author: Robert Varga <nite@hq.sk>
Authored: Sun Aug 13 18:56:29 2017 +0200
Committer: Robert Varga <nite@hq.sk>
Committed: Wed Aug 23 14:22:20 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/ed797960/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 1b84004..a6141ba 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.Comparator;
@@ -601,7 +601,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())) {
@@ -611,7 +611,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());
         }
@@ -642,30 +642,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);
     }
 
@@ -866,7 +864,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);
@@ -876,7 +874,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/ed797960/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 ea79b40..a11c4e2 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.List;
@@ -350,11 +351,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) {


Mime
View raw message