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-project.git

commit 06d3b79ec3fc9ccc2009923d03bf53278e88de06
Author: Benoit Tellier <btell...@linagora.com>
AuthorDate: Fri Jan 17 16:05:14 2020 +0700

    JAMES-2997 step #11 Remove bytes from attachment
---
 .../org/apache/james/mailbox/model/Attachment.java | 42 ++++++---------------
 .../james/mailbox/model/ParsedAttachment.java      | 44 ++++++++++++++--------
 .../james/mailbox/model/MessageAttachmentTest.java | 24 ++++++------
 .../cassandra/mail/CassandraAttachmentDAOV2.java   |  4 +-
 .../cassandra/mail/CassandraAttachmentMapper.java  | 10 ++---
 .../cassandra/mail/AttachmentLoaderTest.java       | 10 ++---
 .../mail/CassandraAttachmentDAOV2Test.java         |  3 +-
 ...asticSearchListeningMessageSearchIndexTest.java |  2 +-
 .../model/openjpa/AbstractJPAMailboxMessage.java   |  6 ++-
 .../mailbox/maildir/mail/model/MaildirMessage.java |  6 ++-
 .../inmemory/mail/InMemoryAttachmentMapper.java    | 28 +++++++-------
 .../james/vault/DeletedMessageConverterTest.java   |  3 +-
 .../james/mailbox/store/StoreMessageManager.java   | 14 ++-----
 .../mailbox/store/StoreAttachmentManagerTest.java  |  6 +--
 .../mail/model/impl/SimpleMailboxMessageTest.java  |  2 +-
 .../apache/james/util/io/InputStreamConsummer.java | 32 ++++++++++++++++
 .../message/view/MessageFullViewFactoryTest.java   |  2 +-
 17 files changed, 127 insertions(+), 111 deletions(-)

diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java 
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java
index eb017c9..934d044 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java
@@ -19,15 +19,12 @@
 
 package org.apache.james.mailbox.model;
 
