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 no...@apache.org on 2011/08/21 20:41:15 UTC
svn commit: r1160032 - in /james/mailbox/trunk:
api/src/main/java/org/apache/james/mailbox/
store/src/main/java/org/apache/james/mailbox/store/
store/src/main/java/org/apache/james/mailbox/store/streaming/
store/src/test/java/org/apache/james/mailbox/s...
Author: norman
Date: Sun Aug 21 18:41:14 2011
New Revision: 1160032
URL: http://svn.apache.org/viewvc?rev=1160032&view=rev
Log:
Allow to access raw message header without parsing it. This will give us some possibilities to improve performance in imap and pop3. See MAILBOX-115
Added:
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java
- copied, changed from r1155009, james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/AbstractFullContent.java
Removed:
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/AbstractFullContent.java
Modified:
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Content.java
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Headers.java
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/InputStreamContent.java
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResult.java
james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MimeDescriptor.java
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/ByteContent.java
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/PartContentBuilder.java
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderComplexMultipartTest.java
james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderMultipartAlternativeTest.java
Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Content.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Content.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Content.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Content.java Sun Aug 21 18:41:14 2011
@@ -20,7 +20,7 @@
package org.apache.james.mailbox;
import java.io.IOException;
-import java.nio.channels.WritableByteChannel;
+import java.io.InputStream;
/**
* IMAP needs to know the size of the content before it starts to write it out.
@@ -28,21 +28,15 @@ import java.nio.channels.WritableByteCha
*/
public interface Content {
+
/**
- * Writes content to the given channel.
- *
- * Be aware that this operation may only be called once one the content
- * because its possible dispose temp data. If you need to write the content
- * more then one time you should "re-create" the content
+ * Return the content as {@link InputStream}
*
- * @param channel
- * <code>Channel</code> open, not null
- * @throws MailboxException
+ * @return content
* @throws IOException
- * when channel IO fails
*/
- void writeTo(WritableByteChannel channel) throws IOException;
-
+ InputStream getInputStream() throws IOException;
+
/**
* Size (in octets) of the content.
*
Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Headers.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Headers.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Headers.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/Headers.java Sun Aug 21 18:41:14 2011
@@ -24,10 +24,7 @@ import java.util.Iterator;
import org.apache.james.mailbox.MessageResult.FetchGroup;
import org.apache.james.mailbox.MessageResult.Header;
-/**
- * TODO: remove when MessageResult is sorted out
- */
-public interface Headers {
+public interface Headers extends Content{
/**
* Gets headers for the message.
*
@@ -35,4 +32,6 @@ public interface Headers {
* {@link FetchGroup#HEADERS} was not fetched
*/
Iterator<Header> headers() throws MailboxException;
+
+
}
Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/InputStreamContent.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/InputStreamContent.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/InputStreamContent.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/InputStreamContent.java Sun Aug 21 18:41:14 2011
@@ -18,19 +18,14 @@
****************************************************************/
package org.apache.james.mailbox;
-import java.io.IOException;
import java.io.InputStream;
/**
* {@link Content} which offers the content via {@link InputStream} too
+ *
+ * @deprecated use {@link Content}
*/
+@Deprecated
public interface InputStreamContent extends Content {
- /**
- * Return the content as {@link InputStream}
- *
- * @return content
- * @throws IOException
- */
- InputStream getInputStream() throws IOException;
}
Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResult.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResult.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResult.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResult.java Sun Aug 21 18:41:14 2011
@@ -19,6 +19,7 @@
package org.apache.james.mailbox;
+import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
@@ -51,7 +52,7 @@ import java.util.Set;
* </p>
*/
-public interface MessageResult extends Comparable<MessageResult>, MessageMetaData, Headers {
+public interface MessageResult extends Comparable<MessageResult>, MessageMetaData {
/**
* Indicates the results fetched.
@@ -185,8 +186,9 @@ public interface MessageResult extends C
* @return <code>Content</code>, or or null if
* {@link FetchGroup#FULL_CONTENT} has not been included in the
* results
+ * @throws IOException
*/
- Content getFullContent() throws MailboxException;
+ Content getFullContent() throws MailboxException, IOException;
/**
* Gets the full content of the given mime part.
@@ -207,8 +209,9 @@ public interface MessageResult extends C
* @return <code>Content</code>, or or null if
* {@link FetchGroup#FULL_CONTENT} has not been included in the
* results
+ * @throws IOException
*/
- Content getBody() throws MailboxException;
+ Content getBody() throws MailboxException, IOException;
/**
* Gets the body of the given mime part.
@@ -234,6 +237,9 @@ public interface MessageResult extends C
*/
Content getMimeBody(MimePath path) throws MailboxException;
+
+ Headers getHeaders() throws MailboxException;
+
/**
* Describes a path within a multipart MIME message. All implementations
* must implement equals. Two paths are equal if and only if each position
Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MimeDescriptor.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MimeDescriptor.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MimeDescriptor.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MimeDescriptor.java Sun Aug 21 18:41:14 2011
@@ -25,7 +25,9 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
-public interface MimeDescriptor extends Headers {
+import org.apache.james.mailbox.MessageResult.Header;
+
+public interface MimeDescriptor {
/**
* Gets the top level MIME content media type.
@@ -137,4 +139,7 @@ public interface MimeDescriptor extends
* @return <code>Header</code> <code>Iterator</code>, not null
*/
Map<String, String> contentTypeParameters();
+
+ Iterator<Header> headers() throws MailboxException;
+
}
\ No newline at end of file
Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java Sun Aug 21 18:41:14 2011
@@ -19,6 +19,8 @@
package org.apache.james.mailbox.store;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
@@ -28,49 +30,35 @@ import java.util.Map;
import javax.mail.Flags;
import org.apache.james.mailbox.Content;
+import org.apache.james.mailbox.Headers;
import org.apache.james.mailbox.MailboxException;
import org.apache.james.mailbox.MessageResult;
import org.apache.james.mailbox.MimeDescriptor;
+import org.apache.james.mailbox.store.mail.model.Message;
+import org.apache.james.mailbox.store.streaming.InputStreamContent;
+import org.apache.james.mailbox.store.streaming.InputStreamContent.Type;
/**
* Bean based implementation.
*/
public class MessageResultImpl implements MessageResult {
- private long uid;
- private Flags flags;
-
- private int size;
-
- private Date internalDate;
-
- private List<Header> headers;
-
- private Content body;
-
- private Content fullContent;
-
- private int includedResults = FetchGroup.MINIMAL;
-
- private Map<MimePath, PartContent> partsByPath = new HashMap<MimePath, PartContent>();
+ private final Map<MimePath, PartContent> partsByPath = new HashMap<MimePath, PartContent>();
private MimeDescriptor mimeDescriptor;
- private long modSeq;
-
- public MessageResultImpl(long uid) {
- setUid(uid);
- }
+ private final Message<?> message;
- public MessageResultImpl() {
- }
+ private HeadersImpl headers;
+ private Content fullContent;
+ private Content bodyContent;
- public MessageResultImpl(long uid, Flags flags) {
- setUid(uid);
- setFlags(flags);
- }
-
+ public MessageResultImpl(Message<?> message) throws IOException {
+ this.message = message;
+ this.headers = new HeadersImpl(message);
+
+ }
/*
* (non-Javadoc)
@@ -78,7 +66,7 @@ public class MessageResultImpl implement
* @see org.apache.james.mailbox.MessageResult#getUid()
*/
public long getUid() {
- return uid;
+ return message.getUid();
}
/*
@@ -87,7 +75,7 @@ public class MessageResultImpl implement
* @see org.apache.james.mailbox.MessageResult#getInternalDate()
*/
public Date getInternalDate() {
- return internalDate;
+ return message.getInternalDate();
}
/*
@@ -96,11 +84,7 @@ public class MessageResultImpl implement
* @see org.apache.james.mailbox.MessageResult#getFlags()
*/
public Flags getFlags() {
- return flags;
- }
-
- public void setUid(long uid) {
- this.uid = uid;
+ return message.createFlags();
}
/*
@@ -109,11 +93,7 @@ public class MessageResultImpl implement
* @see org.apache.james.mailbox.MessageResult#getSize()
*/
public long getSize() {
- return size;
- }
-
- public void setFlags(Flags flags) {
- this.flags = flags;
+ return message.getFullContentOctets();
}
/*
@@ -122,9 +102,9 @@ public class MessageResultImpl implement
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(MessageResult that) {
- if (this.uid > 0 && that.getUid() > 0) {
+ if (getUid() > 0 && that.getUid() > 0) {
// TODO: this seems inefficient
- return Long.valueOf(uid).compareTo(Long.valueOf(that.getUid()));
+ return Long.valueOf(getUid()).compareTo(Long.valueOf(that.getUid()));
} else {
// TODO: throwing an undocumented untyped runtime seems wrong
// TODO: if uids must be greater than zero then this should be
@@ -135,52 +115,17 @@ public class MessageResultImpl implement
}
}
-
- public void setSize(int size) {
- this.size = size;
- }
-
- public void setInternalDate(Date internalDate) {
- this.internalDate = internalDate;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.apache.james.mailbox.MessageResult#headers()
- */
- public Iterator<Header> headers() {
- if (headers == null) {
- return null;
- }
- return headers.iterator();
- }
-
- public List<Header> getHeaders() {
- return headers;
- }
-
- public void setHeaders(List<Header> headers) {
- this.headers = headers;
- if (headers != null) {
- includedResults |= FetchGroup.HEADERS;
- }
- }
-
+
/*
* (non-Javadoc)
*
* @see org.apache.james.mailbox.MessageResult#getFullContent()
*/
- public final Content getFullContent() {
- return fullContent;
- }
-
- public final void setFullContent(Content fullMessage) {
- this.fullContent = fullMessage;
- if (fullMessage != null) {
- includedResults |= FetchGroup.FULL_CONTENT;
+ public synchronized final Content getFullContent() throws IOException {
+ if (fullContent == null) {
+ fullContent = new InputStreamContent(message, Type.Full);
}
+ return fullContent;
}
/*
@@ -188,17 +133,14 @@ public class MessageResultImpl implement
*
* @see org.apache.james.mailbox.MessageResult#getBody()
*/
- public final Content getBody() {
- return body;
- }
-
- public final void setBody(Content messageBody) {
- this.body = messageBody;
- if (messageBody != null) {
- includedResults |= FetchGroup.BODY_CONTENT;
+ public synchronized final Content getBody() throws IOException {
+ if (bodyContent == null) {
+ bodyContent = new InputStreamContent(message, Type.Body);
}
+ return bodyContent;
}
+
/**
* Renders suitably for logging.
*
@@ -207,7 +149,7 @@ public class MessageResultImpl implement
public String toString() {
final String TAB = " ";
- String retValue = "MessageResultImpl ( " + "uid = " + this.uid + TAB + "flags = " + this.flags + TAB + "size = " + this.size + TAB + "internalDate = " + this.internalDate + TAB + "includedResults = " + this.includedResults + TAB + " )";
+ String retValue = "MessageResultImpl ( " + "uid = " + getUid() + TAB + "flags = " + getFlags() + TAB + "size = " + getSize() + TAB + "internalDate = " + getInternalDate()+ ")";
return retValue;
}
@@ -396,7 +338,6 @@ public class MessageResultImpl implement
}
public void setMimeDescriptor(final MimeDescriptor mimeDescriptor) {
- includedResults |= FetchGroup.MIME_DESCRIPTOR;
this.mimeDescriptor = mimeDescriptor;
}
@@ -414,10 +355,46 @@ public class MessageResultImpl implement
* @see org.apache.james.mailbox.MessageMetaData#getModSeq()
*/
public long getModSeq() {
- return modSeq;
+ return message.getModSeq();
}
- public void setModSeq(long modSeq) {
- this.modSeq = modSeq;
+ @Override
+ public synchronized Headers getHeaders() throws MailboxException {
+ if (headers == null) {
+ headers = new HeadersImpl(message);
+ }
+ return headers;
+ }
+
+ private final class HeadersImpl implements Headers {
+
+ private Message<?> msg;
+ private List<Header> headers;
+
+ public HeadersImpl(Message<?> msg) {
+ this.msg = msg;
+ }
+ @Override
+ public InputStream getInputStream() throws IOException {
+ return msg.getHeaderContent();
+ }
+
+ @Override
+ public long size() {
+ return msg.getFullContentOctets() - msg.getBodyOctets();
+ }
+
+ @Override
+ public synchronized Iterator<Header> headers() throws MailboxException {
+ if (headers == null) {
+ try {
+ headers = ResultUtils.createHeaders(message);
+ } catch (IOException e) {
+ throw new MailboxException("Unable to parse headers", e);
+ }
+ }
+ return headers.iterator();
+ }
+
}
}
Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java Sun Aug 21 18:41:14 2011
@@ -151,30 +151,20 @@ public class ResultUtils {
* @return result
* @throws MailboxException
*/
- public static MessageResult loadMessageResult(final Message<?> message, final FetchGroup fetchGroup)
- throws MailboxException {
+ public static MessageResult loadMessageResult(final Message<?> message, final FetchGroup fetchGroup) throws MailboxException {
+ try {
- MessageResultImpl messageResult = new MessageResultImpl();
- messageResult.setUid(message.getUid());
- if (fetchGroup != null) {
- int content = fetchGroup.content();
- messageResult.setFlags(message.createFlags());
- messageResult.setSize((int)message.getFullContentOctets());
- messageResult.setInternalDate(message.getInternalDate());
- messageResult.setModSeq(message.getModSeq());
-
- try {
+ MessageResultImpl messageResult = new MessageResultImpl(message);
+ if (fetchGroup != null) {
+ int content = fetchGroup.content();
if ((content & FetchGroup.HEADERS) > 0) {
- addHeaders(message, messageResult);
content -= FetchGroup.HEADERS;
}
if ((content & FetchGroup.BODY_CONTENT) > 0) {
- addBody(message, messageResult);
content -= FetchGroup.BODY_CONTENT;
}
if ((content & FetchGroup.FULL_CONTENT) > 0) {
- addFullContent(message, messageResult);
content -= FetchGroup.FULL_CONTENT;
}
if ((content & FetchGroup.MIME_DESCRIPTOR) > 0) {
@@ -186,13 +176,15 @@ public class ResultUtils {
}
addPartContent(fetchGroup, message, messageResult);
- } catch (IOException e) {
- throw new MailboxException("Unable to parse message", e);
- } catch (MimeException e) {
- throw new MailboxException("Unable to parse message", e);
}
+ return messageResult;
+
+ } catch (IOException e) {
+ throw new MailboxException("Unable to parse message", e);
+ } catch (MimeException e) {
+ throw new MailboxException("Unable to parse message", e);
}
- return messageResult;
+
}
private static void addMimeDescriptor(Message<?> message, MessageResultImpl messageResult) throws IOException, MimeException {
@@ -200,23 +192,6 @@ public class ResultUtils {
messageResult.setMimeDescriptor(descriptor);
}
- private static void addFullContent(final Message<?> messageRow, MessageResultImpl messageResult) throws IOException {
- Content content = createFullContent(messageRow);
- messageResult.setFullContent(content);
-
- }
-
- private static void addBody(final Message<?> message, MessageResultImpl messageResult)throws IOException {
- final Content content = createBodyContent(message);
- messageResult.setBody(content);
-
- }
-
- private static void addHeaders(final Message<?> message,
- MessageResultImpl messageResult) throws IOException {
- final List<MessageResult.Header> headers = createHeaders(message);
- messageResult.setHeaders(headers);
- }
private static void addPartContent(final FetchGroup fetchGroup,
Message<?> message, MessageResultImpl messageResult)
@@ -287,9 +262,8 @@ public class ResultUtils {
MessageResultImpl messageResult, MimePath mimePath)
throws IOException, MimeException {
final int[] path = path(mimePath);
- if (path == null) {
- addHeaders(message, messageResult);
- } else {
+ if (path != null) {
+
final PartContentBuilder builder = build(path, message);
final List<MessageResult.Header> headers = builder.getMessageHeaders();
messageResult.setHeaders(mimePath, headers.iterator());
@@ -300,9 +274,7 @@ public class ResultUtils {
MessageResultImpl messageResult, MimePath mimePath)
throws IOException, MimeException {
final int[] path = path(mimePath);
- if (path == null) {
- addHeaders(message, messageResult);
- } else {
+ if (path != null) {
final PartContentBuilder builder = build(path, message);
final List<MessageResult.Header> headers = builder.getMimeHeaders();
messageResult.setMimeHeaders(mimePath, headers.iterator());
@@ -312,9 +284,7 @@ public class ResultUtils {
private static void addBodyContent(Message<?> message,
MessageResultImpl messageResult, MimePath mimePath) throws IOException, MimeException {
final int[] path = path(mimePath);
- if (path == null) {
- addBody(message, messageResult);
- } else {
+ if (path != null) {
final PartContentBuilder builder = build(path, message);
final Content content = builder.getMessageBodyContent();
messageResult.setBodyContent(mimePath, content);
@@ -335,9 +305,7 @@ public class ResultUtils {
throws MailboxException, IOException,
MimeException {
final int[] path = path(mimePath);
- if (path == null) {
- addFullContent(message, messageResult);
- } else {
+ if (path != null) {
final PartContentBuilder builder = build(path, message);
final Content content = builder.getFullContent();
messageResult.setFullContent(mimePath, content);
Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java Sun Aug 21 18:41:14 2011
@@ -25,6 +25,7 @@ import java.util.NoSuchElementException;
import javax.mail.Flags;
import org.apache.james.mailbox.Content;
+import org.apache.james.mailbox.Headers;
import org.apache.james.mailbox.MailboxException;
import org.apache.james.mailbox.MessageRange;
import org.apache.james.mailbox.MimeDescriptor;
@@ -223,10 +224,6 @@ public class StoreMessageResultIterator<
return uid;
}
- public Iterator<Header> headers() throws MailboxException {
- throw exception;
- }
-
public int compareTo(MessageResult that) {
// Java 1.5 return (int) Math.signum(uid - that.getUid());
long diff = uid - that.getUid();
@@ -261,6 +258,11 @@ public class StoreMessageResultIterator<
return modSeq;
}
+ @Override
+ public Headers getHeaders() throws MailboxException {
+ throw exception;
+ }
+
}
}
Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/ByteContent.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/ByteContent.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/ByteContent.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/ByteContent.java Sun Aug 21 18:41:14 2011
@@ -22,21 +22,21 @@
*/
package org.apache.james.mailbox.store.streaming;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.channels.WritableByteChannel;
+import java.io.InputStream;
import org.apache.james.mailbox.Content;
public final class ByteContent implements Content {
- private final ByteBuffer contents;
+ private final byte[] contents;
private final long size;
- public ByteContent(final ByteBuffer contents) {
+ public ByteContent(final byte[] contents) {
this.contents = contents;
- size = contents.limit();
+ size = contents.length;
}
/*
@@ -47,14 +47,10 @@ public final class ByteContent implement
return size;
}
- /*
- * (non-Javadoc)
- * @see org.apache.james.mailbox.Content#writeTo(java.nio.channels.WritableByteChannel)
- */
- public void writeTo(WritableByteChannel channel) throws IOException {
- contents.rewind();
- while (channel.write(contents) > 0) {
- // write more
- }
+ @Override
+ public InputStream getInputStream() throws IOException {
+ return new ByteArrayInputStream(contents);
}
+
+
}
Copied: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java (from r1155009, james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/AbstractFullContent.java)
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java?p2=james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java&p1=james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/AbstractFullContent.java&r1=1155009&r2=1160032&rev=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/AbstractFullContent.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java Sun Aug 21 18:41:14 2011
@@ -18,33 +18,39 @@
****************************************************************/
package org.apache.james.mailbox.store.streaming;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.channels.WritableByteChannel;
+import java.io.InputStream;
+import java.io.SequenceInputStream;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.james.mailbox.Content;
import org.apache.james.mailbox.MessageResult;
import org.apache.james.mailbox.MessageResult.Header;
-import org.apache.james.mailbox.store.ResultUtils;
/**
* Abstract base class for {@link Content} implementations which hold the headers and
* the body a email
*
*/
-public abstract class AbstractFullContent implements Content {
+public class FullByteContent implements Content {
private List<Header> headers;
+ private byte[] body;
+ private long size;
- public AbstractFullContent(final List<MessageResult.Header> headers) throws IOException {
+ public FullByteContent(final byte[] body, final List<MessageResult.Header> headers) throws IOException {
this.headers = headers;
+ this.body = body;
+ this.size = caculateSize();
}
protected long caculateSize() throws IOException{
- long result = getBodySize();
+ long result = body.length;
result += 2;
for (final Iterator<MessageResult.Header> it = headers.iterator(); it.hasNext();) {
final MessageResult.Header header = it.next();
@@ -55,57 +61,28 @@ public abstract class AbstractFullConten
}
return result;
}
-
- /*
- * (non-Javadoc)
- * @see org.apache.james.mailbox.Content#writeTo(java.nio.channels.WritableByteChannel)
- */
- public final void writeTo(WritableByteChannel channel) throws IOException {
- ByteBuffer newLine = ByteBuffer.wrap(ResultUtils.BYTES_NEW_LINE);
+ @Override
+ public InputStream getInputStream() throws IOException {
+ List<InputStream> inputs = new ArrayList<InputStream>();
for (final Iterator<MessageResult.Header> it = headers.iterator(); it.hasNext();) {
final MessageResult.Header header = it.next();
if (header != null) {
- header.writeTo(channel);
+
+ inputs.add(header.getInputStream());
}
- newLine.rewind();
- writeAll(channel, newLine);
+ inputs.add(new ByteArrayInputStream("\r\n".getBytes()));
}
- newLine.rewind();
- writeAll(channel, newLine);
- bodyWriteTo(channel);
+ inputs.add(new ByteArrayInputStream("\r\n".getBytes()));
+ inputs.add(new ByteArrayInputStream(body));
+ return new SequenceInputStream(Collections.enumeration(inputs));
}
-
- /**
- * Write all
- *
- * @param channel
- * @param buffer
- * @throws IOException
- */
- protected void writeAll(WritableByteChannel channel, ByteBuffer buffer)
- throws IOException {
- while (channel.write(buffer) > 0) {
- // write more
- }
+ @Override
+ public long size() {
+ return size;
}
- /**
- * Return the size of the body
- *
- * @return size
- * @throws IOException
- */
- protected abstract long getBodySize() throws IOException;
-
- /**
- * Write the body to the channel
- *
- * @param channel
- * @throws IOException
- */
- protected abstract void bodyWriteTo(WritableByteChannel channel) throws IOException;
}
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=1160032&r1=1160031&r2=1160032&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 Sun Aug 21 18:41:14 2011
@@ -21,7 +21,6 @@ package org.apache.james.mailbox.store.s
import java.io.IOException;
import java.io.InputStream;
-import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -132,12 +131,12 @@ public class PartContentBuilder {
public Content getFullContent() throws IOException, UnexpectedEOFException, MimeException {
final List<Header> headers = getMimeHeaders();
final byte[] content = mimeBodyContent();
- return new FullByteContent(ByteBuffer.wrap(content), headers);
+ return new FullByteContent(content, headers);
}
public Content getMessageBodyContent() throws IOException, MimeException {
final byte[] content = messageBodyContent();
- return new ByteContent(ByteBuffer.wrap(content));
+ return new ByteContent(content);
}
private byte[] messageBodyContent() throws IOException, MimeException {
@@ -177,7 +176,7 @@ public class PartContentBuilder {
public Content getMimeBodyContent() throws IOException, MimeException {
final byte[] content = mimeBodyContent();
- return new ByteContent(ByteBuffer.wrap(content));
+ return new ByteContent(content);
}
private byte[] mimeBodyContent() throws IOException, MimeException {
Modified: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderComplexMultipartTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderComplexMultipartTest.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderComplexMultipartTest.java (original)
+++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderComplexMultipartTest.java Sun Aug 21 18:41:14 2011
@@ -27,6 +27,7 @@ import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
+import org.apache.commons.io.IOUtils;
import org.apache.james.mailbox.MessageResult.Header;
import org.apache.james.mailbox.store.ResultHeader;
import org.apache.james.mailbox.store.streaming.PartContentBuilder;
@@ -191,16 +192,12 @@ public class PartContentBuilderComplexMu
private String fullContent(int[] position) throws Exception {
to(position);
- StringBuilderChannel buffer = new StringBuilderChannel();
- builder.getFullContent().writeTo(buffer);
- return buffer.toString();
+ return IOUtils.toString(builder.getFullContent().getInputStream());
}
private String bodyContent(int[] position) throws Exception {
to(position);
- StringBuilderChannel buffer = new StringBuilderChannel();
- builder.getMimeBodyContent().writeTo(buffer);
- return buffer.toString();
+ return IOUtils.toString(builder.getMimeBodyContent().getInputStream());
}
private void checkContentType(String contentType, int[] position)
Modified: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderMultipartAlternativeTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderMultipartAlternativeTest.java?rev=1160032&r1=1160031&r2=1160032&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderMultipartAlternativeTest.java (original)
+++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/PartContentBuilderMultipartAlternativeTest.java Sun Aug 21 18:41:14 2011
@@ -26,6 +26,9 @@ import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
+import javax.management.openmbean.InvalidOpenTypeException;
+
+import org.apache.commons.io.IOUtils;
import org.apache.james.mailbox.MessageResult.Header;
import org.apache.james.mailbox.store.ResultHeader;
import org.apache.james.mailbox.store.streaming.PartContentBuilder;
@@ -104,9 +107,7 @@ public class PartContentBuilderMultipart
.encode(mail).array());
builder.parse(in);
builder.to(position);
- StringBuilderChannel buffer = new StringBuilderChannel();
- builder.getFullContent().writeTo(buffer);
- return buffer.toString();
+ return IOUtils.toString(builder.getFullContent().getInputStream());
}
private String bodyContent(String mail, int position) throws Exception {
@@ -114,9 +115,7 @@ public class PartContentBuilderMultipart
.encode(mail).array());
builder.parse(in);
builder.to(position);
- StringBuilderChannel buffer = new StringBuilderChannel();
- builder.getMimeBodyContent().writeTo(buffer);
- return buffer.toString();
+ return IOUtils.toString(builder.getMimeBodyContent().getInputStream());
}
private void checkContentType(String contentType, String mail, int position)
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org