1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.emailcommon.internet;
18
19import com.android.emailcommon.mail.Body;
20import com.android.emailcommon.mail.MessagingException;
21
22import android.util.Base64;
23
24import java.io.ByteArrayInputStream;
25import java.io.IOException;
26import java.io.InputStream;
27import java.io.OutputStream;
28import java.io.UnsupportedEncodingException;
29
30public class TextBody implements Body {
31    String mBody;
32
33    public TextBody(String body) {
34        this.mBody = body;
35    }
36
37    public void writeTo(OutputStream out) throws IOException, MessagingException {
38        byte[] bytes = mBody.getBytes("UTF-8");
39        out.write(Base64.encode(bytes, Base64.CRLF));
40    }
41
42    /**
43     * Get the text of the body in it's unencoded format.
44     * @return
45     */
46    public String getText() {
47        return mBody;
48    }
49
50    /**
51     * Returns an InputStream that reads this body's text in UTF-8 format.
52     */
53    public InputStream getInputStream() throws MessagingException {
54        try {
55            byte[] b = mBody.getBytes("UTF-8");
56            return new ByteArrayInputStream(b);
57        }
58        catch (UnsupportedEncodingException usee) {
59            return null;
60        }
61    }
62}
63