diff options
| author | Ade Lee <alee@redhat.com> | 2013-03-08 09:29:02 -0500 |
|---|---|---|
| committer | Ade Lee <alee@redhat.com> | 2013-03-08 21:26:30 -0500 |
| commit | ddc391f574fff16f84cfa485c09ebc495f654340 (patch) | |
| tree | 11400da443e222716cb68d22b74ba108347e2fed /base/common/src/com | |
| parent | b953c172bf274352c628ffef7d3ef0ef4c9ce59d (diff) | |
Plug resource leaks
Diffstat (limited to 'base/common/src/com')
10 files changed, 53 insertions, 77 deletions
diff --git a/base/common/src/com/netscape/certsrv/authentication/AuthToken.java b/base/common/src/com/netscape/certsrv/authentication/AuthToken.java index 827278711..c44ed7668 100644 --- a/base/common/src/com/netscape/certsrv/authentication/AuthToken.java +++ b/base/common/src/com/netscape/certsrv/authentication/AuthToken.java @@ -257,9 +257,9 @@ public class AuthToken implements IAuthToken { if (value == null) { return false; } - DerOutputStream out = new DerOutputStream(); + DerValue[] derValues = new DerValue[value.length]; - try { + try (DerOutputStream out = new DerOutputStream()) { for (int i = 0; i < value.length; i++) { derValues[i] = new DerValue(value[i]); } @@ -343,10 +343,9 @@ public class AuthToken implements IAuthToken { if (value == null) { return false; } - DerOutputStream derStream = new DerOutputStream(); X509Certificate[] certArray = value.getCertificates(); DerValue[] derValues = new DerValue[certArray.length]; - try { + try (DerOutputStream derStream = new DerOutputStream()) { for (int i = 0; i < certArray.length; i++) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try { @@ -386,9 +385,9 @@ public class AuthToken implements IAuthToken { if (value == null) { return false; } - DerOutputStream out = new DerOutputStream(); + DerValue[] derValues = new DerValue[value.length]; - try { + try (DerOutputStream out = new DerOutputStream()) { for (int i = 0; i < value.length; i++) { derValues[i] = new DerValue(DerValue.tag_OctetString, value[i]); } diff --git a/base/common/src/com/netscape/cms/logging/LogFile.java b/base/common/src/com/netscape/cms/logging/LogFile.java index caada0bf5..2c3510c74 100644 --- a/base/common/src/com/netscape/cms/logging/LogFile.java +++ b/base/common/src/com/netscape/cms/logging/LogFile.java @@ -343,18 +343,15 @@ public class LogFile implements ILogEventListener, IExtendedPluginInfo { // All this streaming is lame, but Base64OutputStream needs a // PrintStream ByteArrayOutputStream output = new ByteArrayOutputStream(); - Base64OutputStream b64 = new Base64OutputStream(new - PrintStream(new - FilterOutputStream(output) - ) - ); - - b64.write(bytes); - b64.flush(); - - // This is internationally safe because Base64 chars are - // contained within 8859_1 - return output.toString("8859_1"); + try (Base64OutputStream b64 = new Base64OutputStream( + new PrintStream(new FilterOutputStream(output)))) { + b64.write(bytes); + b64.flush(); + + // This is internationally safe because Base64 chars are + // contained within 8859_1 + return output.toString("8859_1"); + } } private static boolean mInSignedAuditLogFailureMode = false; diff --git a/base/common/src/com/netscape/cms/profile/def/EnrollDefault.java b/base/common/src/com/netscape/cms/profile/def/EnrollDefault.java index 53ec23b40..7b4c3c74d 100644 --- a/base/common/src/com/netscape/cms/profile/def/EnrollDefault.java +++ b/base/common/src/com/netscape/cms/profile/def/EnrollDefault.java @@ -620,8 +620,7 @@ public abstract class EnrollDefault implements IPolicyDefault, ICertInfoPolicyDe // if the OID isn't valid (ex. n.n) the error isn't caught til // encoding time leaving a bad request in the request queue. - try { - DerOutputStream derOut = new DerOutputStream(); + try (DerOutputStream derOut = new DerOutputStream()) { derOut.putOID(v); new ObjectIdentifier(new DerInputStream(derOut.toByteArray())); diff --git a/base/common/src/com/netscape/cms/profile/def/GenericExtDefault.java b/base/common/src/com/netscape/cms/profile/def/GenericExtDefault.java index 4a8acace1..f344648ab 100644 --- a/base/common/src/com/netscape/cms/profile/def/GenericExtDefault.java +++ b/base/common/src/com/netscape/cms/profile/def/GenericExtDefault.java @@ -230,7 +230,7 @@ public class GenericExtDefault extends EnrollExtDefault { public Extension createExtension(IRequest request) { Extension ext = null; - try { + try (DerOutputStream out = new DerOutputStream()) { boolean critical = getConfigBoolean(CONFIG_CRITICAL); ObjectIdentifier oid = new ObjectIdentifier(getConfig(CONFIG_OID)); byte data[] = null; @@ -241,7 +241,6 @@ public class GenericExtDefault extends EnrollExtDefault { data = getBytes(mapPattern(request, getConfig(CONFIG_DATA))); } - DerOutputStream out = new DerOutputStream(); out.putOctetString(data); ext = new Extension(oid, critical, out.toByteArray()); diff --git a/base/common/src/com/netscape/cms/servlet/common/CMSTemplate.java b/base/common/src/com/netscape/cms/servlet/common/CMSTemplate.java index 0188b781a..336032dd3 100644 --- a/base/common/src/com/netscape/cms/servlet/common/CMSTemplate.java +++ b/base/common/src/com/netscape/cms/servlet/common/CMSTemplate.java @@ -145,14 +145,9 @@ public class CMSTemplate extends CMSFile { Enumeration<IArgBlock> q = null; IArgBlock r = null; CMSTemplateParams data = input; - HTTPOutputStreamWriter http_out = null; - if (mCharset == null) - http_out = new HTTPOutputStreamWriter(rout); - else - http_out = new HTTPOutputStreamWriter(rout, mCharset); - - try { + try (HTTPOutputStreamWriter http_out = (mCharset == null ? + new HTTPOutputStreamWriter(rout): new HTTPOutputStreamWriter(rout, mCharset))) { templateLine out = new templateLine(); // Output the prolog diff --git a/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java b/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java index 6e269089e..78d8dab6b 100644 --- a/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java +++ b/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.net.URL; import java.net.URLDecoder; import java.util.Locale; -import java.util.Random; import javax.servlet.ServletConfig; import javax.servlet.ServletException; @@ -49,7 +48,6 @@ public class GetCookie extends CMSServlet { * */ private static final long serialVersionUID = 2466968231929541707L; - private static Random mRandom = null; private String mErrorFormPath = null; private String mFormPath = null; @@ -72,7 +70,6 @@ public class GetCookie extends CMSServlet { CMS.debug("GetCookie init"); mTemplates.remove(CMSRequest.SUCCESS); - mRandom = new Random(); mErrorFormPath = sc.getInitParameter("errorTemplatePath"); if (mOutputTemplatePath != null) { mFormPath = mOutputTemplatePath; diff --git a/base/common/src/com/netscape/cmscore/base/PropConfigStore.java b/base/common/src/com/netscape/cmscore/base/PropConfigStore.java index 075d4be78..161f549f2 100644 --- a/base/common/src/com/netscape/cmscore/base/PropConfigStore.java +++ b/base/common/src/com/netscape/cmscore/base/PropConfigStore.java @@ -355,10 +355,8 @@ public class PropConfigStore implements IConfigStore, Cloneable { */ public void putByteArray(String name, byte value[]) { ByteArrayOutputStream output = new ByteArrayOutputStream(); - Base64OutputStream b64 = new Base64OutputStream(new - PrintStream(new FilterOutputStream(output))); - - try { + try (Base64OutputStream b64 = new Base64OutputStream(new + PrintStream(new FilterOutputStream(output)))) { b64.write(value); b64.flush(); diff --git a/base/common/src/com/netscape/cmscore/security/KeyCertUtil.java b/base/common/src/com/netscape/cmscore/security/KeyCertUtil.java index 210c04227..b91b4cf93 100644 --- a/base/common/src/com/netscape/cmscore/security/KeyCertUtil.java +++ b/base/common/src/com/netscape/cmscore/security/KeyCertUtil.java @@ -175,36 +175,34 @@ public class KeyCertUtil { // All this streaming is lame, but Base64OutputStream needs a // PrintStream ByteArrayOutputStream output = new ByteArrayOutputStream(); - Base64OutputStream b64 = new Base64OutputStream(new - PrintStream(new - FilterOutputStream(output) - ) - ); - - b64.write(bytes); - b64.flush(); - - // This is internationally safe because Base64 chars are - // contained within 8859_1 - return output.toString("8859_1"); + try (Base64OutputStream b64 = new Base64OutputStream( + new PrintStream(new FilterOutputStream(output)))) { + + b64.write(bytes); + b64.flush(); + + // This is internationally safe because Base64 chars are + // contained within 8859_1 + return output.toString("8859_1"); + } } public static byte[] makeDSSParms(BigInteger P, BigInteger Q, BigInteger G) throws IOException { + try (DerOutputStream sequence = new DerOutputStream()) { - // Write P, Q, G to a DER stream - DerOutputStream contents = new DerOutputStream(); + // Write P, Q, G to a DER stream + DerOutputStream contents = new DerOutputStream(); - contents.putInteger(new BigInt(P)); - contents.putInteger(new BigInt(Q)); - contents.putInteger(new BigInt(G)); + contents.putInteger(new BigInt(P)); + contents.putInteger(new BigInt(Q)); + contents.putInteger(new BigInt(G)); - // Make a sequence from the PQG stream - DerOutputStream sequence = new DerOutputStream(); + // Make a sequence from the PQG stream + sequence.write(DerValue.tag_Sequence, contents); - sequence.write(DerValue.tag_Sequence, contents); - - return sequence.toByteArray(); + return sequence.toByteArray(); + } } public static PrivateKey getPrivateKey(String tokenname, String nickname) @@ -235,7 +233,7 @@ public class KeyCertUtil { public static X509CertImpl signCert(PrivateKey privateKey, X509CertInfo certInfo, SignatureAlgorithm sigAlg) throws NoSuchTokenException, EBaseException, NotInitializedException { - try { + try (DerOutputStream out = new DerOutputStream()) { CertificateAlgorithmId sId = (CertificateAlgorithmId) certInfo.get(X509CertInfo.ALGORITHM_ID); AlgorithmId sigAlgId = @@ -246,7 +244,6 @@ public class KeyCertUtil { CryptoToken token = priKey.getOwningToken(); DerOutputStream tmp = new DerOutputStream(); - DerOutputStream out = new DerOutputStream(); certInfo.encode(tmp); diff --git a/base/common/src/com/netscape/cmscore/security/PWsdrCache.java b/base/common/src/com/netscape/cmscore/security/PWsdrCache.java index d723d55ed..729a368f2 100644 --- a/base/common/src/com/netscape/cmscore/security/PWsdrCache.java +++ b/base/common/src/com/netscape/cmscore/security/PWsdrCache.java @@ -191,18 +191,16 @@ public class PWsdrCache { // All this streaming is lame, but Base64OutputStream needs a // PrintStream ByteArrayOutputStream output = new ByteArrayOutputStream(); - Base64OutputStream b64 = new Base64OutputStream(new - PrintStream(new - FilterOutputStream(output) - ) - ); - - b64.write(bytes); - b64.flush(); - - // This is internationally safe because Base64 chars are - // contained within 8859_1 - return output.toString("8859_1"); + try (Base64OutputStream b64 = new Base64OutputStream( + new PrintStream(new FilterOutputStream(output)))) { + + b64.write(bytes); + b64.flush(); + + // This is internationally safe because Base64 chars are + // contained within 8859_1 + return output.toString("8859_1"); + } } // for PWCBsdr diff --git a/base/common/src/com/netscape/cmscore/util/ExceptionFormatter.java b/base/common/src/com/netscape/cmscore/util/ExceptionFormatter.java index 88dd32a05..14351cd7b 100644 --- a/base/common/src/com/netscape/cmscore/util/ExceptionFormatter.java +++ b/base/common/src/com/netscape/cmscore/util/ExceptionFormatter.java @@ -35,12 +35,10 @@ public class ExceptionFormatter { public static String getStackTraceAsString(Throwable e) { String returnvalue = e.toString(); - try { - PipedOutputStream po = new PipedOutputStream(); - PipedInputStream pi = new PipedInputStream(po); + PipedOutputStream po = new PipedOutputStream(); + try (PipedInputStream pi = new PipedInputStream(po)) { PrintWriter ps = new PrintWriter(po); - e.printStackTrace(ps); ps.flush(); @@ -52,7 +50,6 @@ public class ExceptionFormatter { } catch (Exception ex) { } return returnvalue; - } /* test code below */ |
