mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-27 03:32:16 -05:00
Merge pull request #91 from andrewgaul/findbugs-stream-close
Improve the way we open/close streams.
This commit is contained in:
commit
7f046e5f0a
@ -6,6 +6,7 @@ import android.content.Context;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.fsck.k9.K9;
|
import com.fsck.k9.K9;
|
||||||
import com.fsck.k9.helper.DomainNameChecker;
|
import com.fsck.k9.helper.DomainNameChecker;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
import javax.net.ssl.TrustManager;
|
import javax.net.ssl.TrustManager;
|
||||||
import javax.net.ssl.X509TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
||||||
@ -105,12 +106,12 @@ public final class TrustManagerFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
java.io.InputStream fis = null;
|
||||||
try {
|
try {
|
||||||
javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509");
|
javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509");
|
||||||
Application app = K9.app;
|
Application app = K9.app;
|
||||||
keyStoreFile = new File(app.getDir("KeyStore", Context.MODE_PRIVATE) + File.separator + "KeyStore.bks");
|
keyStoreFile = new File(app.getDir("KeyStore", Context.MODE_PRIVATE) + File.separator + "KeyStore.bks");
|
||||||
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||||
java.io.FileInputStream fis;
|
|
||||||
try {
|
try {
|
||||||
fis = new java.io.FileInputStream(keyStoreFile);
|
fis = new java.io.FileInputStream(keyStoreFile);
|
||||||
} catch (FileNotFoundException e1) {
|
} catch (FileNotFoundException e1) {
|
||||||
@ -118,9 +119,6 @@ public final class TrustManagerFactory {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
keyStore.load(fis, "".toCharArray());
|
keyStore.load(fis, "".toCharArray());
|
||||||
//if (fis != null) {
|
|
||||||
// fis.close();
|
|
||||||
//}
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(LOG_TAG, "KeyStore IOException while initializing TrustManagerFactory ", e);
|
Log.e(LOG_TAG, "KeyStore IOException while initializing TrustManagerFactory ", e);
|
||||||
keyStore = null;
|
keyStore = null;
|
||||||
@ -154,6 +152,8 @@ public final class TrustManagerFactory {
|
|||||||
Log.e(LOG_TAG, "Unable to get X509 Trust Manager ", e);
|
Log.e(LOG_TAG, "Unable to get X509 Trust Manager ", e);
|
||||||
} catch (KeyStoreException e) {
|
} catch (KeyStoreException e) {
|
||||||
Log.e(LOG_TAG, "Key Store exception while initializing TrustManagerFactory ", e);
|
Log.e(LOG_TAG, "Key Store exception while initializing TrustManagerFactory ", e);
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(fis);
|
||||||
}
|
}
|
||||||
unsecureTrustManager = new SimpleX509TrustManager();
|
unsecureTrustManager = new SimpleX509TrustManager();
|
||||||
}
|
}
|
||||||
@ -195,17 +195,18 @@ public final class TrustManagerFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
java.io.FileOutputStream keyStoreStream;
|
java.io.OutputStream keyStoreStream = null;
|
||||||
try {
|
try {
|
||||||
keyStoreStream = new java.io.FileOutputStream(keyStoreFile);
|
keyStoreStream = new java.io.FileOutputStream(keyStoreFile);
|
||||||
keyStore.store(keyStoreStream, "".toCharArray());
|
keyStore.store(keyStoreStream, "".toCharArray());
|
||||||
keyStoreStream.close();
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
||||||
} catch (CertificateException e) {
|
} catch (CertificateException e) {
|
||||||
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(keyStoreStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
@ -11,6 +11,7 @@ import com.fsck.k9.mail.Folder.OpenMode;
|
|||||||
import com.fsck.k9.mail.filter.EOLConvertingOutputStream;
|
import com.fsck.k9.mail.filter.EOLConvertingOutputStream;
|
||||||
import com.fsck.k9.mail.internet.MimeMessage;
|
import com.fsck.k9.mail.internet.MimeMessage;
|
||||||
import com.fsck.k9.mail.transport.TrustedSocketFactory;
|
import com.fsck.k9.mail.transport.TrustedSocketFactory;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.http.*;
|
import org.apache.http.*;
|
||||||
import org.apache.http.client.CookieStore;
|
import org.apache.http.client.CookieStore;
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
@ -1481,26 +1482,32 @@ public class WebDavStore extends Store {
|
|||||||
StringBuilder buffer = new StringBuilder();
|
StringBuilder buffer = new StringBuilder();
|
||||||
String tempText = "";
|
String tempText = "";
|
||||||
String resultText = "";
|
String resultText = "";
|
||||||
BufferedReader reader;
|
BufferedReader reader = null;
|
||||||
int currentLines = 0;
|
int currentLines = 0;
|
||||||
|
|
||||||
istream = WebDavHttpClient.getUngzippedContent(entity);
|
try {
|
||||||
|
istream = WebDavHttpClient.getUngzippedContent(entity);
|
||||||
|
|
||||||
if (lines != -1) {
|
if (lines != -1) {
|
||||||
reader = new BufferedReader(new InputStreamReader(istream), 8192);
|
reader = new BufferedReader(new InputStreamReader(istream), 8192);
|
||||||
|
|
||||||
while ((tempText = reader.readLine()) != null &&
|
while ((tempText = reader.readLine()) != null &&
|
||||||
(currentLines < lines)) {
|
(currentLines < lines)) {
|
||||||
buffer.append(tempText).append("\r\n");
|
buffer.append(tempText).append("\r\n");
|
||||||
currentLines++;
|
currentLines++;
|
||||||
|
}
|
||||||
|
|
||||||
|
istream.close();
|
||||||
|
resultText = buffer.toString();
|
||||||
|
istream = new ByteArrayInputStream(resultText.getBytes("UTF-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
istream.close();
|
wdMessage.parse(istream);
|
||||||
resultText = buffer.toString();
|
|
||||||
istream = new ByteArrayInputStream(resultText.getBytes("UTF-8"));
|
|
||||||
}
|
|
||||||
|
|
||||||
wdMessage.parse(istream);
|
} finally {
|
||||||
|
IOUtils.closeQuietly(reader);
|
||||||
|
IOUtils.closeQuietly(istream);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IllegalArgumentException iae) {
|
} catch (IllegalArgumentException iae) {
|
||||||
|
Loading…
Reference in New Issue
Block a user