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.email.mail.internet;
     18 
     19 import com.android.email.Email;
     20 import com.android.email.mail.Body;
     21 import com.android.email.mail.MessagingException;
     22 
     23 import org.apache.commons.io.IOUtils;
     24 
     25 import android.util.Config;
     26 import android.util.Log;
     27 import android.util.Base64;
     28 import android.util.Base64OutputStream;
     29 
     30 import java.io.File;
     31 import java.io.FileInputStream;
     32 import java.io.FileOutputStream;
     33 import java.io.FilterInputStream;
     34 import java.io.IOException;
     35 import java.io.InputStream;
     36 import java.io.OutputStream;
     37 
     38 /**
     39  * A Body that is backed by a temp file. The Body exposes a getOutputStream method that allows
     40  * the user to write to the temp file. After the write the body is available via getInputStream
     41  * and writeTo one time. After writeTo is called, or the InputStream returned from
     42  * getInputStream is closed the file is deleted and the Body should be considered disposed of.
     43  */
     44 public class BinaryTempFileBody implements Body {
     45     private static File mTempDirectory;
     46 
     47     private File mFile;
     48 
     49     public static void setTempDirectory(File tempDirectory) {
     50         mTempDirectory = tempDirectory;
     51     }
     52 
     53     public BinaryTempFileBody() throws IOException {
     54         if (mTempDirectory == null) {
     55             throw new
     56                 RuntimeException("setTempDirectory has not been called on BinaryTempFileBody!");
     57         }
     58     }
     59 
     60     /**
     61      * An alternate way to put data into a BinaryTempFileBody is to simply supply an already-
     62      * created file.  Note that this file will be deleted after it is read.
     63      * @param filePath The file containing the data to be stored on disk temporarily
     64      */
     65     public void setFile(String filePath) {
     66         mFile = new File(filePath);
     67     }
     68 
     69     public OutputStream getOutputStream() throws IOException {
     70         mFile = File.createTempFile("body", null, mTempDirectory);
     71         mFile.deleteOnExit();
     72         return new FileOutputStream(mFile);
     73     }
     74 
     75     public InputStream getInputStream() throws MessagingException {
     76         try {
     77             return new BinaryTempFileBodyInputStream(new FileInputStream(mFile));
     78         }
     79         catch (IOException ioe) {
     80             throw new MessagingException("Unable to open body", ioe);
     81         }
     82     }
     83 
     84     public void writeTo(OutputStream out) throws IOException, MessagingException {
     85         InputStream in = getInputStream();
     86         Base64OutputStream base64Out = new Base64OutputStream(
     87             out, Base64.CRLF | Base64.NO_CLOSE);
     88         IOUtils.copy(in, base64Out);
     89         base64Out.close();
     90         mFile.delete();
     91     }
     92 
     93     class BinaryTempFileBodyInputStream extends FilterInputStream {
     94         public BinaryTempFileBodyInputStream(InputStream in) {
     95             super(in);
     96         }
     97 
     98         @Override
     99         public void close() throws IOException {
    100             super.close();
    101             mFile.delete();
    102         }
    103     }
    104 }
    105