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

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

commit 30dc0b9f04d5bd3fc7121aedf8af8a972a356d35
Author: Benoit Tellier <[email protected]>
AuthorDate: Fri Dec 7 15:08:00 2018 +0700

    Allow Store.Impl to cary along a byte[] representation
    
    Bytes are available but hidded behing an inputStream for implementation 
purposes.
    
    We propose in this commit a new level of indirection allowing to save an 
object as bytes or as InputSteam.
---
 .../main/java/org/apache/james/blob/api/Store.java | 24 ++++++++++++++++++----
 .../apache/james/blob/mail/MimeMessageStore.java   |  6 +++---
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git 
a/server/blob/blob-api/src/main/java/org/apache/james/blob/api/Store.java 
b/server/blob/blob-api/src/main/java/org/apache/james/blob/api/Store.java
index 6a4f859..ce640d5 100644
--- a/server/blob/blob-api/src/main/java/org/apache/james/blob/api/Store.java
+++ b/server/blob/blob-api/src/main/java/org/apache/james/blob/api/Store.java
@@ -19,7 +19,6 @@
 
 package org.apache.james.blob.api;
 
-import java.io.InputStream;
 import java.util.Collection;
 import java.util.Objects;
 import java.util.stream.Stream;
@@ -66,8 +65,25 @@ public interface Store<T, I> {
 
     class Impl<T, I extends BlobPartsId> implements Store<T, I> {
 
+        public interface ValueToSave {
+            Mono<BlobId> saveIn(BucketName bucketName, BlobStore blobStore);
+        }
+
+        public static class BytesToSave implements ValueToSave {
+            private final byte[] bytes;
+
+            public BytesToSave(byte[] bytes) {
+                this.bytes = bytes;
+            }
+
+            @Override
+            public Mono<BlobId> saveIn(BucketName bucketName, BlobStore 
blobStore) {
+                return blobStore.save(bucketName, bytes);
+            }
+        }
+
         public interface Encoder<T> {
-            Stream<Pair<BlobType, InputStream>> encode(T t);
+            Stream<Pair<BlobType, ValueToSave>> encode(T t);
         }
 
         public interface Decoder<T> {
@@ -94,9 +110,9 @@ public interface Store<T, I> {
                 .map(idFactory::generate);
         }
 
-        private Mono<Tuple2<BlobType, BlobId>> saveEntry(Pair<BlobType, 
InputStream> entry) {
+        private Mono<Tuple2<BlobType, BlobId>> saveEntry(Pair<BlobType, 
ValueToSave> entry) {
             return Mono.just(entry.getLeft())
-                .zipWith(blobStore.save(BucketName.DEFAULT, entry.getRight()));
+                .zipWith(entry.getRight().saveIn(BucketName.DEFAULT, 
blobStore));
         }
 
         @Override
diff --git 
a/server/blob/mail-store/src/main/java/org/apache/james/blob/mail/MimeMessageStore.java
 
b/server/blob/mail-store/src/main/java/org/apache/james/blob/mail/MimeMessageStore.java
index 12683f9..f0c2480 100644
--- 
a/server/blob/mail-store/src/main/java/org/apache/james/blob/mail/MimeMessageStore.java
+++ 
b/server/blob/mail-store/src/main/java/org/apache/james/blob/mail/MimeMessageStore.java
@@ -69,15 +69,15 @@ public class MimeMessageStore {
 
     static class MimeMessageEncoder implements Store.Impl.Encoder<MimeMessage> 
{
         @Override
-        public Stream<Pair<BlobType, InputStream>> encode(MimeMessage message) 
{
+        public Stream<Pair<BlobType, Store.Impl.ValueToSave>> 
encode(MimeMessage message) {
             try {
                 byte[] messageAsArray = messageToArray(message);
                 int bodyStartOctet = computeBodyStartOctet(messageAsArray);
                 byte[] headerBytes = getHeaderBytes(messageAsArray, 
bodyStartOctet);
                 byte[] bodyBytes = getBodyBytes(messageAsArray, 
bodyStartOctet);
                 return Stream.of(
-                    Pair.of(HEADER_BLOB_TYPE, new 
ByteArrayInputStream(headerBytes)),
-                    Pair.of(BODY_BLOB_TYPE, new 
ByteArrayInputStream(bodyBytes)));
+                    Pair.of(HEADER_BLOB_TYPE, new 
Store.Impl.BytesToSave(headerBytes)),
+                    Pair.of(BODY_BLOB_TYPE, new 
Store.Impl.BytesToSave(bodyBytes)));
             } catch (MessagingException | IOException e) {
                 throw new RuntimeException(e);
             }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to