Clean up; close streams, readers (potential memory leaks)
This commit is contained in:
@@ -102,7 +102,9 @@ public class Digest
|
||||
{
|
||||
MessageDigest md = getMessageDigest();
|
||||
StringBuilder data = new StringBuilder();
|
||||
PrintWriter pout = new PrintWriter(
|
||||
PrintWriter pout = null;
|
||||
try {
|
||||
pout = new PrintWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(output), "UTF-8"));
|
||||
|
||||
// compute and append the MD5 digest of each resource in the list
|
||||
@@ -123,8 +125,12 @@ public class Digest
|
||||
byte[] contents = data.toString().getBytes("UTF-8");
|
||||
pout.println(DIGEST_FILE + " = " + StringUtil.hexlate(md.digest(contents)));
|
||||
|
||||
} finally {
|
||||
if (pout != null) {
|
||||
pout.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains an appropriate message digest instance for use by the Getdown system.
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import com.samskivert.io.StreamUtil;
|
||||
import com.threerings.getdown.data.Application;
|
||||
import com.threerings.getdown.data.Digest;
|
||||
import com.threerings.getdown.data.Resource;
|
||||
@@ -81,15 +82,19 @@ public class Digester
|
||||
File inputFile = new File(appdir, Digest.DIGEST_FILE);
|
||||
File signatureFile = new File(appdir, Digest.DIGEST_FILE + Application.SIGNATURE_SUFFIX);
|
||||
|
||||
FileInputStream storeInput = null;
|
||||
FileInputStream dataInput = null;
|
||||
FileOutputStream signatureOutput = null;
|
||||
try {
|
||||
// initialize the keystore
|
||||
KeyStore store = KeyStore.getInstance("JKS");
|
||||
FileInputStream storeInput = new FileInputStream(storePath);
|
||||
storeInput = new FileInputStream(storePath);
|
||||
store.load(storeInput, storePass.toCharArray());
|
||||
PrivateKey key = (PrivateKey)store.getKey(storeAlias, storePass.toCharArray());
|
||||
|
||||
// sign the digest file
|
||||
Signature sig = Signature.getInstance("SHA1withRSA");
|
||||
FileInputStream dataInput = new FileInputStream(inputFile);
|
||||
dataInput = new FileInputStream(inputFile);
|
||||
byte[] buffer = new byte[8192];
|
||||
int length;
|
||||
|
||||
@@ -99,8 +104,14 @@ public class Digester
|
||||
}
|
||||
|
||||
// Write out the signature
|
||||
FileOutputStream signatureOutput = new FileOutputStream(signatureFile);
|
||||
signatureOutput = new FileOutputStream(signatureFile);
|
||||
String signed = new String(Base64.encodeBase64(sig.sign()));
|
||||
signatureOutput.write(signed.getBytes("utf8"));
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(storeInput);
|
||||
StreamUtil.close(dataInput);
|
||||
StreamUtil.close(signatureOutput);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,25 +5,25 @@
|
||||
|
||||
package com.threerings.getdown.tools;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.LineNumberReader;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.JarOutputStream;
|
||||
|
||||
import com.threerings.getdown.util.ProgressObserver;
|
||||
|
||||
@@ -43,15 +43,19 @@ public class JarDiffPatcher implements JarDiffCodes
|
||||
*
|
||||
* @throws IOException if any problem occurs during patching.
|
||||
*/
|
||||
public void patchJar (String jarPath, String diffPath, OutputStream target,
|
||||
public void patchJar (String jarPath, String diffPath, File target,
|
||||
ProgressObserver observer)
|
||||
throws IOException
|
||||
{
|
||||
File oldFile = new File(jarPath);
|
||||
File diffFile = new File(diffPath);
|
||||
JarOutputStream jos = new JarOutputStream(target);
|
||||
JarFile oldJar = new JarFile(oldFile);
|
||||
JarFile jarDiff = new JarFile(diffFile);
|
||||
JarOutputStream jos = null;
|
||||
JarFile oldJar = null;
|
||||
JarFile jarDiff = null;
|
||||
try {
|
||||
jos = new JarOutputStream(new FileOutputStream(target));
|
||||
oldJar = new JarFile(oldFile);
|
||||
jarDiff = new JarFile(diffFile);
|
||||
Set<String> ignoreSet = new HashSet<String>();
|
||||
|
||||
Map<String, String> renameMap = new HashMap<String, String>();
|
||||
@@ -158,7 +162,11 @@ public class JarDiffPatcher implements JarDiffCodes
|
||||
}
|
||||
updateObserver(observer, currentEntry, size);
|
||||
|
||||
jos.finish();
|
||||
} finally {
|
||||
jos = closeStream(jos);
|
||||
oldJar = closeFile(oldJar);
|
||||
jarDiff = closeFile(jarDiff);
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateObserver (ProgressObserver observer,
|
||||
@@ -281,6 +289,26 @@ public class JarDiffPatcher implements JarDiffCodes
|
||||
data.close();
|
||||
}
|
||||
|
||||
private static JarFile closeFile(JarFile jar) {
|
||||
if (jar != null) {
|
||||
try {
|
||||
jar.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static JarOutputStream closeStream(JarOutputStream jar) {
|
||||
if (jar != null) {
|
||||
try {
|
||||
jar.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static final int DEFAULT_READ_SIZE = 2048;
|
||||
|
||||
protected static byte[] newBytes = new byte[DEFAULT_READ_SIZE];
|
||||
|
||||
@@ -149,8 +149,6 @@ public class Patcher
|
||||
FileOutputStream fout = null;
|
||||
try {
|
||||
StreamUtil.copy(in = file.getInputStream(entry), fout = new FileOutputStream(patch));
|
||||
StreamUtil.close(fout);
|
||||
fout = null;
|
||||
|
||||
// move the current version of the jar to .old
|
||||
if (!FileUtil.renameTo(target, otarget)) {
|
||||
@@ -168,8 +166,7 @@ public class Patcher
|
||||
|
||||
// now apply the patch to create the new target file
|
||||
patcher = new JarDiffPatcher();
|
||||
fout = new FileOutputStream(target);
|
||||
patcher.patchJar(otarget.getPath(), patch.getPath(), fout, obs);
|
||||
patcher.patchJar(otarget.getPath(), patch.getPath(), target, obs);
|
||||
|
||||
} catch (IOException ioe) {
|
||||
if (patcher == null) {
|
||||
|
||||
@@ -32,11 +32,10 @@ public class VersionUtil
|
||||
*/
|
||||
public static long readVersion (File vfile)
|
||||
{
|
||||
FileInputStream fin = null;
|
||||
long fileVersion = -1;
|
||||
BufferedReader bin = null;
|
||||
try {
|
||||
fin = new FileInputStream(vfile);
|
||||
BufferedReader bin = new BufferedReader(new InputStreamReader(fin));
|
||||
bin = new BufferedReader(new InputStreamReader(new FileInputStream(vfile)));
|
||||
String vstr = bin.readLine();
|
||||
if (!StringUtil.isBlank(vstr)) {
|
||||
fileVersion = Long.parseLong(vstr);
|
||||
@@ -44,7 +43,7 @@ public class VersionUtil
|
||||
} catch (Exception e) {
|
||||
log.info("Unable to read version file: " + e.getMessage());
|
||||
} finally {
|
||||
StreamUtil.close(fin);
|
||||
StreamUtil.close(bin);
|
||||
}
|
||||
|
||||
return fileVersion;
|
||||
@@ -89,15 +88,15 @@ public class VersionUtil
|
||||
*/
|
||||
public static long readReleaseVersion (File relfile, String versRegex)
|
||||
{
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new FileReader(relfile));
|
||||
in = new BufferedReader(new FileReader(relfile));
|
||||
String line = null, relvers = null;
|
||||
while ((line = in.readLine()) != null) {
|
||||
if (line.startsWith("JAVA_VERSION=")) {
|
||||
relvers = line.substring("JAVA_VERSION=".length()).replace('"', ' ').trim();
|
||||
}
|
||||
}
|
||||
in.close();
|
||||
|
||||
if (relvers == null) {
|
||||
log.warning("No JAVA_VERSION line in 'release' file", "file", relfile);
|
||||
@@ -108,6 +107,8 @@ public class VersionUtil
|
||||
} catch (Exception e) {
|
||||
log.warning("Failed to read version from 'release' file", "file", relfile, e);
|
||||
return 0L;
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user