1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-11 12:05:06 -05:00

Merge pull request #91 from andrewgaul/findbugs-stream-close

Improve the way we open/close streams.
This commit is contained in:
Andrew Chen 2011-11-03 06:40:07 -07:00
commit 7f046e5f0a
2 changed files with 27 additions and 19 deletions

View File

@ -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) {

View File

@ -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) {