/* * Copyright (c) 1997-2011 Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; /** * This class models a UUEncoded Message sent from MS Outlook etc.
* * The message structure looks like this := * [text body] [uuencoded attachment]* *
* i.e., an optional text/plain main-body, followed by zero or more
* UUENCODE-ed attachments.
*
* @author John Mani
* @author Bill Shannon
* @see javax.mail.internet.MimeMessage
*/
public class MSMessage extends MimeMessage {
private String type;
/**
* Constructor that converts a MimeMessage object into a MSMessage.
*
* @exception MessagingException if the given MimeMessage
* is not a non-MIME MS message, or if an
* IOException occurs when accessing the given
* MimeMessage object
*/
public MSMessage(Session session, MimeMessage msg)
throws MessagingException {
super(session);
if (!isMSMessage(msg)) // sanity check
throw new MessagingException("Not an MS message");
class FastByteArrayOutputStream extends ByteArrayOutputStream {
ByteArrayInputStream toByteArrayInputStream() {
return new ByteArrayInputStream(buf, 0, count);
}
}
// extract the bytes of the given message
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
try {
msg.writeTo(bos);
} catch (IOException ioex) {
throw new MessagingException("IOException", ioex);
} catch (Exception ex) {
throw new MessagingException("Exception", ex);
}
//parse(new ByteArrayInputStream(bos.toByteArray()));
parse(bos.toByteArrayInputStream());
}
/**
* Constructor to create a MSMessage from the given InputStream.
*/
public MSMessage(Session session, InputStream is)
throws MessagingException {
super(session); // setup headerstore etc
parse(is);
}
// parse input stream
protected void parse(InputStream is) throws MessagingException {
// Create a buffered input stream for efficiency
if (!(is instanceof ByteArrayInputStream) &&
!(is instanceof BufferedInputStream))
is = new BufferedInputStream(is);
// Load headerstore
headers.load(is);
/*
* Load the content into a byte[].
* This byte[] is shared among the bodyparts.
*/
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int b;
// XXX - room for performance improvement
while ((b = is.read()) != -1)
bos.write(b);
content = bos.toByteArray();
} catch (IOException ioex) {
throw new MessagingException("IOException", ioex);
}
/*
* Check whether this is multipart.
*/
boolean isMulti = false;
// check for presence of X-MS-Attachment header
String[] att = getHeader("X-MS-Attachment");
if (att != null && att.length > 0)
isMulti = true;
else {
/*
* Fall back to scanning the content.
* We scan the content until we find a sequence that looks
* like the start of a uuencoded block, i.e., "