You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dk...@apache.org on 2017/07/26 21:33:27 UTC

[1/2] camel git commit: Start using Files.newXXputStream instead of "new FileXXputStream" to reduce finalizer contention

Repository: camel
Updated Branches:
  refs/heads/master 98d346612 -> d8c0a53b6


Start using Files.newXXputStream instead of "new FileXXputStream" to reduce finalizer contention


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

Branch: refs/heads/master
Commit: d8c0a53b6787778661fb4cde3c59a50b5691efac
Parents: 81de0f5
Author: Daniel Kulp <dk...@apache.org>
Authored: Wed Jul 26 17:29:23 2017 -0400
Committer: Daniel Kulp <dk...@apache.org>
Committed: Wed Jul 26 17:33:13 2017 -0400

----------------------------------------------------------------------
 .../camel/component/file/FileOperations.java    | 54 +++++++-------------
 .../converter/stream/FileInputStreamCache.java  | 28 ++--------
 .../java/org/apache/camel/util/FileUtil.java    | 24 ++-------
 3 files changed, 26 insertions(+), 80 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d8c0a53b/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java b/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
index ac6304e..2ee56fe 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
@@ -17,17 +17,17 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.RandomAccessFile;
 import java.io.Reader;
 import java.io.Writer;
 import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.charset.Charset;
 import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
 import java.nio.file.attribute.PosixFilePermission;
 import java.nio.file.attribute.PosixFilePermissions;
 import java.util.Date;
