ImapTempFileLiteral.java revision f419287f22ae44f25e1ba1f757ec33c7941bbfa8
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 android.util.Log; 20 21import com.android.email.FixedLengthInputStream; 22import com.android.emailcommon.Logging; 23import com.android.emailcommon.TempDirectory; 24import com.android.emailcommon.utility.Utility; 25 26import org.apache.commons.io.IOUtils; 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 byte[] bytes = IOUtils.toByteArray(getAsStream()); 92 // Prevent crash from OOM; we've seen this, but only rarely and not reproducibly 93 if (bytes.length > ImapResponseParser.LITERAL_KEEP_IN_MEMORY_THRESHOLD) { 94 throw new IOException(); 95 } 96 return Utility.fromAscii(bytes); 97 } catch (IOException e) { 98 Log.w(Logging.LOG_TAG, "ImapTempFileLiteral: Error while reading temp file", e); 99 return ""; 100 } 101 } 102 103 @Override 104 public void destroy() { 105 try { 106 if (!isDestroyed() && mFile.exists()) { 107 mFile.delete(); 108 } 109 } catch (RuntimeException re) { 110 // Just log and ignore. 111 Log.w(Logging.LOG_TAG, "Failed to remove temp file: " + re.getMessage()); 112 } 113 super.destroy(); 114 } 115 116 @Override 117 public String toString() { 118 return String.format("{%d byte literal(file)}", mSize); 119 } 120 121 public boolean tempFileExistsForTest() { 122 return mFile.exists(); 123 } 124} 125