-import java.util.Arrays;
-
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 
 public class Attachment {
-
     public static Builder builder() {
         return new Builder();
     }
@@ -35,7 +32,7 @@ public class Attachment {
     public static class Builder {
 
         private AttachmentId attachmentId;
-        private byte[] bytes;
+        private Long size;
         private String type;
 
         public Builder attachmentId(AttachmentId attachmentId) {
@@ -44,24 +41,24 @@ public class Attachment {
             return this;
         }
 
-        public Builder bytes(byte[] bytes) {
-            Preconditions.checkArgument(bytes != null);
-            this.bytes = bytes;
-            return this;
-        }
-
         public Builder type(String type) {
             Preconditions.checkArgument(!Strings.isNullOrEmpty(type));
             this.type = type;
             return this;
         }
 
+        public Builder size(long size) {
+            Preconditions.checkArgument(size >= 0, "'size' must be positive");
+            this.size = size;
+            return this;
+        }
+
         public Attachment build() {
-            Preconditions.checkState(bytes != null, "'bytes' is mandatory");
             Preconditions.checkState(type != null, "'type' is mandatory");
+            Preconditions.checkState(size != null, "'size' is mandatory");
             AttachmentId builtAttachmentId = attachmentId();
             Preconditions.checkState(builtAttachmentId != null, 
"'attachmentId' is mandatory");
-            return new Attachment(bytes, builtAttachmentId, type, size());
+            return new Attachment(builtAttachmentId, type, size);
         }
 
         private AttachmentId attachmentId() {
@@ -70,19 +67,13 @@ public class Attachment {
             }
             return AttachmentId.random();
         }
-
-        private long size() {
-            return bytes.length;
-        }
     }
 
-    private final byte[] bytes;
     private final AttachmentId attachmentId;
     private final String type;
     private final long size;
 
-    private Attachment(byte[] bytes, AttachmentId attachmentId, String type, 
long size) {
-        this.bytes = bytes;
+    private Attachment(AttachmentId attachmentId, String type, long size) {
         this.attachmentId = attachmentId;
         this.type = type;
         this.size = size;
@@ -100,22 +91,12 @@ public class Attachment {
         return size;
     }
 
-    /**
-     * Be careful the returned array is not a copy of the attachment byte 
array.
-     * Mutating it will mutate the attachment!
-     * @return the attachment content
-     */
-    public byte[] getBytes() {
-        return bytes;
-    }
-
 
     @Override
     public boolean equals(Object obj) {
         if (obj instanceof Attachment) {
             Attachment other = (Attachment) obj;
             return Objects.equal(attachmentId, other.attachmentId)
-                && Arrays.equals(bytes, other.bytes)
                 && Objects.equal(type, other.type)
                 && Objects.equal(size, other.size);
         }
@@ -124,7 +105,7 @@ public class Attachment {
 
     @Override
     public int hashCode() {
-        return Objects.hashCode(attachmentId, bytes, type, size);
+        return Objects.hashCode(attachmentId, type, size);
     }
 
     @Override
@@ -132,7 +113,6 @@ public class Attachment {
         return MoreObjects
                 .toStringHelper(this)
                 .add("attachmentId", attachmentId)
-                .add("bytes", bytes)
                 .add("type", type)
                 .add("size", size)
                 .toString();
diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/model/ParsedAttachment.java
 
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/ParsedAttachment.java
index 649b034..96dd66d 100644
--- 
a/mailbox/api/src/main/java/org/apache/james/mailbox/model/ParsedAttachment.java
+++ 
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/ParsedAttachment.java
@@ -23,7 +23,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Optional;
 
-import org.apache.commons.io.IOUtils;
+import org.apache.james.util.io.InputStreamConsummer;
+import org.apache.james.util.io.SizeInputStream;
 
 public class ParsedAttachment {
     interface Builder {
@@ -111,20 +112,31 @@ public class ParsedAttachment {
         return isInline;
     }
 
-    public MessageAttachment asMessageAttachment(AttachmentId attachmentId) {
-        try {
-            return MessageAttachment.builder()
-                .attachment(Attachment.builder()
-                        .attachmentId(attachmentId)
-                        .type(contentType)
-                        .bytes(IOUtils.toByteArray(content))
-                        .build())
-                    .name(name)
-                    .cid(cid)
-                    .isInline(isInline)
-                    .build();
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
+    public MessageAttachment asMessageAttachment(AttachmentId attachmentId, 
long size) {
+        return MessageAttachment.builder()
+            .attachment(Attachment.builder()
+                .attachmentId(attachmentId)
+                .type(contentType)
+                .size(size)
+                .build())
+            .name(name)
+            .cid(cid)
+            .isInline(isInline)
+            .build();
+    }
+
+    public MessageAttachment asMessageAttachment(AttachmentId attachmentId) 
throws IOException {
+        SizeInputStream sizeInputStream = new SizeInputStream(content);
+        InputStreamConsummer.consume(sizeInputStream);
+        return MessageAttachment.builder()
+            .attachment(Attachment.builder()
+                .attachmentId(attachmentId)
+                .type(contentType)
+                .size(sizeInputStream.getSize())
+                .build())
+            .name(name)
+            .cid(cid)
+            .isInline(isInline)
+            .build();
     }
 }
diff --git 
a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MessageAttachmentTest.java
 
b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MessageAttachmentTest.java
index edbc673..d501c30 100644
--- 
a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MessageAttachmentTest.java
+++ 
b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MessageAttachmentTest.java
@@ -45,9 +45,9 @@ class MessageAttachmentTest {
     @Test
     void buildShouldWorkWhenMandatoryAttributesAreGiven() {
         Attachment attachment = Attachment.builder()
-                .bytes("content".getBytes())
-                .type("type")
-                .build();
+            .size(36)
+            .type("type")
+            .build();
         MessageAttachment expectedMessageAttachment = new 
MessageAttachment(attachment, Optional.empty(), Optional.empty(), false);
 
         MessageAttachment messageAttachment = MessageAttachment.builder()
@@ -60,9 +60,9 @@ class MessageAttachmentTest {
     @Test
     void buildShouldAcceptIsInlineAndNoCid() {
         Attachment attachment = Attachment.builder()
-                .bytes("content".getBytes())
-                .type("type")
-                .build();
+            .size(36)
+            .type("type")
+            .build();
 
         MessageAttachment messageAttachment = MessageAttachment.builder()
             .attachment(attachment)
@@ -75,9 +75,9 @@ class MessageAttachmentTest {
     @Test
     void buildShouldSetAttributesWhenAllAreGiven() {
         Attachment attachment = Attachment.builder()
-                .bytes("content".getBytes())
-                .type("type")
-                .build();
+            .size(36)
+            .type("type")
+            .build();
         MessageAttachment expectedMessageAttachment = new 
MessageAttachment(attachment, Optional.of("name"), 
Optional.of(Cid.from("cid")), true);
 
         MessageAttachment messageAttachment = MessageAttachment.builder()
@@ -93,7 +93,7 @@ class MessageAttachmentTest {
     @Test
     void isInlinedWithCidShouldReturnTrueWhenIsInlineAndHasCid() throws 
Exception {
         Attachment attachment = Attachment.builder()
-            .bytes("content".getBytes())
+            .size(36)
             .type("type")
             .build();
 
@@ -110,7 +110,7 @@ class MessageAttachmentTest {
     @Test
     void isInlinedWithCidShouldReturnFalseWhenIsNotInline() throws Exception {
         Attachment attachment = Attachment.builder()
-            .bytes("content".getBytes())
+            .size(36)
             .type("type")
             .build();
 
@@ -127,7 +127,7 @@ class MessageAttachmentTest {
     @Test
     void isInlinedWithCidShouldReturnFalseWhenIsInlineButNoCid() throws 
Exception {
         Attachment attachment = Attachment.builder()
-            .bytes("content".getBytes())
+            .size(36)
             .type("type")
             .build();
 
diff --git 
a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentDAOV2.java
 
b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentDAOV2.java
index d846f91..226b40a 100644
--- 
a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentDAOV2.java
+++ 
b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentDAOV2.java
@@ -78,11 +78,11 @@ public class CassandraAttachmentDAOV2 {
             return size;
         }
 
-        public Attachment toAttachment(byte[] data) {
+        public Attachment toAttachment() {
             return Attachment.builder()
                 .attachmentId(attachmentId)
                 .type(type)
-                .bytes(data)
+                .size(size)
                 .build();
         }
 
diff --git 
a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
 
b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
index dced0a0..7cd1484 100644
--- 
a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
+++ 
b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
@@ -83,11 +83,6 @@ public class CassandraAttachmentMapper implements 
AttachmentMapper {
             .orElseThrow(() -> new 
AttachmentNotFoundException(attachmentId.getId()));
     }
 
-    private Mono<Attachment> retrievePayload(DAOAttachment daoAttachment) {
-        return Mono.from(blobStore.readBytes(blobStore.getDefaultBucketName(), 
daoAttachment.getBlobId()))
-            .map(daoAttachment::toAttachment);
-    }
-
     @Override
     public List<Attachment> getAttachments(Collection<AttachmentId> 
attachmentIds) {
         Preconditions.checkArgument(attachmentIds != null);
@@ -112,7 +107,7 @@ public class CassandraAttachmentMapper implements 
AttachmentMapper {
 
     private Mono<Attachment> getAttachmentInternal(AttachmentId id) {
         return attachmentDAOV2.getAttachment(id)
-            .flatMap(this::retrievePayload);
+            .map(DAOAttachment::toAttachment);
     }
 
     @Override
@@ -127,6 +122,7 @@ public class CassandraAttachmentMapper implements 
AttachmentMapper {
             .map(any -> Attachment.builder()
                 .attachmentId(attachmentId)
                 .type(contentType)
+                .size(sizeInputStream.getSize())
                 .build());
     }
 
@@ -156,7 +152,7 @@ public class CassandraAttachmentMapper implements 
AttachmentMapper {
         return Mono.from(blobStore.save(blobStore.getDefaultBucketName(), 
content, LOW_COST))
             .map(blobId -> new DAOAttachment(attachmentId, blobId, 
parsedAttachment.getContentType(), content.getSize()))
             .flatMap(daoAttachment -> storeAttachmentWithIndex(daoAttachment, 
ownerMessageId))
-            
.then(Mono.just(parsedAttachment.asMessageAttachment(attachmentId)));
+            .then(Mono.defer(() -> 
Mono.just(parsedAttachment.asMessageAttachment(attachmentId, 
content.getSize()))));
     }
 
     private Mono<Void> storeAttachmentWithIndex(DAOAttachment daoAttachment, 
MessageId ownerMessageId) {
diff --git 
a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/AttachmentLoaderTest.java
 
b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/AttachmentLoaderTest.java
index c536650..cc23204 100644
--- 
a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/AttachmentLoaderTest.java
+++ 
b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/AttachmentLoaderTest.java
@@ -53,7 +53,7 @@ class AttachmentLoaderTest {
 
         Attachment attachment = Attachment.builder()
             .attachmentId(attachmentId)
-            .bytes("attachment".getBytes())
+            .size(11)
             .type("type")
             .build();
 
@@ -79,7 +79,7 @@ class AttachmentLoaderTest {
 
         Attachment attachment = Attachment.builder()
             .attachmentId(attachmentId)
-            .bytes("attachment".getBytes())
+            .size(11)
             .type("type")
             .build();
 
@@ -108,12 +108,12 @@ class AttachmentLoaderTest {
 
         Attachment attachment1 = Attachment.builder()
             .attachmentId(attachmentId1)
-            .bytes("attachment1".getBytes())
+            .size(12)
             .type("type")
             .build();
         Attachment attachment2 = Attachment.builder()
             .attachmentId(attachmentId2)
-            .bytes("attachment2".getBytes())
+            .size(13)
             .type("type")
             .build();
 
@@ -143,7 +143,7 @@ class AttachmentLoaderTest {
 
         Attachment attachment = Attachment.builder()
             .attachmentId(attachmentId)
-            .bytes("attachment".getBytes())
+            .size(11)
             .type("type")
             .build();
 
diff --git 
a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentDAOV2Test.java
 
b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentDAOV2Test.java
index 2bf90c7..e9cbec6 100644
--- 
a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentDAOV2Test.java
+++ 
b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentDAOV2Test.java
@@ -21,7 +21,6 @@ package org.apache.james.mailbox.cassandra.mail;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.nio.charset.StandardCharsets;
 import java.util.Optional;
 
 import org.apache.james.backends.cassandra.CassandraCluster;
@@ -62,7 +61,7 @@ class CassandraAttachmentDAOV2Test {
         Attachment attachment = Attachment.builder()
             .attachmentId(ATTACHMENT_ID)
             .type("application/json")
-            
.bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8))
+            .size(4)
             .build();
         BlobId blobId = BLOB_ID_FACTORY.from("blobId");
         DAOAttachment daoAttachment = 
CassandraAttachmentDAOV2.from(attachment, blobId);
diff --git 
a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
 
b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
index 95b67f8..73a6b0c 100644
--- 
a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
+++ 
b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
@@ -114,8 +114,8 @@ class ElasticSearchListeningMessageSearchIndexTest {
 
     static final MessageAttachment MESSAGE_ATTACHMENT = 
MessageAttachment.builder()
         .attachment(Attachment.builder()
-            .bytes("".getBytes(StandardCharsets.UTF_8))
             .type("type")
+            .size(523)
             .build())
         .name("name")
         .isInline(false)
diff --git 
a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMessage.java
 
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMessage.java
index f174fe6..68afe5d 100644
--- 
a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMessage.java
+++ 
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMessage.java
@@ -53,6 +53,7 @@ import org.apache.james.mailbox.model.ComposedMessageId;
 import org.apache.james.mailbox.model.ComposedMessageIdWithMetaData;
 import org.apache.james.mailbox.model.MessageAttachment;
 import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.model.ParsedAttachment;
 import org.apache.james.mailbox.store.mail.model.DefaultMessageId;
 import org.apache.james.mailbox.store.mail.model.DelegatingMailboxMessage;
 import org.apache.james.mailbox.store.mail.model.FlagsFactory;
@@ -64,6 +65,7 @@ import org.apache.openjpa.persistence.jdbc.ElementJoinColumn;
 import org.apache.openjpa.persistence.jdbc.ElementJoinColumns;
 import org.apache.openjpa.persistence.jdbc.Index;
 
+import com.github.fge.lambdas.Throwing;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.base.Objects;
 
@@ -507,7 +509,9 @@ public abstract class AbstractJPAMailboxMessage implements 
MailboxMessage {
         try {
             return new MessageParser().retrieveAttachments(getFullContent())
                 .stream()
-                .map(attachmentMetadata -> 
attachmentMetadata.asMessageAttachment(AttachmentId.random()))
+                .map(Throwing.<ParsedAttachment, MessageAttachment>function(
+                    attachmentMetadata -> 
attachmentMetadata.asMessageAttachment(AttachmentId.random()))
+                    .sneakyThrow())
                 .collect(Guavate.toImmutableList());
         } catch (IOException e) {
             throw new RuntimeException(e);
diff --git 
a/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
 
b/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
index c2a0b01..b2b4725 100644
--- 
a/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
+++ 
b/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
@@ -33,6 +33,7 @@ import org.apache.james.mailbox.maildir.MaildirMessageName;
 import org.apache.james.mailbox.model.AttachmentId;
 import org.apache.james.mailbox.model.MessageAttachment;
 import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.model.ParsedAttachment;
 import org.apache.james.mailbox.store.mail.model.DefaultMessageId;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.james.mailbox.store.mail.model.Property;
@@ -47,6 +48,7 @@ import org.apache.james.mime4j.stream.MimeConfig;
 import org.apache.james.mime4j.stream.MimeTokenStream;
 import org.apache.james.mime4j.stream.RecursionMode;
 
+import com.github.fge.lambdas.Throwing;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.io.ByteStreams;
 
@@ -275,7 +277,9 @@ public class MaildirMessage implements Message {
         try {
             return new MessageParser().retrieveAttachments(getFullContent())
                 .stream()
-                .map(attachmentMetadata -> 
attachmentMetadata.asMessageAttachment(AttachmentId.random()))
+                .map(Throwing.<ParsedAttachment, MessageAttachment>function(
+                    attachmentMetadata -> 
attachmentMetadata.asMessageAttachment(AttachmentId.random()))
+                    .sneakyThrow())
                 .collect(Guavate.toImmutableList());
         } catch (IOException e) {
             throw new RuntimeException(e);
diff --git 
a/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
 
b/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
index 331c60f..ac192f8 100644
--- 
a/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
+++ 
b/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
@@ -87,18 +87,18 @@ public class InMemoryAttachmentMapper implements 
AttachmentMapper {
 
     @Override
     public Mono<Attachment> storeAttachmentForOwner(String contentType, 
InputStream inputStream, Username owner) {
-            return Mono.fromCallable(() -> {
-                byte[] bytes = toByteArray(inputStream);
-                Attachment attachment = Attachment.builder()
-                    .bytes(bytes)
-                    .type(contentType)
-                    .attachmentId(AttachmentId.random())
-                    .build();
-                attachmentsById.put(attachment.getAttachmentId(), attachment);
-                attachmentsRawContentById.put(attachment.getAttachmentId(), 
bytes);
-                ownersByAttachmentId.put(attachment.getAttachmentId(), owner);
-                return attachment;
-            });
+        return Mono.fromCallable(() -> {
+            byte[] bytes = toByteArray(inputStream);
+            Attachment attachment = Attachment.builder()
+                .type(contentType)
+                .attachmentId(AttachmentId.random())
+                .size(bytes.length)
+                .build();
+            attachmentsById.put(attachment.getAttachmentId(), attachment);
+            attachmentsRawContentById.put(attachment.getAttachmentId(), bytes);
+            ownersByAttachmentId.put(attachment.getAttachmentId(), owner);
+            return attachment;
+        });
     }
 
     private byte[] toByteArray(InputStream inputStream) {
@@ -136,11 +136,11 @@ public class InMemoryAttachmentMapper implements 
AttachmentMapper {
             attachmentsById.put(attachmentId, Attachment.builder()
                 .attachmentId(attachmentId)
                 .type(parsedAttachment.getContentType())
-                .bytes(bytes)
+                .size(bytes.length)
                 .build());
             attachmentsRawContentById.put(attachmentId, bytes);
             messageIdsByAttachmentId.put(attachmentId, ownerMessageId);
-            return parsedAttachment.asMessageAttachment(attachmentId);
+            return parsedAttachment.asMessageAttachment(attachmentId, 
bytes.length);
         } catch (IOException e) {
             throw new MailboxException(String.format("Failed to persist 
attachment %s of message %s", attachmentId, ownerMessageId.serialize()), e);
         }
diff --git 
a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageConverterTest.java
 
b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageConverterTest.java
index e22c8e8..eeb6a1c 100644
--- 
a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageConverterTest.java
+++ 
b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageConverterTest.java
@@ -35,7 +35,6 @@ import static 
org.apache.mailet.base.MailAddressFixture.SENDER;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
-import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -73,8 +72,8 @@ class DeletedMessageConverterTest {
     private static final Collection<MessageAttachment> NO_ATTACHMENT = 
ImmutableList.of();
     private static final Collection<MessageAttachment> ATTACHMENTS = 
ImmutableList.of(MessageAttachment.builder()
         .attachment(Attachment.builder()
-            .bytes("content".getBytes(StandardCharsets.UTF_8))
             .type("type")
+            .size(48)
             .build())
         .build());
 
diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
index 1cb207e..19d2106 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
@@ -99,6 +99,7 @@ import org.apache.james.mime4j.stream.MimeTokenStream;
 import org.apache.james.mime4j.stream.RecursionMode;
 import org.apache.james.util.IteratorWrapper;
 import org.apache.james.util.io.BodyOffsetInputStream;
+import org.apache.james.util.io.InputStreamConsummer;
 import org.apache.james.util.streams.Iterators;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -342,7 +343,8 @@ public class StoreMessageManager implements MessageManager {
                 if (internalDate == null) {
                     internalDate = new Date();
                 }
-                consumeStream(bufferedOut, tmpMsgIn);
+                InputStreamConsummer.consume(tmpMsgIn);
+                bufferedOut.flush();
                 int bodyStartOctet = getBodyStartOctet(bIn);
                 return createAndDispatchMessage(internalDate, mailboxSession, 
file, propertyBuilder, flags, bodyStartOctet);
             }
@@ -426,16 +428,6 @@ public class StoreMessageManager implements MessageManager 
{
         }
     }
 
-    private void consumeStream(BufferedOutputStream bufferedOut, 
BufferedInputStream tmpMsgIn) throws IOException {
-        byte[] discard = new byte[4096];
-        while (tmpMsgIn.read(discard) != -1) {
-            // consume the rest of the stream so everything get copied to
-            // the file now
-            // via the TeeInputStream
-        }
-        bufferedOut.flush();
-    }
-
     private int getBodyStartOctet(BodyOffsetInputStream bIn) {
         int bodyStartOctet = (int) bIn.getBodyStartOffset();
         if (bodyStartOctet == -1) {
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreAttachmentManagerTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreAttachmentManagerTest.java
index 9e129a1..66131d0 100644
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreAttachmentManagerTest.java
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreAttachmentManagerTest.java
@@ -24,8 +24,6 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-import java.nio.charset.StandardCharsets;
-
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageIdManager;
 import org.apache.james.mailbox.exception.AttachmentNotFoundException;
@@ -47,8 +45,8 @@ class StoreAttachmentManagerTest {
     static final AttachmentId ATTACHMENT_ID = AttachmentId.from("1");
     static final Attachment ATTACHMENT = Attachment.builder()
         .attachmentId(ATTACHMENT_ID)
+        .size(48)
         .type("type")
-        .bytes("Any".getBytes(StandardCharsets.UTF_8))
         .build();
 
     StoreAttachmentManager testee;
@@ -56,7 +54,7 @@ class StoreAttachmentManagerTest {
     MessageIdManager messageIdManager;
 
     @BeforeEach
-    void setup() throws Exception {
+    void setup() {
         attachmentMapper = mock(AttachmentMapper.class);
         AttachmentMapperFactory attachmentMapperFactory = 
mock(AttachmentMapperFactory.class);
         
when(attachmentMapperFactory.getAttachmentMapper(any(MailboxSession.class)))
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/SimpleMailboxMessageTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/SimpleMailboxMessageTest.java
index 4d1ff30..0e13f97 100644
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/SimpleMailboxMessageTest.java
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/SimpleMailboxMessageTest.java
@@ -177,8 +177,8 @@ class SimpleMailboxMessageTest {
         MessageUid uid = MessageUid.of(45);
         MessageAttachment messageAttachment = MessageAttachment.builder()
             .attachment(Attachment.builder()
-                .bytes("".getBytes(StandardCharsets.UTF_8))
                 .type("type")
+                .size(485)
                 .build())
             .name("name")
             .isInline(false)
diff --git 
a/server/container/util/src/main/java/org/apache/james/util/io/InputStreamConsummer.java
 
b/server/container/util/src/main/java/org/apache/james/util/io/InputStreamConsummer.java
new file mode 100644
index 0000000..77a7365
--- /dev/null
+++ 
b/server/container/util/src/main/java/org/apache/james/util/io/InputStreamConsummer.java
@@ -0,0 +1,32 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.util.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class InputStreamConsummer {
+    public static void consume(InputStream inputStream) throws IOException {
+        byte[] discard = new byte[4096];
+        while (inputStream.read(discard) != -1) {
+            // consume the rest of the stream
+        }
+    }
+}
diff --git 
a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
 
b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
index 14f7221..4805f1e 100644
--- 
a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
+++ 
b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
@@ -496,7 +496,7 @@ class MessageFullViewFactoryTest {
                 .attachments(ImmutableList.of(MessageAttachment.builder()
                         
.attachment(org.apache.james.mailbox.model.Attachment.builder()
                                 
.attachmentId(AttachmentId.from(blodId.getRawValue()))
-                                .bytes(payload.getBytes())
+                                .size(payload.length())
                                 .type(type)
                                 .build())
                         .cid(Cid.from("cid"))


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

Reply via email to