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
17package com.android.emailcommon.internet;
18
19import com.android.emailcommon.TempDirectory;
20import com.android.emailcommon.mail.Body;
21import com.android.emailcommon.mail.MessagingException;
22
23import org.apache.commons.io.IOUtils;
24
25import android.util.Base64;
26import android.util.Base64OutputStream;
27
28import java.io.File;
29import java.io.FileInputStream;
30import java.io.FileOutputStream;
31import java.io.FilterInputStream;
32import java.io.IOException;
33import java.io.InputStream;
34import 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 */
42public 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     */
50    public void setFile(String filePath) {
51        mFile = new File(filePath);
52    }
53
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    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    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 {
79        public BinaryTempFileBodyInputStream(InputStream in) {
80            super(in);
81        }
82
83        @Override
84        public void close() throws IOException {
85            super.close();
86            mFile.delete();
87        }
88    }
89}
90