1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 03:32:16 -05:00

Work around the BinaryTempFileBodyInputStream mess

This commit is contained in:
cketti 2015-01-22 04:56:08 +01:00
parent 64e92ab1c1
commit fe7b88f7c2
2 changed files with 15 additions and 30 deletions

View File

@ -48,7 +48,7 @@ public class MessageExtractor {
* determine the charset from HTML message. * determine the charset from HTML message.
*/ */
if (mimeType.equalsIgnoreCase("text/html") && charset == null) { if (mimeType.equalsIgnoreCase("text/html") && charset == null) {
InputStream in = part.getBody().getInputStream(); InputStream in = MimeUtility.decodeBody(body);
try { try {
byte[] buf = new byte[256]; byte[] buf = new byte[256];
in.read(buf, 0, buf.length); in.read(buf, 0, buf.length);
@ -64,18 +64,8 @@ public class MessageExtractor {
} }
} finally { } finally {
try { try {
if (in instanceof BinaryTempFileBody.BinaryTempFileBodyInputStream) { MimeUtility.closeInputStreamWithoutDeletingTemporaryFiles(in);
/* } catch (IOException e) { /* ignore */ }
* If this is a BinaryTempFileBodyInputStream, calling close()
* will delete the file. But we can't let that happen because
* the file needs to be opened again by the code a few lines
* down.
*/
((BinaryTempFileBody.BinaryTempFileBodyInputStream) in).closeWithoutDeleting();
} else {
in.close();
}
} catch (Exception e) { /* ignore */ }
} }
} }
charset = fixupCharset(charset, getMessageFromPart(part)); charset = fixupCharset(charset, getMessageFromPart(part));
@ -86,20 +76,10 @@ public class MessageExtractor {
*/ */
InputStream in = MimeUtility.decodeBody(body); InputStream in = MimeUtility.decodeBody(body);
try { try {
String text = CharsetSupport.readToString(in, charset); return CharsetSupport.readToString(in, charset);
// Replace the body with a TextBody that already contains the decoded text
part.setBody(new TextBody(text));
return text;
} finally { } finally {
try { try {
/* MimeUtility.closeInputStreamWithoutDeletingTemporaryFiles(in);
* This time we don't care if it's a BinaryTempFileBodyInputStream. We
* replaced the body with a TextBody instance and hence don't need the
* file anymore.
*/
in.close();
} catch (IOException e) { /* Ignore */ } } catch (IOException e) { /* Ignore */ }
} }
} }

View File

@ -16,10 +16,7 @@ import org.apache.james.mime4j.util.MimeUtil;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -1029,7 +1026,7 @@ public class MimeUtility {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
super.close(); super.close();
rawInputStream.close(); closeInputStreamWithoutDeletingTemporaryFiles(rawInputStream);
} }
}; };
} else if (MimeUtil.ENC_QUOTED_PRINTABLE.equalsIgnoreCase(encoding)) { } else if (MimeUtil.ENC_QUOTED_PRINTABLE.equalsIgnoreCase(encoding)) {
@ -1037,7 +1034,7 @@ public class MimeUtility {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
super.close(); super.close();
rawInputStream.close(); closeInputStreamWithoutDeletingTemporaryFiles(rawInputStream);
} }
}; };
} else { } else {
@ -1050,6 +1047,14 @@ public class MimeUtility {
return inputStream; return inputStream;
} }
public static void closeInputStreamWithoutDeletingTemporaryFiles(InputStream rawInputStream) throws IOException {
if (rawInputStream instanceof BinaryTempFileBody.BinaryTempFileBodyInputStream) {
((BinaryTempFileBody.BinaryTempFileBodyInputStream) rawInputStream).closeWithoutDeleting();
} else {
rawInputStream.close();
}
}
public static String getMimeTypeByExtension(String filename) { public static String getMimeTypeByExtension(String filename) {
String returnedType = null; String returnedType = null;
String extension = null; String extension = null;