You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by ma...@apache.org on 2023/02/03 12:32:19 UTC

[james-project] 05/05: Refactor text extraction in a more monadic style and avoid building unnecessary `InputStream`s

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

matthieu pushed a commit to branch refactorings-2
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 956db1063b3113eba08b533ba6fd92be26a3fda3
Author: Matthieu Baechler <ma...@baechler-craftsmanship.fr>
AuthorDate: Fri Feb 3 09:19:00 2023 +0100

    Refactor text extraction in a more monadic style and avoid building unnecessary `InputStream`s
---
 .../james/mailbox/opensearch/json/MimePart.java      | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/mailbox/opensearch/src/main/java/org/apache/james/mailbox/opensearch/json/MimePart.java b/mailbox/opensearch/src/main/java/org/apache/james/mailbox/opensearch/json/MimePart.java
index bce4c80cbe..be9c1a3fdc 100644
--- a/mailbox/opensearch/src/main/java/org/apache/james/mailbox/opensearch/json/MimePart.java
+++ b/mailbox/opensearch/src/main/java/org/apache/james/mailbox/opensearch/json/MimePart.java
@@ -196,15 +196,15 @@ public class MimePart {
         }
 
         private Mono<ParsedContent> extractText(TextExtractor textExtractor) {
-            if (bodyContent.isEmpty()) {
-                return Mono.empty();
-            }
-            if (shouldPerformTextExtraction()) {
-                return textExtractor.extractContentReactive(
-                    new ByteArrayInputStream(bodyContent.get()),
-                    contentType.orElse(null));
-            }
-            return Mono.fromCallable(() -> ParsedContent.of(IOUtils.toString(new ByteArrayInputStream(bodyContent.get()), charset.orElse(StandardCharsets.UTF_8))));
+            return Mono.justOrEmpty(bodyContent)
+                .flatMap(content -> {
+                    if (shouldPerformTextExtraction()) {
+                        return textExtractor.extractContentReactive(
+                                new ByteArrayInputStream(content),
+                                contentType.orElse(null));
+                    }
+                    return Mono.fromCallable(() -> ParsedContent.of(new String(content, charset.orElse(StandardCharsets.UTF_8))));
+                });
         }
 
         private boolean shouldPerformTextExtraction() {
@@ -220,7 +220,7 @@ public class MimePart {
         }
 
     }
-    
+
     public static Builder builder(Predicate<ContentType> shouldCaryOverContent) {
         return new Builder(shouldCaryOverContent);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org