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