diff --git a/src/com/fsck/k9/mail/store/TrustManagerFactory.java b/src/com/fsck/k9/mail/store/TrustManagerFactory.java index 32c9f4572..f6a907080 100644 --- a/src/com/fsck/k9/mail/store/TrustManagerFactory.java +++ b/src/com/fsck/k9/mail/store/TrustManagerFactory.java @@ -6,6 +6,7 @@ import android.content.Context; import android.util.Log; import com.fsck.k9.K9; import com.fsck.k9.helper.DomainNameChecker; +import org.apache.commons.io.IOUtils; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; @@ -105,12 +106,12 @@ public final class TrustManagerFactory { } static { + java.io.InputStream fis = null; try { javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509"); Application app = K9.app; keyStoreFile = new File(app.getDir("KeyStore", Context.MODE_PRIVATE) + File.separator + "KeyStore.bks"); keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); - java.io.FileInputStream fis; try { fis = new java.io.FileInputStream(keyStoreFile); } catch (FileNotFoundException e1) { @@ -118,9 +119,6 @@ public final class TrustManagerFactory { } try { keyStore.load(fis, "".toCharArray()); - //if (fis != null) { - // fis.close(); - //} } catch (IOException e) { Log.e(LOG_TAG, "KeyStore IOException while initializing TrustManagerFactory ", e); keyStore = null; @@ -154,6 +152,8 @@ public final class TrustManagerFactory { Log.e(LOG_TAG, "Unable to get X509 Trust Manager ", e); } catch (KeyStoreException e) { Log.e(LOG_TAG, "Key Store exception while initializing TrustManagerFactory ", e); + } finally { + IOUtils.closeQuietly(fis); } unsecureTrustManager = new SimpleX509TrustManager(); } @@ -195,17 +195,18 @@ public final class TrustManagerFactory { } } } - java.io.FileOutputStream keyStoreStream; + java.io.OutputStream keyStoreStream = null; try { keyStoreStream = new java.io.FileOutputStream(keyStoreFile); keyStore.store(keyStoreStream, "".toCharArray()); - keyStoreStream.close(); } catch (FileNotFoundException e) { throw new CertificateException("Unable to write KeyStore: " + e.getMessage()); } catch (CertificateException e) { throw new CertificateException("Unable to write KeyStore: " + e.getMessage()); } catch (IOException e) { throw new CertificateException("Unable to write KeyStore: " + e.getMessage()); + } finally { + IOUtils.closeQuietly(keyStoreStream); } } catch (NoSuchAlgorithmException e) { diff --git a/src/com/fsck/k9/mail/store/WebDavStore.java b/src/com/fsck/k9/mail/store/WebDavStore.java index 4e94ac471..6a1e52e06 100644 --- a/src/com/fsck/k9/mail/store/WebDavStore.java +++ b/src/com/fsck/k9/mail/store/WebDavStore.java @@ -11,6 +11,7 @@ import com.fsck.k9.mail.Folder.OpenMode; import com.fsck.k9.mail.filter.EOLConvertingOutputStream; import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.transport.TrustedSocketFactory; +import org.apache.commons.io.IOUtils; import org.apache.http.*; import org.apache.http.client.CookieStore; import org.apache.http.client.entity.UrlEncodedFormEntity; @@ -1481,26 +1482,32 @@ public class WebDavStore extends Store { StringBuilder buffer = new StringBuilder(); String tempText = ""; String resultText = ""; - BufferedReader reader; + BufferedReader reader = null; int currentLines = 0; - istream = WebDavHttpClient.getUngzippedContent(entity); + try { + istream = WebDavHttpClient.getUngzippedContent(entity); - if (lines != -1) { - reader = new BufferedReader(new InputStreamReader(istream), 8192); + if (lines != -1) { + reader = new BufferedReader(new InputStreamReader(istream), 8192); - while ((tempText = reader.readLine()) != null && - (currentLines < lines)) { - buffer.append(tempText).append("\r\n"); - currentLines++; + while ((tempText = reader.readLine()) != null && + (currentLines < lines)) { + buffer.append(tempText).append("\r\n"); + currentLines++; + } + + istream.close(); + resultText = buffer.toString(); + istream = new ByteArrayInputStream(resultText.getBytes("UTF-8")); } - istream.close(); - resultText = buffer.toString(); - istream = new ByteArrayInputStream(resultText.getBytes("UTF-8")); - } + wdMessage.parse(istream); - wdMessage.parse(istream); + } finally { + IOUtils.closeQuietly(reader); + IOUtils.closeQuietly(istream); + } } } catch (IllegalArgumentException iae) {