@@ -37,10 +37,10 @@ import java.util.Set;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.WrappedFile;
-import org.apache.camel.converter.IOConverter;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -157,7 +157,7 @@ public class FileOperations implements GenericFileOperations<File> {
             path = endpointPath;
         } else {
             // relative after the endpoint path
-            String afterRoot = ObjectHelper.after(directory, endpointPath.getPath() + File.separator);
+            String afterRoot = StringHelper.after(directory, endpointPath.getPath() + File.separator);
             if (ObjectHelper.isNotEmpty(afterRoot)) {
                 // dir is under the root path
                 path = new File(endpoint.getFile(), afterRoot);
@@ -430,26 +430,12 @@ public class FileOperations implements GenericFileOperations<File> {
     }
 
     private void writeFileByFile(File source, File target) throws IOException {
-        FileChannel in = new FileInputStream(source).getChannel();
-        FileChannel out = null;
-        try {
-            out = prepareOutputFileChannel(target);
-            LOG.debug("Using FileChannel to write file: {}", target);
-            long size = in.size();
-            long position = 0;
-            while (position < size) {
-                position += in.transferTo(position, endpoint.getBufferSize(), out);
-            }
-        } finally {
-            IOHelper.close(in, source.getName(), LOG);
-            IOHelper.close(out, target.getName(), LOG, endpoint.isForceWrites());
-        }
+        Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
     }
 
     private void writeFileByStream(InputStream in, File target) throws IOException {
-        FileChannel out = null;
-        try {
-            out = prepareOutputFileChannel(target);
+        try (SeekableByteChannel out = prepareOutputFileChannel(target)) {
+            
             LOG.debug("Using InputStream to write file: {}", target);
             int size = endpoint.getBufferSize();
             byte[] buffer = new byte[size];
@@ -464,21 +450,20 @@ public class FileOperations implements GenericFileOperations<File> {
             }
         } finally {
             IOHelper.close(in, target.getName(), LOG);
-            IOHelper.close(out, target.getName(), LOG, endpoint.isForceWrites());
         }
     }
 
     private void writeFileByReaderWithCharset(Reader in, File target, String charset) throws IOException {
         boolean append = endpoint.getFileExist() == GenericFileExist.Append;
-        FileOutputStream os = new FileOutputStream(target, append);
-        Writer out = IOConverter.toWriter(os, charset);
-        try {
+        try (Writer out = Files.newBufferedWriter(target.toPath(), Charset.forName(charset), 
+                                                  StandardOpenOption.WRITE,
+                                                  append ? StandardOpenOption.APPEND : StandardOpenOption.TRUNCATE_EXISTING, 
+                                                  StandardOpenOption.CREATE)) {
             LOG.debug("Using Reader to write file: {} with charset: {}", target, charset);
             int size = endpoint.getBufferSize();
             IOHelper.copy(in, out, size);
         } finally {
             IOHelper.close(in, target.getName(), LOG);
-            IOHelper.close(out, os, target.getName(), LOG, endpoint.isForceWrites());
         }
     }
 
@@ -492,11 +477,8 @@ public class FileOperations implements GenericFileOperations<File> {
             FileUtil.createNewFile(target);
         } else if (endpoint.getFileExist() == GenericFileExist.Override) {
             LOG.debug("Truncating existing file: {}", target);
-            FileChannel out = new FileOutputStream(target).getChannel();
-            try {
-                out.truncate(0);
-            } finally {
-                IOHelper.close(out, target.getName(), LOG, endpoint.isForceWrites());
+            try (SeekableByteChannel out = Files.newByteChannel(target.toPath(), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
+                //nothing to write
             }
         }
     }
@@ -505,11 +487,11 @@ public class FileOperations implements GenericFileOperations<File> {
      * Creates and prepares the output file channel. Will position itself in correct position if the file is writable
      * eg. it should append or override any existing content.
      */
-    private FileChannel prepareOutputFileChannel(File target) throws IOException {
+    private SeekableByteChannel prepareOutputFileChannel(File target) throws IOException {
         if (endpoint.getFileExist() == GenericFileExist.Append) {
-            FileChannel out = new RandomAccessFile(target, "rw").getChannel();
+            SeekableByteChannel out = Files.newByteChannel(target.toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
             return out.position(out.size());
         }
-        return new FileOutputStream(target).getChannel();
+        return Files.newByteChannel(target.toPath(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/d8c0a53b/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java b/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
index a0d6501..acea1c9 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
@@ -19,15 +19,12 @@ package org.apache.camel.converter.stream;
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.nio.channels.Channels;
-import java.nio.channels.FileChannel;
-import java.nio.channels.WritableByteChannel;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
 import java.security.GeneralSecurityException;
 import java.util.ArrayList;
 import java.util.List;
@@ -94,22 +91,7 @@ public final class FileInputStreamCache extends InputStream implements StreamCac
 
     public void writeTo(OutputStream os) throws IOException {
         if (stream == null && ciphers == null) {
-            FileInputStream s = new FileInputStream(file);
-            long len = file.length();
-            WritableByteChannel out;
-            if (os instanceof WritableByteChannel) {
-                out = (WritableByteChannel)os;
-            } else {
-                out = Channels.newChannel(os);
-            }
-            FileChannel fc = s.getChannel();
-            long pos = 0;
-            while (pos < len) {
-                long i = fc.transferTo(pos, len - pos, out);
-                pos += i;
-            }
-            s.close();
-            fc.close();
+            Files.copy(file.toPath(), os);
         } else {
             IOHelper.copy(getInputStream(), os);
         }
@@ -147,7 +129,7 @@ public final class FileInputStreamCache extends InputStream implements StreamCac
     }
 
     private InputStream createInputStream(File file) throws IOException {
-        InputStream in = new BufferedInputStream(new FileInputStream(file));
+        InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath(), StandardOpenOption.READ));
         if (ciphers != null) {
             in = new CipherInputStream(in, ciphers.getDecryptor()) {
                 boolean closed;
@@ -258,7 +240,7 @@ public final class FileInputStreamCache extends InputStream implements StreamCac
             tempFile = FileUtil.createTempFile("cos", ".tmp", strategy.getSpoolDirectory());
 
             LOG.trace("Creating temporary stream cache file: {}", tempFile);
-            OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
+            OutputStream out = new BufferedOutputStream(Files.newOutputStream(tempFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE));
             if (ObjectHelper.isNotEmpty(strategy.getSpoolChiper())) {
                 try {
                     if (ciphers == null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/d8c0a53b/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/FileUtil.java b/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
index f60bab4..d1782e0 100644
--- a/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
+++ b/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
@@ -17,10 +17,9 @@
 package org.apache.camel.util;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
 import java.util.ArrayDeque;
 import java.util.Deque;
 import java.util.Iterator;
@@ -532,24 +531,7 @@ public final class FileUtil {
      * @throws IOException If an I/O error occurs during copy operation
      */
     public static void copyFile(File from, File to) throws IOException {
-        FileChannel in = null;
-        FileChannel out = null;
-        try {
-            in = new FileInputStream(from).getChannel();
-            out = new FileOutputStream(to).getChannel();
-            if (LOG.isTraceEnabled()) {
-                LOG.trace("Using FileChannel to copy from: " + in + " to: " + out);
-            }
-
-            long size = in.size();
-            long position = 0;
-            while (position < size) {
-                position += in.transferTo(position, BUFFER_SIZE, out);
-            }
-        } finally {
-            IOHelper.close(in, from.getName(), LOG);
-            IOHelper.close(out, to.getName(), LOG);
-        }
+        Files.copy(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING);
     }
 
     /**


[2/2] camel git commit: Put jackson-core in test scope as well as jackson-databinding has classes that depend on it and not having it makes the eclipse compiler complain and not compile anything.

Posted by dk...@apache.org.
Put jackson-core in test scope as well as jackson-databinding has classes that depend on it and not having it makes the eclipse compiler complain and not compile anything.


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

Branch: refs/heads/master
Commit: 81de0f5808b0c8877d762481150a494fe26b8d32
Parents: 98d3466
Author: Daniel Kulp <dk...@apache.org>
Authored: Wed Jul 26 16:00:28 2017 -0400
Committer: Daniel Kulp <dk...@apache.org>
Committed: Wed Jul 26 17:33:13 2017 -0400

----------------------------------------------------------------------
 camel-core/pom.xml | 5 +++++
 1 file changed, 5 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/81de0f58/camel-core/pom.xml
----------------------------------------------------------------------
diff --git a/camel-core/pom.xml b/camel-core/pom.xml
index 598e216..6726527 100644
--- a/camel-core/pom.xml
+++ b/camel-core/pom.xml
@@ -195,6 +195,11 @@
       <artifactId>jackson-databind</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+        <groupId>com.fasterxml.jackson.core</groupId>
+        <artifactId>jackson-core</artifactId>
+        <scope>test</scope>
+    </dependency>
 
     <!-- validator -->
     <dependency>