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