You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2022/11/04 07:09:19 UTC

[james-mime4j] branch master updated: MIME4J-321 SingleBody should return its size (#80)

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

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-mime4j.git


The following commit(s) were added to refs/heads/master by this push:
     new 8479bf66 MIME4J-321 SingleBody should return its size (#80)
8479bf66 is described below

commit 8479bf6650cf6e56f7446438c23cad2803fc08d2
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Fri Nov 4 14:09:14 2022 +0700

    MIME4J-321 SingleBody should return its size (#80)
    
    A quite common need is for instance to get the size off attachments.
    
    Today the DOM API offer no other ways than to read the inputstream of the Body Part and manually compute the size.
    
    However, implementation holding the raw data might already have this value pre-computed and this could be used to get the size very cheaply, without copies.
    
    I propose to add a default "copy" implementation, and provide more optimal implementations where possible.
---
 .../java/org/apache/james/mime4j/dom/SingleBody.java |  6 ++++++
 .../james/mime4j/message/BasicBodyFactory.java       | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/dom/src/main/java/org/apache/james/mime4j/dom/SingleBody.java b/dom/src/main/java/org/apache/james/mime4j/dom/SingleBody.java
index ea421114..a13ad267 100644
--- a/dom/src/main/java/org/apache/james/mime4j/dom/SingleBody.java
+++ b/dom/src/main/java/org/apache/james/mime4j/dom/SingleBody.java
@@ -23,6 +23,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.NullOutputStream;
 import org.apache.james.mime4j.util.ContentUtil;
 
 /**
@@ -82,6 +84,10 @@ public abstract class SingleBody implements Body {
         in.close();
     }
 
+    public long size() throws IOException {
+        return IOUtils.copyLarge(getInputStream(), NullOutputStream.NULL_OUTPUT_STREAM);
+    }
+
     /**
      * Returns a copy of this <code>SingleBody</code> (optional operation).
      * <p>
diff --git a/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java b/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
index 7470dc7b..30cee09d 100644
--- a/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
+++ b/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
@@ -226,6 +226,11 @@ public class BasicBodyFactory implements BodyFactory {
             out.write(content);
         }
 
+        @Override
+        public long size() {
+            return content.length;
+        }
+
         @Override
         public void dispose() {
         }
@@ -268,6 +273,11 @@ public class BasicBodyFactory implements BodyFactory {
             return this.content.getValue().toInputStream();
         }
 
+        @Override
+        public long size() {
+            return content.getValue().size();
+        }
+
         @Override
         public void writeTo(OutputStream out) throws IOException {
             content.getValue().writeTo(out);
@@ -304,6 +314,11 @@ public class BasicBodyFactory implements BodyFactory {
             out.write(content);
         }
 
+        @Override
+        public long size() {
+            return content.length;
+        }
+
         @Override
         public void dispose() {
         }
@@ -334,6 +349,11 @@ public class BasicBodyFactory implements BodyFactory {
             content.getValue().writeTo(out);
         }
 
+        @Override
+        public long size() {
+            return content.getValue().size();
+        }
+
         @Override
         public void dispose() {
             content.release();


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