You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/07/25 12:08:45 UTC

[camel] branch main updated: [CAMEL-18285] Lazy-loading for file length and last modified attributes (#8019)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new e4671271949 [CAMEL-18285] Lazy-loading for file length and last modified attributes (#8019)
e4671271949 is described below

commit e4671271949d7d58607497e97b54ba0d28a523bf
Author: Radovan Netuka <rn...@redhat.com>
AuthorDate: Mon Jul 25 14:08:40 2022 +0200

    [CAMEL-18285] Lazy-loading for file length and last modified attributes (#8019)
    
    Co-authored-by: Radovan Netuka <rn...@localhost.localdomain>
---
 .../apache/camel/component/file/FileConsumer.java   |  6 ++++--
 .../apache/camel/component/file/GenericFile.java    | 21 +++++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
index 2ed9dfd9d44..4b1b8f4db38 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
@@ -250,7 +250,6 @@ public class FileConsumer extends GenericFileConsumer<File> implements ResumeAwa
         answer.setEndpointPath(endpointPath);
         answer.setFile(file);
         answer.setFileNameOnly(file.getName());
-        answer.setFileLength(file.length());
         answer.setDirectory(file.isDirectory());
         // must use FileUtil.isAbsolute to have consistent check for whether the
         // file is
@@ -263,7 +262,10 @@ public class FileConsumer extends GenericFileConsumer<File> implements ResumeAwa
         // to return a consistent answer for all OS platforms.
         answer.setAbsolute(FileUtil.isAbsolute(file));
         answer.setAbsoluteFilePath(file.getAbsolutePath());
-        answer.setLastModified(file.lastModified());
+
+        // file length and last modified are loaded lazily
+        answer.setFileLengthSupplier(file::length);
+        answer.setLastModifiedSupplier(file::lastModified);
 
         // compute the file path as relative to the starting directory
         File path;
diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFile.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFile.java
index 5518c321f9f..19b85360b28 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFile.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFile.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Map;
+import java.util.function.LongSupplier;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.WrappedFile;
@@ -44,7 +45,11 @@ public class GenericFile<T> implements WrappedFile<T> {
     private String relativeFilePath;
     private String absoluteFilePath;
     private long fileLength;
+    // file length in case of lazy-loading
+    private LongSupplier fileLengthSupplier;
     private long lastModified;
+    // last modified in case of lazy-loading
+    private LongSupplier lastModifiedSupplier;
     private long lastOffsetValue;
     private T file;
     private GenericFileBinding<T> binding;
@@ -286,6 +291,10 @@ public class GenericFile<T> implements WrappedFile<T> {
     }
 
     public long getFileLength() {
+        if (fileLength == 0 && fileLengthSupplier != null) {
+            // file length not set, try lazy-loading
+            fileLength = fileLengthSupplier.getAsLong();
+        }
         return fileLength;
     }
 
@@ -293,7 +302,15 @@ public class GenericFile<T> implements WrappedFile<T> {
         this.fileLength = fileLength;
     }
 
+    void setFileLengthSupplier(LongSupplier supplier) {
+        fileLengthSupplier = supplier;
+    }
+
     public long getLastModified() {
+        if (lastModified == 0 && lastModifiedSupplier != null) {
+            // last modified not set, try lazy-loading
+            lastModified = lastModifiedSupplier.getAsLong();
+        }
         return lastModified;
     }
 
@@ -301,6 +318,10 @@ public class GenericFile<T> implements WrappedFile<T> {
         this.lastModified = lastModified;
     }
 
+    void setLastModifiedSupplier(LongSupplier supplier) {
+        lastModifiedSupplier = supplier;
+    }
+
     public String getCharset() {
         return charset;
     }