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 <[email protected]>
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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to