1/*
2 * Copyright (C) 2010 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.email.mail.store.imap;
18
19import com.android.email.FixedLengthInputStream;
20import com.android.emailcommon.Logging;
21import com.android.emailcommon.TempDirectory;
22import com.android.emailcommon.utility.Utility;
23
24import org.apache.commons.io.IOUtils;
25
26import android.util.Log;
27
28import java.io.ByteArrayInputStream;
29import java.io.File;
30import java.io.FileInputStream;
31import java.io.FileNotFoundException;
32import java.io.FileOutputStream;
33import java.io.IOException;
34import java.io.InputStream;
35import java.io.OutputStream;
36
37/**
38 * Subclass of {@link ImapString} used for literals backed by a temp file.
39 */
40public class ImapTempFileLiteral extends ImapString {
41    /* package for test */ final File mFile;
42
43    /** Size is purely for toString() */
44    private final int mSize;
45
46    /* package */  ImapTempFileLiteral(FixedLengthInputStream stream) throws IOException {
47        mSize = stream.getLength();
48        mFile = File.createTempFile("imap", ".tmp", TempDirectory.getTempDirectory());
49
50        // Unfortunately, we can't really use deleteOnExit(), because temp filenames are random
51        // so it'd simply cause a memory leak.
52        // deleteOnExit() simply adds filenames to a static list and the list will never shrink.
53        // mFile.deleteOnExit();
54        OutputStream out = new FileOutputStream(mFile);
55        IOUtils.copy(stream, out);
56        out.close();
57    }
58
59    /**
60     * Make sure we delete the temp file.
61     *
62     * We should always be calling {@link ImapResponse#destroy()}, but it's here as a last resort.
63     */
64    @Override
65    protected void finalize() throws Throwable {
66        try {
67            destroy();
68        } finally {
69            super.finalize();
70        }
71    }
72
73    @Override
74    public InputStream getAsStream() {
75        checkNotDestroyed();
76        try {
77            return new FileInputStream(mFile);
78        } catch (FileNotFoundException e) {
79            // It's probably possible if we're low on storage and the system clears the cache dir.
80            Log.w(Logging.LOG_TAG, "ImapTempFileLiteral: Temp file not found");
81
82            // Return 0 byte stream as a dummy...
83            return new ByteArrayInputStream(new byte[0]);
84        }
85    }
86
87    @Override
88    public String getString() {
89        checkNotDestroyed();
90        try {
91            return Utility.fromAscii(IOUtils.toByteArray(getAsStream()));
92        } catch (IOException e) {
93            Log.w(Logging.LOG_TAG, "ImapTempFileLiteral: Error while reading temp file");
94            return "";
95        }
96    }
97
98    @Override
99    public void destroy() {
100        try {
101            if (!isDestroyed() && mFile.exists()) {
102                mFile.delete();
103            }
104        } catch (RuntimeException re) {
105            // Just log and ignore.
106            Log.w(Logging.LOG_TAG, "Failed to remove temp file: " + re.getMessage());
107        }
108        super.destroy();
109    }
110
111    @Override
112    public String toString() {
113        return String.format("{%d byte literal(file)}", mSize);
114    }
115
116    public boolean tempFileExistsForTest() {
117        return mFile.exists();
118    }
119}
120