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