Author: norman
Date: Tue May 24 15:56:14 2011
New Revision: 1127121

URL: http://svn.apache.org/viewvc?rev=1127121&view=rev
Log:
Some improvements to reduce the streaming amount while append a message

Added:
    
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStream.java
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/streaming/
    
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStreamTest.java
Removed:
    
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/CRLFOutputStream.java
    
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/StreamUtils.java
Modified:
    
james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMessage.java
    
james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMessage.java
    
james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAStreamingMessage.java
    
james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
    
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
    
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/PartContentBuilder.java

Modified: 
james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMessage.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMessage.java?rev=1127121&r1=1127120&r2=1127121&view=diff
==============================================================================
--- 
james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMessage.java
 (original)
+++ 
james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMessage.java
 Tue May 24 15:56:14 2011
@@ -32,6 +32,7 @@ import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.mail.Flags;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.commons.JcrUtils;
 import org.apache.james.mailbox.MailboxException;
@@ -43,7 +44,6 @@ import org.apache.james.mailbox.store.ma
 import org.apache.james.mailbox.store.mail.model.Property;
 import org.apache.james.mailbox.store.mail.model.PropertyBuilder;
 import org.apache.james.mailbox.store.streaming.LazySkippingInputStream;
-import org.apache.james.mailbox.store.streaming.StreamUtils;
 import org.slf4j.Logger;
 
 /**
@@ -143,7 +143,7 @@ public class JCRMessage extends Abstract
         
         this.logger = logger;
         try {
-            this.content = new 
ByteArrayInputStream(StreamUtils.toByteArray(message.getFullContent()));
+            this.content = new 
ByteArrayInputStream(IOUtils.toByteArray(message.getFullContent()));
         } catch (IOException e) {
             throw new MailboxException("Unable to parse message",e);
         }

Modified: 
james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMessage.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMessage.java?rev=1127121&r1=1127120&r2=1127121&view=diff
==============================================================================
--- 
james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMessage.java
 (original)
+++ 
james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMessage.java
 Tue May 24 15:56:14 2011
@@ -32,12 +32,12 @@ import javax.persistence.FetchType;
 import javax.persistence.Lob;
 import javax.persistence.Table;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.jpa.mail.model.JPAHeader;
 import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.james.mailbox.store.mail.model.PropertyBuilder;
-import org.apache.james.mailbox.store.streaming.StreamUtils;
 
 @Entity(name="Message")
 @Table(name="JAMES_MAIL")
@@ -56,7 +56,7 @@ public class JPAMessage extends Abstract
             InputStream content, int bodyStartOctet, final List<JPAHeader> 
headers, final PropertyBuilder propertyBuilder) throws MailboxException {
         super(mailbox, uid, internalDate, flags, size 
,bodyStartOctet,headers,propertyBuilder);
         try {
-            this.content = StreamUtils.out(content).toByteArray();
+            this.content = IOUtils.toByteArray(content);
         } catch (IOException e) {
             throw new MailboxException("Unable to parse message",e);
         }
@@ -71,7 +71,7 @@ public class JPAMessage extends Abstract
     public JPAMessage(JPAMailbox mailbox, long uid, Message<?> message) throws 
MailboxException{
         super(mailbox, uid, message);
         try {
-            this.content = 
StreamUtils.out(message.getFullContent()).toByteArray();
+            this.content = IOUtils.toByteArray(message.getFullContent());
         } catch (IOException e) {
             throw new MailboxException("Unable to parse message",e);
         }

Modified: 
james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAStreamingMessage.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAStreamingMessage.java?rev=1127121&r1=1127120&r2=1127121&view=diff
==============================================================================
--- 
james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAStreamingMessage.java
 (original)
+++ 
james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAStreamingMessage.java
 Tue May 24 15:56:14 2011
@@ -31,13 +31,13 @@ import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.Table;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.jpa.mail.model.JPAHeader;
 import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.james.mailbox.store.mail.model.PropertyBuilder;
 import org.apache.james.mailbox.store.streaming.LazySkippingInputStream;
-import org.apache.james.mailbox.store.streaming.StreamUtils;
 import org.apache.openjpa.persistence.Persistent;
 
 /**
@@ -76,7 +76,7 @@ public class JPAStreamingMessage extends
     public JPAStreamingMessage(JPAMailbox mailbox, long uid,Message<?> 
message) throws MailboxException {
         super(mailbox, uid, message);
         try {
-            this.content = new 
ByteArrayInputStream(StreamUtils.toByteArray(message.getFullContent()));
+            this.content = new 
ByteArrayInputStream(IOUtils.toByteArray(message.getFullContent()));
         } catch (IOException e) {
             throw new MailboxException("Unable to parse message",e);
         }

Modified: 
james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java?rev=1127121&r1=1127120&r2=1127121&view=diff
==============================================================================
--- 
james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
 (original)
+++ 
james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
 Tue May 24 15:56:14 2011
@@ -27,6 +27,7 @@ import java.util.List;
 
 import javax.mail.Flags;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.NotImplementedException;
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.store.mail.model.Header;
@@ -34,7 +35,6 @@ import org.apache.james.mailbox.store.ma
 import org.apache.james.mailbox.store.mail.model.Property;
 import org.apache.james.mailbox.store.mail.model.PropertyBuilder;
 import org.apache.james.mailbox.store.streaming.LazySkippingInputStream;
-import org.apache.james.mailbox.store.streaming.StreamUtils;
 
 public class MaildirMessage extends AbstractMaildirMessage {
 
@@ -110,7 +110,7 @@ public class MaildirMessage extends Abst
         this.seen = message.isSeen();
         
         try {
-            this.rawFullContent = new 
ByteArrayInputStream(StreamUtils.toByteArray(message.getFullContent()));
+            this.rawFullContent = new 
ByteArrayInputStream(IOUtils.toByteArray(message.getFullContent()));
         } catch (IOException e) {
             throw new MailboxException("Parsing of message failed",e);
         }

Modified: 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java?rev=1127121&r1=1127120&r2=1127121&view=diff
==============================================================================
--- 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
 (original)
+++ 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
 Tue May 24 15:56:14 2011
@@ -20,10 +20,10 @@
 package org.apache.james.mailbox.store;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.PushbackInputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -36,8 +36,9 @@ import java.util.TreeMap;
 
 import javax.mail.Flags;
 import javax.mail.Flags.Flag;
-import javax.mail.util.SharedFileInputStream;
 
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.input.TeeInputStream;
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxListener;
 import org.apache.james.mailbox.MailboxSession;
@@ -58,6 +59,7 @@ import org.apache.james.mailbox.store.ma
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.james.mailbox.store.mail.model.PropertyBuilder;
+import org.apache.james.mailbox.store.streaming.BodyOffsetInputStream;
 import org.apache.james.mailbox.store.streaming.ConfigurableMimeTokenStream;
 import org.apache.james.mailbox.store.streaming.CountingInputStream;
 import org.apache.james.mailbox.store.transaction.Mapper;
@@ -164,26 +166,20 @@ public abstract class StoreMessageManage
             final MailboxSession mailboxSession,final boolean isRecent, final 
Flags flagsToBeSet)
     throws MailboxException {
         File file = null;
-        SharedFileInputStream tmpMsgIn = null;
+        TeeInputStream tmpMsgIn = null;
+        BodyOffsetInputStream bIn = null;
+        FileOutputStream out = null;
+        FileInputStream contentIn = null;
+        
         try {
             // Create a temporary file and copy the message to it. We will 
work with the file as
             // source for the InputStream
             file = File.createTempFile("imap", ".msg");
-            FileOutputStream out = new FileOutputStream(file);
-            
-            byte[] buf = new byte[1024];
-            int i = 0;
-            while ((i = msgIn.read(buf)) != -1) {
-                out.write(buf, 0, i);
-            }
-            out.flush();
-            out.close();
+            out = new FileOutputStream(file);
             
-            tmpMsgIn = new SharedFileInputStream(file);
+            tmpMsgIn = new TeeInputStream(msgIn, out);
            
-            final int size = (int) file.length();
-            final int bodyStartOctet = bodyStartOctet(tmpMsgIn);
-
+            bIn = new BodyOffsetInputStream(tmpMsgIn);
             // Disable line length... This should be handled by the smtp 
server component and not the parser itself
             // https://issues.apache.org/jira/browse/IMAP-122
             MimeEntityConfig config = new MimeEntityConfig();
@@ -192,7 +188,7 @@ public abstract class StoreMessageManage
             final ConfigurableMimeTokenStream parser = new 
ConfigurableMimeTokenStream(config);
            
             parser.setRecursionMode(MimeTokenStream.M_NO_RECURSE);
-            parser.parse(tmpMsgIn.newStream(0, -1));
+            parser.parse(bIn);
             final List<Header> headers = new ArrayList<Header>();
             
             int lineNumber = 0;
@@ -256,12 +252,14 @@ public abstract class StoreMessageManage
                 final CountingInputStream bodyStream = new 
CountingInputStream(parser.getInputStream());
                 bodyStream.readAll();
                 long lines = bodyStream.getLineCount();
-                
+                bodyStream.close();
                 next = parser.next();
                 if (next == MimeTokenStream.T_EPILOGUE)  {
                     final CountingInputStream epilogueStream = new 
CountingInputStream(parser.getInputStream());
                     epilogueStream.readAll();
                     lines+=epilogueStream.getLineCount();
+                    epilogueStream.close();
+
                 }
                 propertyBuilder.setTextualLineCount(lines);
             }
@@ -282,8 +280,17 @@ public abstract class StoreMessageManage
             if (internalDate == null) {
                 internalDate = new Date();
             }
+            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
+            }
+            int bodyStartOctet = (int) bIn.getBodyStartOffset();
+            contentIn = new FileInputStream(file);
+            final int size = (int) file.length();
+
             long nextUid = uidProvider.nextUid(mailboxSession, 
getMailboxEntity());
-            final Message<Id> message = createMessage(nextUid, internalDate, 
size, bodyStartOctet, tmpMsgIn.newStream(0, -1), flags, headers, 
propertyBuilder);
+            final Message<Id> message = createMessage(nextUid, internalDate, 
size, bodyStartOctet, contentIn, flags, headers, propertyBuilder);
             long uid = appendMessageToStore(message, mailboxSession);
                        
             Map<Long, MessageMetaData> uids = new HashMap<Long, 
MessageMetaData>();
@@ -297,13 +304,11 @@ public abstract class StoreMessageManage
         } catch (MailboxException e) {
             throw new MailboxException("Unable to parse message", e);
         } finally {
-            if (tmpMsgIn != null) {
-                try {
-                    tmpMsgIn.close();
-                } catch (IOException e) {
-                    // ignore on close
-                }
-            }
+            IOUtils.closeQuietly(bIn);
+            IOUtils.closeQuietly(tmpMsgIn);
+            IOUtils.closeQuietly(out);
+            IOUtils.closeQuietly(contentIn);
+
             // delete the temporary file if one was specified
             if (file != null) {
                 file.delete();
@@ -311,46 +316,7 @@ public abstract class StoreMessageManage
         }
 
     }
-    
-    /**
-     * Return the position in the given {@link InputStream} at which the Body 
of the 
-     * Message starts
-     * 
-     * @param msgIn
-     * @return bodyStartOctet
-     * @throws IOException
-     */
-    private int bodyStartOctet(InputStream msgIn) throws IOException{
-        // we need to pushback maximal 3 bytes
-        PushbackInputStream in = new PushbackInputStream(msgIn,3);
-        
-        int bodyStartOctet = in.available();
-        int i = -1;
-        int count = 0;
-        while ((i = in.read()) != -1 && in.available() > 4) {
-            if (i == 0x0D) {
-                int a = in.read();
-                if (a == 0x0A) {
-                    int b = in.read();
-
-                    if (b == 0x0D) {
-                        int c = in.read();
-
-                        if (c == 0x0A) {
-                            bodyStartOctet = count+4;
-                            break;
-                        }
-                        in.unread(c);
-                    }
-                    in.unread(b);
-                }
-                in.unread(a);
-            }
-            count++;
-        }
-        
-        return bodyStartOctet;
-    }
+
 
     /**
      * Create a new {@link MailboxMembership} for the given data

Added: 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStream.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStream.java?rev=1127121&view=auto
==============================================================================
--- 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStream.java
 (added)
+++ 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStream.java
 Tue May 24 15:56:14 2011
@@ -0,0 +1,160 @@
+/****************************************************************
+ * 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.mailbox.store.streaming;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PushbackInputStream;
+
+/**
+ * {@link InputStream} which helps to keep track of the BodyOffset of the 
wrapped
+ * {@link InputStream}
+ *  
+ *  IMPORTANT: This class is not thread-safe!
+ *
+ */
+public class BodyOffsetInputStream extends InputStream{
+    private long count = 0;
+    private long bodyStartOctet = -1;
+    private PushbackInputStream in;
+    private long readBytes = 0;
+
+    public BodyOffsetInputStream(InputStream in) {
+        // we need to pushback at max 3 bytes
+        this.in = new PushbackInputStream(in, 3);
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see java.io.InputStream#read()
+     */
+    public int read() throws IOException {
+        int i = in.read();
+        if (i != -1) {
+            readBytes++;
+            if (bodyStartOctet == -1 && i == 0x0D) {
+                int a = in.read();
+                if (a == 0x0A) {
+                    int b = in.read();
+
+                    if (b == 0x0D) {
+                        int c = in.read();
+
+                        if (c == 0x0A) {
+                            bodyStartOctet = count+4;
+                        }
+                        in.unread(c);
+                    }
+                    in.unread(b);
+                }
+                in.unread(a);
+            }
+            count++;
+        }
+        return i;
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        if (bodyStartOctet == -1) {
+            return super.read(b, off, len);
+        } else {
+            int r = in.read(b, off, len);
+            if (r != -1) {
+                readBytes += r;
+            }
+            return r;
+        }
+    }
+
+    @Override
+    public int read(byte[] b) throws IOException {
+        if (bodyStartOctet == -1) {
+            return super.read(b);
+        } else {
+            int r = in.read(b);
+            if (r != -1) {
+                readBytes += r;
+            }
+            return r;
+        }
+    }
+
+    @Override
+    public int available() throws IOException {
+        return in.available();
+    }
+
+    @Override
+    public void close() throws IOException {
+        in.close();
+    }
+
+    @Override
+    public void mark(int readlimit) {
+        
+    }
+
+    /**
+     * Mark is not supported by this implementation
+     */
+    public boolean markSupported() {
+        return false;
+    }
+
+    /**
+     * Throws {@link IOException}
+     */
+    public void reset() throws IOException {
+        throw new IOException("Not supported");
+    }
+
+    @Override
+    public long skip(long n) throws IOException {
+        long i = 0; 
+        while(i++ < n) {
+            if (read() == -1) {
+                break;
+            }
+        }
+        return i;
+    }
+    
+    /**
+     * Return the bodyStartOffset or -1 if it could not be found. 
+     * Be aware you can only expect some valid result from the method
+     * if you have consumed the whole InputStream or if you are
+     * sure that you reached the body
+     * 
+     * @return offset
+     */
+    public long getBodyStartOffset() {
+        return bodyStartOctet;
+    }
+    
+    /**
+     * Return the read bytes so far
+     * 
+     * @return readBytes
+     */
+    public long getReadBytes() {
+        return readBytes;
+    }
+    
+}

Modified: 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/PartContentBuilder.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/PartContentBuilder.java?rev=1127121&r1=1127120&r2=1127121&view=diff
==============================================================================
--- 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/PartContentBuilder.java
 (original)
+++ 
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/PartContentBuilder.java
 Tue May 24 15:56:14 2011
@@ -26,6 +26,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.mailbox.Content;
 import org.apache.james.mailbox.MessageResult;
 import org.apache.james.mailbox.MessageResult.Header;
@@ -159,7 +160,7 @@ public class PartContentBuilder {
                     }
                 }
                 if (valid) {
-                    content = StreamUtils.toByteArray(parser.getInputStream());
+                    content = IOUtils.toByteArray(parser.getInputStream());
                 } else {
                     content = EMPTY;
                 }
