Home | History | Annotate | Download | only in internet
      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 
     17 package com.android.emailcommon.internet;
     18 
     19 import com.android.emailcommon.mail.Body;
     20 import com.android.emailcommon.mail.MessagingException;
     21 
     22 import android.util.Base64;
     23 
     24 import java.io.ByteArrayInputStream;
     25 import java.io.IOException;
     26 import java.io.InputStream;
     27 import java.io.OutputStream;
     28 import java.io.UnsupportedEncodingException;
     29 
     30 public class TextBody implements Body {
     31     String mBody;
     32 
     33     public TextBody(String body) {
     34         this.mBody = body;
     35     }
     36 
     37     @Override
     38     public void writeTo(OutputStream out) throws IOException, MessagingException {
     39         byte[] bytes = mBody.getBytes("UTF-8");
     40         out.write(Base64.encode(bytes, Base64.CRLF));
     41     }
     42 
     43     /**
     44      * Get the text of the body in it's unencoded format.
     45      * @return
     46      */
     47     public String getText() {
     48         return mBody;
     49     }
     50 
     51     /**
     52      * Returns an InputStream that reads this body's text in UTF-8 format.
     53      */
     54     @Override
     55     public InputStream getInputStream() throws MessagingException {
     56         try {
     57             byte[] b = mBody.getBytes("UTF-8");
     58             return new ByteArrayInputStream(b);
     59         }
     60         catch (UnsupportedEncodingException usee) {
     61             return null;
     62         }
     63     }
     64 }
     65