2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.mail.internet;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.mail.Body;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.io.*;
|
|
|
|
|
2010-02-05 09:26:09 -05:00
|
|
|
import org.apache.james.mime4j.codec.QuotedPrintableOutputStream;
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public class TextBody implements Body
|
|
|
|
{
|
2010-08-02 07:55:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Immutable empty byte array
|
|
|
|
*/
|
|
|
|
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
|
|
|
|
2010-02-04 18:37:50 -05:00
|
|
|
private String mBody;
|
|
|
|
private String mEncoding;
|
2011-01-04 08:25:59 -05:00
|
|
|
private String mCharset = "UTF-8";
|
2011-01-12 18:48:28 -05:00
|
|
|
// Length of the message composed (as opposed to quoted). I don't like the name of this variable and am open to
|
|
|
|
// suggestions as to what it should otherwise be. -achen 20101207
|
|
|
|
private Integer mComposedMessageLength;
|
|
|
|
// Offset from position 0 where the composed message begins.
|
|
|
|
private Integer mComposedMessageOffset;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public TextBody(String body)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
this.mBody = body;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void writeTo(OutputStream out) throws IOException, MessagingException
|
|
|
|
{
|
2010-02-04 18:37:50 -05:00
|
|
|
if (mBody != null)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2011-01-04 08:25:59 -05:00
|
|
|
byte[] bytes = mBody.getBytes(mCharset);
|
2010-02-04 18:37:50 -05:00
|
|
|
if ("8bit".equals(mEncoding))
|
|
|
|
{
|
|
|
|
out.write(bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-05 09:26:09 -05:00
|
|
|
QuotedPrintableOutputStream qp = new QuotedPrintableOutputStream(out, false);
|
|
|
|
qp.write(bytes);
|
|
|
|
qp.flush();
|
2010-02-04 18:37:50 -05:00
|
|
|
}
|
2009-05-22 01:42:16 -04:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
/**
|
2009-11-24 19:40:29 -05:00
|
|
|
* Get the text of the body in it's unencoded format.
|
2008-11-01 17:32:06 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
public String getText()
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-04 08:25:59 -05:00
|
|
|
* Returns an InputStream that reads this body's text.
|
2008-11-01 17:32:06 -04:00
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
public InputStream getInputStream() throws MessagingException
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2009-05-22 01:42:16 -04:00
|
|
|
byte[] b;
|
2009-11-24 19:40:29 -05:00
|
|
|
if (mBody!=null)
|
|
|
|
{
|
2011-01-04 08:25:59 -05:00
|
|
|
b = mBody.getBytes(mCharset);
|
2009-05-22 01:42:16 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else
|
|
|
|
{
|
2010-08-02 07:55:31 -04:00
|
|
|
b = EMPTY_BYTE_ARRAY;
|
2009-05-22 01:42:16 -04:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
return new ByteArrayInputStream(b);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (UnsupportedEncodingException usee)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2010-02-04 18:37:50 -05:00
|
|
|
|
|
|
|
public void setEncoding(String encoding)
|
|
|
|
{
|
2010-04-29 00:59:14 -04:00
|
|
|
mEncoding = encoding;
|
2010-02-04 18:37:50 -05:00
|
|
|
}
|
2011-01-04 08:25:59 -05:00
|
|
|
|
|
|
|
public void setCharset(String charset)
|
|
|
|
{
|
|
|
|
mCharset = charset;
|
|
|
|
}
|
2011-01-12 18:48:28 -05:00
|
|
|
|
|
|
|
public Integer getComposedMessageLength()
|
|
|
|
{
|
|
|
|
return mComposedMessageLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setComposedMessageLength(Integer composedMessageLength)
|
|
|
|
{
|
|
|
|
this.mComposedMessageLength = composedMessageLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Integer getComposedMessageOffset()
|
|
|
|
{
|
|
|
|
return mComposedMessageOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setComposedMessageOffset(Integer composedMessageOffset)
|
|
|
|
{
|
|
|
|
this.mComposedMessageOffset = composedMessageOffset;
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|