• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.phone.common.mail.internet;
17 
18 import com.android.phone.common.mail.Body;
19 import com.android.phone.common.mail.MessagingException;
20 import com.android.phone.common.mail.TempDirectory;
21 
22 import org.apache.commons.io.IOUtils;
23 
24 import android.util.Base64;
25 import android.util.Base64OutputStream;
26 
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.FilterInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 
35 /**
36  * A Body that is backed by a temp file. The Body exposes a getOutputStream method that allows
37  * the user to write to the temp file. After the write the body is available via getInputStream
38  * and writeTo one time. After writeTo is called, or the InputStream returned from
39  * getInputStream is closed the file is deleted and the Body should be considered disposed of.
40  */
41 public class BinaryTempFileBody implements Body {
42     private File mFile;
43 
44     /**
45      * An alternate way to put data into a BinaryTempFileBody is to simply supply an already-
46      * created file.  Note that this file will be deleted after it is read.
47      * @param filePath The file containing the data to be stored on disk temporarily
48      */
setFile(String filePath)49     public void setFile(String filePath) {
50         mFile = new File(filePath);
51     }
52 
getOutputStream()53     public OutputStream getOutputStream() throws IOException {
54         mFile = File.createTempFile("body", null, TempDirectory.getTempDirectory());
55         mFile.deleteOnExit();
56         return new FileOutputStream(mFile);
57     }
58 
59     @Override
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 
69     @Override
writeTo(OutputStream out)70     public void writeTo(OutputStream out) throws IOException, MessagingException {
71         InputStream in = getInputStream();
72         Base64OutputStream base64Out = new Base64OutputStream(
73             out, Base64.CRLF | Base64.NO_CLOSE);
74         IOUtils.copy(in, base64Out);
75         base64Out.close();
76         mFile.delete();
77         in.close();
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