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 60 @Override getInputStream()61 public InputStream getInputStream() throws MessagingException { 62 try { 63 return new BinaryTempFileBodyInputStream(new FileInputStream(mFile)); 64 } 65 catch (IOException ioe) { 66 throw new MessagingException("Unable to open body", ioe); 67 } 68 } 69 70 @Override writeTo(OutputStream out)71 public void writeTo(OutputStream out) throws IOException, MessagingException { 72 InputStream in = getInputStream(); 73 Base64OutputStream base64Out = new Base64OutputStream( 74 out, Base64.CRLF | Base64.NO_CLOSE); 75 IOUtils.copy(in, base64Out); 76 base64Out.close(); 77 mFile.delete(); 78 } 79 80 class BinaryTempFileBodyInputStream extends FilterInputStream { BinaryTempFileBodyInputStream(InputStream in)81 public BinaryTempFileBodyInputStream(InputStream in) { 82 super(in); 83 } 84 85 @Override close()86 public void close() throws IOException { 87 super.close(); 88 mFile.delete(); 89 } 90 } 91 } 92