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 c04df0b CAMEL-17229: optimisation to create files from a String (#6473) c04df0b is described below commit c04df0b626664f7fea233c1b2fe610911c8ba1a4 Author: klease <38634989+kle...@users.noreply.github.com> AuthorDate: Fri Dec 24 14:30:23 2021 +0100 CAMEL-17229: optimisation to create files from a String (#6473) * CAMEL-17229: optimisation to create files from a String This change reduces the risk of a file being consumed before being completely written which frequently happens during parallel unit tests. * Make changes requested for PR 6473 * Fix bug in previous commit --- .../org/apache/camel/component/file/FileOperations.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java index 5629672..4d6710e 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java @@ -381,6 +381,10 @@ public class FileOperations implements GenericFileOperations<File> { // buffer the reader in = IOHelper.buffered(in); writeFileByReaderWithCharset(in, file, charset); + } else if (exchange.getIn().getBody() instanceof String) { + // If the body is a string, write it directly + String stringBody = (String) exchange.getIn().getBody(); + writeFileByString(stringBody, file); } else { // fallback and use stream based InputStream in = exchange.getIn().getMandatoryBody(InputStream.class); @@ -500,6 +504,16 @@ public class FileOperations implements GenericFileOperations<File> { } } + private void writeFileByString(String body, File target) throws IOException { + boolean append = endpoint.getFileExist() == GenericFileExist.Append; + Files.writeString(target.toPath(), body, StandardOpenOption.WRITE, + append ? StandardOpenOption.APPEND : StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); + if (append && endpoint.getAppendChars() != null) { + Files.writeString(target.toPath(), endpoint.getAppendChars(), StandardOpenOption.WRITE, + StandardOpenOption.APPEND); + } + } + /** * Creates a new file if the file doesn't exist. If the endpoint's existing file logic is set to 'Override' then the * target file will be truncated