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