@@ -191,7 +192,7 @@ public class PartContentBuilder {
                 }
             }
             if (valid) {
-                content = StreamUtils.toByteArray(parser.getInputStream());
+                content = IOUtils.toByteArray(parser.getInputStream());
             } else {
                 content = EMPTY;
             }

Added: 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStreamTest.java
URL: 
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStreamTest.java?rev=1127121&view=auto
==============================================================================
--- 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStreamTest.java
 (added)
+++ 
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/streaming/BodyOffsetInputStreamTest.java
 Tue May 24 15:56:14 2011
@@ -0,0 +1,75 @@
+/****************************************************************
+ * 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.mailbox.store.streaming;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class BodyOffsetInputStreamTest {
+    private String mail = "Subject: test\r\n\r\nbody";
+    private long expectedOffset = 17;
+    private long bytes = mail.length();
+    @Test
+    public void testRead() throws IOException {
+        BodyOffsetInputStream in = new BodyOffsetInputStream(new 
ByteArrayInputStream(mail.getBytes()));
+        
+        @SuppressWarnings("unused")
+        int i = -1;
+        while ((i = in.read())!= -1) {
+            // consume stream
+        }
+        assertEquals(expectedOffset, in.getBodyStartOffset());
+        assertEquals(bytes, in.getReadBytes());
+
+    }
+    
+    
+    @Test
+    public void testReadWithArray() throws IOException {
+        BodyOffsetInputStream in = new BodyOffsetInputStream(new 
ByteArrayInputStream(mail.getBytes()));
+        
+        @SuppressWarnings("unused")
+        int i = -1;
+        byte[] b = new byte[8];
+        while ((i = in.read(b))!= -1) {
+            // consume stream
+        }
+        assertEquals(expectedOffset, in.getBodyStartOffset());
+        assertEquals(bytes, in.getReadBytes());
+    }
+    
+    
+    @Test
+    public void testReadWithArrayBiggerThenStream() throws IOException {
+        BodyOffsetInputStream in = new BodyOffsetInputStream(new 
ByteArrayInputStream(mail.getBytes()));
+        
+        @SuppressWarnings("unused")
+        int i = -1;
+        byte[] b = new byte[4096];
+        while ((i = in.read(b))!= -1) {
+            // consume stream
+        }
+        assertEquals(expectedOffset, in.getBodyStartOffset());
+        assertEquals(bytes, in.getReadBytes());
+    }
+}



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

Reply via email to